January 28, 2014

OOMP Assignment#1

/*
Aim: Create a class named weather report that holds a daily weather report with datamembers day_of_month,hightemp,lowtemp,amount_rain and amount_snow. The
constructor initializes the fields with default values: 99 for day_of_month, 999 for
hightemp,-999 for low emp and 0 for amount_rain and amount_snow. Include a
function that prompts the user and sets values for each field so that you can override
the default values. Write a C++/Java/Python program that creates a monthly report.
*/

#include <iostream>
#include <cmath>
#define MAX_MONTH 12
#define MAX_DAY 30
using namespace std;

class WeatherReport{
 public:
   int day_of_month, high_temp, low_temp, amount_snow, amount_rain;
   WeatherReport(){
     day_of_month = 99;
     high_temp = 999;
     low_temp = -999;
     amount_snow = amount_rain = 0;
   }
   void get(){
     cout << endl << "Enter day of month: ";
     cin >> day_of_month;
     cout << endl << "Enter high temperature: ";
     cin >> high_temp;
     cout << endl << "Enter low temperature: ";
     cin >> low_temp;
     cout << endl << "Enter amount of rain: ";
     cin >> amount_rain;
     cout << endl << "Enter amount of snow: ";
     cin >> amount_snow;
   }
   int format(int number, int digit = 3){
     int base = (int) (pow(10.0, digit-1) + 0.5f);
     cout << ((number < 0) ? "-" : "+");
     number = abs(number);
     while(number < base && base > 1){
       cout << "0";
       base /= 10;
     }
     return number;
   }
   void display(){
     cout << endl;
     cout << format(day_of_month) << "\t";
     cout << format(high_temp) << "\t";
     cout << format(low_temp) << "\t";
     cout << format(amount_rain) << "\t";
     cout << format(amount_snow);
   }
};

class YearReport{
   WeatherReport * wr[MAX_MONTH][MAX_DAY];
   int month, day;
 public:
   YearReport(){
     month = day = 0;
     for(int i = 0; i < MAX_MONTH; i++){
       for(int j = 0; j < MAX_DAY; j++){
         wr[i][j] = new WeatherReport();
       }
     }
   }
   void getMonth(){
     cout << endl << "Enter month: ";
     cin >> month;
     if(month <= 0 || month > MAX_MONTH){
       month = 0;
       cout << endl << "Month must be in the range 1 - " << MAX_MONTH;
     }
   }
   void getDay(){
     cout << endl << "Enter day: ";
     cin >> day;
     if(day <= 0 || day > MAX_DAY){
       day = 0;
       cout << endl << "Day must be in the range 1 - " << MAX_DAY;
     }
   }
   void get(){
     getMonth();
     getDay();
     if(month > 0 && day > 0){
       wr[month-1][day-1]->get();
     }
   }
   void display(){
     getMonth();
     cout << endl << "Day     Temp_H  Temp_L  Rain    Snow";
     cout << endl << "-------------------------------------";
     if(month > 0){
       for(int i = 0; i < MAX_DAY; i++){
         wr[month-1][i]->display();
       }
     }
     displayAverage();
   }
   void displayAverage(){
     if(month > 0){
       float avg_ht = 0, avg_lt = 0, avg_r = 0, avg_s = 0, count = 0;
       for(int i = 0; i < MAX_DAY; i++){
         if(wr[month-1][i]->day_of_month != 99){
           avg_ht += wr[month-1][i]->high_temp;
           avg_lt += wr[month-1][i]->low_temp;
           avg_s += wr[month-1][i]->amount_snow;
           avg_r += wr[month-1][i]->amount_rain;
           count += 1;
         }
       }
       if(count != 0){
         avg_ht /= count;
         avg_lt /= count;
         avg_s /= count;
         avg_r /= count;
       }
       cout << endl << "Avg" << endl << "   \t" << avg_ht << "\t" << avg_lt << "\t" << avg_r << "\t" << avg_s;
     }
   }
};

int main(){
 int choice;
 YearReport * yr = new YearReport();
 do{
   cout << endl << "-- Weather Report --";
   cout << endl << "1. Enter data";
   cout << endl << "2. Display Report";
   cout << endl << "3. Exit";
   cout << endl << "Your choice: ";
   cin >> choice;
   switch(choice){
     case 1 : yr->get(); break;
     case 2 : yr->display(); break;
   }
 }while(choice != 3);
 delete yr;
 return 0;
}

/*
---------------------------------------
             OUTPUT
---------------------------------------


-- Weather Report --
1. Enter data
2. Display Report
3. Exit
Your choice: 1

Enter month: 1

Enter day: 1

Enter day of month: 1

Enter high temperature: 15

Enter low temperature: -10

Enter amount of rain: 20

Enter amount of snow: 10

-- Weather Report --
1. Enter data
2. Display Report
3. Exit
Your choice: 1

Enter month: 1

Enter day: 2

Enter day of month: 2

Enter high temperature: 14

Enter low temperature: -15

Enter amount of rain: 30

Enter amount of snow: 20

-- Weather Report --
1. Enter data
2. Display Report
3. Exit
Your choice: 2

Enter month: 1

Day     Temp_H  Temp_L  Rain    Snow
-------------------------------------
+001    +015    -010    +020    +010
+002    +014    -015    +030    +020
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
+099    +999    -999    +000    +000
Avg
       14.5    -12.5   25      15
-- Weather Report --
1. Enter data
2. Display Report
3. Exit
Your choice: 3
*/

No comments:

Post a Comment