Advertisement
Guest User

Untitled

a guest
Feb 7th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.40 KB | None | 0 0
  1. household.h
  2.  
  3. /*************************************************
  4.       This is a class describing a single household. It has private member
  5.       variables for the name of the head of the household, the street address
  6.       of the household, the number of people living in the household, and
  7.       the household income. The functions allow access to and modification
  8.       of those attributes either by the application programmer or by the
  9.       user through the input functions.
  10.   **********************************************************/
  11.   #include<iostream>
  12.   #include<string>
  13.   #include<fstream>
  14. #ifndef HOUSEHOLD_H
  15. #define HOUSEHOLD_H
  16.  
  17.   class Household{
  18.     public:
  19.         Household();
  20.         // Mutators
  21.         void set_head(const std::string& hd);
  22.         void set_address(const std::string& add);
  23.         void set_occupants(int num);
  24.         void set_income(int inc);
  25.         // Accessors
  26.         std::string get_head()const;
  27.         std::string get_address()const;
  28.         int get_occupants()const;
  29.         int get_income() const;
  30.         // Input and output
  31.         void input(std::istream& ins);
  32.         void output(std::ostream& outs)const;
  33.   private:
  34.       std::string house_head;
  35.       std::string address;
  36.       int occupants;
  37.       int income;
  38.   };
  39.  
  40.   //overloaded i/o operators
  41.   std::istream& operator >>(std::istream& ins, Household& h);
  42.   std::ostream& operator <<(std::ostream& outs, const Household& h);
  43.  
  44. #endif
  45.  
  46. /*
  47. household.cc
  48.  
  49. This file contains the function descriptions that will allow
  50. for the user to read a text file of people with households into
  51. a census for sorting by name, total income, and the amount of
  52. people in the house.
  53. */
  54. #include "household.h"
  55. #include <string>
  56. #include <fstream>
  57. #include <iostream>
  58. using namespace std;
  59.  
  60.  
  61. Household::Household()
  62. {
  63.     house_head = "unknown";
  64.     address = "unknown";
  65.     occupants = 0;
  66.     income = 0;
  67. }
  68.  
  69. void Household::set_head(const std::string& hd)
  70. {
  71.     house_head = hd;
  72. }
  73.  
  74. void Household::set_address(const std::string& add)
  75. {
  76.     address = add;
  77. }
  78.  
  79. void Household::set_occupants(int num)
  80. {
  81.     occupants = num;
  82. }
  83.  
  84. void Household::set_income(int inc)
  85. {
  86.     income = inc;
  87. }
  88.  
  89. string Household::get_head()const
  90. {
  91.     return house_head;
  92. }
  93.  
  94. string Household::get_address()const
  95. {
  96.     return address;
  97. }
  98.  
  99. int Household::get_occupants()const
  100. {
  101.     return occupants;
  102. }
  103.  
  104. int Household::get_income() const
  105. {
  106.     return income;
  107. }
  108.  
  109. void Household::input(std::istream& ins)
  110. {
  111.     while(ins.peek() == '\n')
  112.     {
  113.         ins.ignore();
  114.     }
  115.     getline(ins, house_head);
  116.     getline(ins, address);
  117.     ins >> occupants;
  118.     ins >> income;
  119. }
  120.  
  121. void Household::output(std::ostream& outs) const
  122. {  
  123.     outs<<house_head;
  124.     outs<<"\n";
  125.     outs<<address;
  126.     outs<<"\n";
  127.     outs<<occupants;
  128.     outs<<"\n";
  129.     outs<<income;
  130.     outs<<"\n";
  131. }
  132.  
  133. istream& operator >>(std::istream& ins, Household& h)
  134. {
  135.     h.input(ins);
  136.     return ins;
  137. }
  138.  
  139. ostream& operator <<(std::ostream& outs, const Household& h)
  140. {
  141.     h.output(outs);
  142.     return outs;
  143. }
  144.  
  145. /*
  146. ward.cc
  147.  
  148. This is the definition holder for the
  149. ward class, which contains and
  150. manipulates all the households
  151. */
  152.  
  153. #include<iostream>
  154. #include<string>
  155. #include<fstream>
  156. #include<string>
  157. #include<cctype>
  158. #include"ward.h"
  159. #include"household.h"
  160. using namespace std;
  161.  
  162.  
  163. Ward::Ward()
  164. {
  165.     used = 0;
  166. }
  167.  
  168. void Ward::load(istream& ins)
  169. {
  170.     ins>>data[used];
  171.     used++;
  172.     while(ins>>data[used])
  173.     {
  174.         cout<< "used = " << used << endl;
  175.         used++;
  176.     }
  177. }
  178.  
  179. void Ward::add(Household& tmp)
  180. {
  181.     if(used < SIZE)
  182.     {
  183.     data[used] = tmp;
  184.     used++;
  185.     }
  186.     else
  187. }
  188.  
  189. void Ward::display(std::ostream& cout)
  190. {
  191.     for(size_t i = 0; i<used; i++)
  192.     {
  193.         cout << data[i];
  194.     }
  195. }
  196.  
  197. Household Ward::find(string name)
  198. {
  199.     string target = lowerCase(name);
  200.     Household error;
  201.  
  202.     for(size_t i = 0; i<used; i++)
  203.     {
  204.         string search = data[i].get_head();
  205.         string lowSearch = lowerCase(search);
  206.         if (target.compare(lowSearch) == 0)
  207.             {
  208.                 return data[i];
  209.             }  
  210.         else    
  211.                 return error;
  212.        
  213.     }
  214. }
  215.  
  216. void Ward::remove(string name)
  217. {
  218.     string target = lowerCase(name);
  219.     Household deleted;
  220.  
  221.     for(size_t i = 0; i<used; i++)
  222.     {
  223.         string search = data[i].get_head();
  224.         string lowSearch = lowerCase(search);
  225.         if (target.compare(lowSearch) == 0)
  226.             {
  227.                 data[i] = deleted;
  228.             }
  229.     }
  230.    
  231. }
  232.  
  233.  
  234. string Ward::lowerCase(string targ)
  235. {
  236.     char t;
  237.     int i = 0;
  238.     while(targ[i])
  239.     {
  240.         t = targ[i];
  241.         putchar(tolower(t));
  242.         i++;
  243.     }
  244.     cout << "lowerCase working";
  245.     return targ;
  246. }
  247.  
  248. void Ward::display_richest()
  249. {
  250.     Household highInc = data[0];
  251.    
  252.     for(size_t i = 0; i < used; i++)
  253.     {
  254.         for(size_t j = 0; j < used; j++)
  255.         {
  256.             int income1 = data[i].get_income();
  257.             int income2 = data[j].get_income();
  258.            
  259.             if(income1 < income2)
  260.             {
  261.                 highInc = data[j];
  262.             }
  263.         }
  264.     }
  265.     cout << highInc;
  266. }
  267.  
  268. void Ward::display_avg_income()
  269. {
  270.     int total = 0;
  271.  
  272.     for(size_t i = 0; i < used; i++)
  273.     {
  274.         total += data[i].get_income(); 
  275.     }
  276.     int average = total/used;
  277.     cout << average;
  278. }
  279.  
  280. void Ward::display_avg_people()
  281. {
  282.     int total = 0;
  283.  
  284.     for(size_t i = 0; i < used; i++)
  285.     {
  286.         total += data[i].get_occupants();  
  287.     }
  288.     int average = total/used;
  289.     cout << average;
  290. }
  291.  
  292. void Ward::sort_by_income()
  293. {
  294.     bool done = false;
  295.     int i;
  296.     Household tmp;
  297.     while(!done)
  298.         {
  299.         done = true;
  300.             for(i=used-1; i > 0; --i)
  301.             {
  302.           if((data[i].get_income()) < (data[i-1].get_income()))
  303.                 {
  304.                     done = false;
  305.                 }
  306.         tmp = data[i];
  307.         data[i] = data[i-1];
  308.         data[i-1] = tmp;
  309.       }
  310.     }
  311. }
  312.  
  313.  
  314. void Ward::sort_by_name()
  315. {
  316.     bool done = false;
  317.     int i;
  318.     Household tmp;
  319.     while(!done)
  320.         {
  321.         done = true;
  322.             for(i=used-1; i > 0; --i)
  323.             {
  324.                
  325.           if((data[i].get_head()) < (data[i-1].get_head()))
  326.                 {
  327.                     done = false;
  328.                 }
  329.         tmp = data[i];
  330.         data[i] = data[i-1];
  331.         data[i-1] = tmp;
  332.       }
  333.     }
  334. }
  335.  
  336. void Ward::save(std::ostream& outs)
  337. {
  338.     for (size_t i = 0; i < used; ++i)
  339.     {
  340.         outs<<data[i];
  341.     }
  342. }
  343.  
  344.  
  345.  
  346. /*
  347.  
  348. ward.h
  349.  
  350. This is a Ward class definition file
  351. */
  352.  
  353.   #include<iostream>
  354.   #include<string>
  355.   #include<fstream>
  356.   #include<string>
  357.   #include<cctype>
  358.   #include"household.h"
  359. #ifndef WARD_H
  360. #define WARD_H
  361.  
  362. class Ward{
  363.  
  364.     public:
  365.     const static int SIZE = 200;
  366.     Ward();
  367.     void load(std::istream& ins);
  368.     void add(Household& tmp);
  369.     void display(std::ostream& cout);
  370.     void remove(std::string name);
  371.     std::string lowerCase(std::string targ);
  372.     Household find(std::string name);
  373.     void display_richest();
  374.     void display_avg_income();
  375.     void display_avg_people();
  376.     void sort_by_income();
  377.     void sort_by_name();
  378.     void save(std::ostream& outs);
  379.  
  380.     private:
  381.     Household data[SIZE];
  382.     size_t used;
  383.        
  384.  
  385. };
  386.  
  387.  
  388. #endif
  389.  
  390. /******************************************************
  391. main.cc
  392.  
  393. You are to write two classes, one called Household which stores
  394. information about a single household, and another called Ward which
  395. stores information about 200 of these households.
  396. You can see the necessary memeber functions of the Ward class below.
  397. *************************************************************/
  398. #include<iostream>
  399. #include<fstream>
  400. #include "household.h"
  401. #include "ward.h"
  402. using namespace std;
  403.  
  404. // The menu function displays the menu and returns the user's choice
  405. int menu();
  406.  
  407. int main(){
  408.     Household tmp;
  409.     Ward neighborhood;
  410.     int choice;
  411.     string name;
  412.     ofstream outs;
  413.     ifstream ins;
  414.     ins.open("households.txt");
  415.  
  416.     // if the file is not there we will assume that this is the first
  417.     // time the program has been run on this machine
  418.  
  419.     if(!ins.fail()){
  420.     neighborhood.load(ins);
  421.     ins.close();
  422.     }
  423.     do{
  424.     choice = menu();
  425.     switch(choice){
  426.         case 1:
  427.         cin>>tmp;
  428.         cout<< "working line 44 ward.cc"<< endl;
  429.         neighborhood.add(tmp); 
  430.         break;
  431.         case 2:
  432.         neighborhood.display(cout);
  433.         break;
  434.         case 3:
  435.         cout<<"Name the head of household for the household you wish to remove:";
  436.         getline(cin,name);
  437.         neighborhood.remove(name);
  438.         break;
  439.         case 4:
  440.         cout<<"Name the head of household for the household you wish to view:";
  441.         getline(cin,name);
  442.         tmp = neighborhood.find(name);
  443.         cout<<tmp;
  444.         break;
  445.         case 5:
  446.         neighborhood.display_richest();
  447.         break;
  448.         case 6:
  449.         neighborhood.display_avg_income();
  450.         break;
  451.         case 7:
  452.         neighborhood.display_avg_people();
  453.         break;
  454.         case 8:
  455.         neighborhood.sort_by_income();
  456.         break;
  457.         case 9:
  458.         neighborhood.sort_by_name();
  459.         break;
  460.         case 0:
  461.         cout<<"Good-bye.\n";
  462.         break;
  463.         default:
  464.         cout<<"Not an option.\n";
  465.         break;
  466.         } // bottom of the switch
  467.     } while(choice != 0);
  468.         // bottom of the while
  469.         outs.open("households.txt");
  470.     if(outs.fail())
  471.         cout<<"Unable to save data to file.\n";
  472.     else
  473.         neighborhood.save(outs);
  474.     outs.close();
  475. return 0;
  476. } // bottom of the main
  477.  
  478. int menu(){
  479.    int choice;
  480.    cout<<"Managing the Ward.\nChoose from the selections below:\n";
  481.    cout<<"1) Add a new household.\n";
  482.    cout<<"2) Display all households in the Ward.\n";
  483.    cout<<"3) Remove a household from the Ward.\n";
  484.    cout<<"4) Find out information about a particular household.\n";
  485.    cout<<"5) Display household with the greatest inome.\n";
  486.    cout<<"6) Display the average income for households in the Ward.\n";
  487.    cout<<"7) Display average number of people living in the household.\n";
  488.    cout<<"8) Sort by income - requires an additional choice 2 to view.\n";
  489.    cout<<"9) Sort by name - requires an addtional choice 2 to view.\n";
  490.  
  491.    cin>>choice;
  492.    // clear out the extra newline character
  493.    if(cin.peek() == '\n') cin.ignore();
  494.    return choice;
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement