#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);
}
}
}
}
I'm writting a simple programm with structures which can sort by 3 indicators, I'm thinking about writting a fuction which can do it to avoid repeating parts of my code, but there is a problem.I don't know how exactly I can pass fields from structure as a parametrs to sorting function. Show me how to do it if it possible.