1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
#include <iomanip>
#include "rainfall.h"
using namespace std;
void fill(Rainfall *p);
void sort(Rainfall *p);
int main()
{ Rainfall *p = new Rainfall[10];
fill(p);
cout << "Rainfall: Date:\n";
for(int i = 0; i < 10; i++)
{ (p + i) ->displayRainfall();
cout << setw(8) << " ";
(p + i) ->displayDate();
cout << endl;
}
cout << "\nSort in descending order? (Y/N) ";
char choice;
cin >> choice;
if(choice == 'y' || choice == 'Y')
{ sort(p); //********
cout << "Rainfall: Date:\n";
for(int i = 0; i < 10; i++)
{ (p + i) ->displayRainfall();
cout << setw(8) << " ";
(p + i) ->displayDate();
cout << endl;
}}
delete [] p;
p = 0;
return 0;
}
/******************************************************************/
//setData(rainfall, month, day, year)
void fill(Rainfall *p)
{ p ->setData(0.26, 10, 11, 2011);
(p + 1) ->setData(1.1, 10, 8, 2011);
(p + 2) ->setData(0, 10, 7, 2011);
(p + 3) ->setData(0.61, 10, 6, 2011);
(p + 4) ->setData(0, 10, 5, 2011);
(p + 5) ->setData(0.22, 10, 4, 2011);
(p + 6) ->setData(1.11, 10, 1, 2011);
(p + 7) ->setData(0, 9, 30, 2011);
(p + 8) ->setData(0.58, 9, 29, 2011);
(p + 9) ->setData(0.89, 9, 28, 2011);
}
/******************************************************************/
void sort(Rainfall *p)
{ int num = (10 - 1);
for(int r = 0; r < num; num--)
{for(int i = 0; i < num; i++)
{if(p->getRain() > (p + 1)->getRain())
{ Rainfall temp = *p;
*p = *(p + 1);
*(p + 1) = temp;
}}}}
|