如何将结构作为参数传递给函数.C ++

时间:2018-01-18 14:36:17

标签: c++ struct

#include <QtCore/QCoreApplication>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<cctype>
#include<iomanip>
using namespace std; 
void showArr();
void Sort_ind();
void sort_rule();

 struct Weather
    {
        char Date[12];
        int Temperature;
        int Pressure;
        int Humidity;
    };

    Weather arr[30] =
    {
        {"2002.12.15", -4, 115, 5},
        {"2014.5.3", 10, 101, 10},
        {"2001.8.3", 15, 68, 12},
        {"2013.11.9", 12, 123, 3},
    };
    int size = 4;

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        Sort_ind();
        return a.exec();
    }
    void showArr()
    {
        for(int i =0;i < size;i++)
        {
            cout <<setw(4)<<arr[i].Date<<" - Date"
                 <<setw(4)<<arr[i].Temperature<< " - Temperature"
                 <<setw(4)<<arr[i].Pressure<< "  - Pressure"
                 <<setw(4)<<arr[i].Humidity<< " -  % Humidity"<<endl;
        }
    }

    void Sort_ind()
    {
        int k;
        cout <<"0 - Exit\n";
        cout <<"1 - Sort temperature\n";
        cout <<"2 - Sort pressure\n";
        cout <<"3 - Sort humidity\n";
        cin >>k;
        switch(k)
        {
        case 0:exit(0);
        case 1:
                sort_rule() // here must be parametr.For example, we call
                showArr();  // sort function with parametr for Temperature 
                break;      // and sorts by  the right rule
    // and other case for indicators       
    }

    void sort_rule() // and here
    {
        for(int i =0;i< size;i++)
        {
            for(int j = i + 1;j<size;j++)
            {
                if() // compared indicators 
                {
                    swap(arr[i].Pressure, arr[j].Pressure);
                    swap(arr[i].Temperature, arr[j].Temperature);
                    swap(arr[i].Humidity, arr[j].Humidity);
                    swap(arr[i].Date, arr[j].Date);

                }

            }
        }
    }

我正在编写一个简单的程序,其结构可以按3个指标排序, 我正在考虑写一个功能,可以做到这一点,以避免重复我的代码的一部分,但有一个问题。我不知道我怎么能把结构中的字段作为参数传递给排序函数。告诉我如何做到这一点。

0 个答案:

没有答案
相关问题