Advertisement
bazmikel

Untitled

Dec 6th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <stdlib.h>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8. class cheat{
  9. private:
  10.     int x;
  11. public:
  12.     cheat(int _x): x(_x){};
  13.     int getX()const {return x;}
  14.     ~cheat(){ x = 0;}
  15. };
  16.  
  17. struct People
  18. {
  19.     size_t size_;
  20.     string * eyeColor;
  21.     string * race;
  22.     double * height;
  23.     int raceCounter[2] = {0, 0};
  24.  
  25. };
  26.  
  27. void fileInput(ifstream & file, People & data);
  28. double avgHeight(const People & data);
  29. void raceParser(People & data);
  30. void showRacesAnalyze(const People & data);
  31. void runApp(People & data, ifstream & file);
  32.  
  33. int main()
  34. {
  35.     People data; // struct object
  36.     ifstream file("data.txt"); //input file stream. filename = "data.txt"
  37.     runApp(data, file);
  38.     cheat s(4);
  39.     cout << s.getX();
  40.    
  41.    
  42.     return 0;
  43. }
  44.  
  45.  
  46. void fileInput(ifstream & file, People & data)
  47. {
  48.     file >> data.size_; //read fro
  49.     data.height = new double[data.size_];
  50.     data.eyeColor = new string[data.size_];
  51.     data.race = new string[data.size_];
  52.  
  53.     for(size_t i = 0; i < data.size_; i++) //HEIGHT
  54.     {
  55.         file >> data.race[i] >> data.eyeColor[i] >> data.height[i];
  56.     }
  57. };
  58.  
  59. double avgHeight(const People & data){
  60.     double sum  = 0;
  61.     for(size_t i = 0; i < data.size_; ++i){
  62.         sum += data.height[i];
  63.     }
  64.  
  65.     cout << "AVG HEIGHT: ";
  66.     return sum/data.size_;
  67. };
  68.  
  69. void raceParser(People & data){
  70.     for(size_t i = 0; i < data.size_; i++){
  71.         if(data.race[i] == "black")
  72.             data.raceCounter[0]++;
  73.         if(data.race[i] == "white")
  74.             data.raceCounter[1]++;
  75.     }
  76. }
  77.  
  78. void showRacesAnalyze(const People & data){
  79.     if(data.raceCounter[0] > data.raceCounter[1]){
  80.     cout << "RACE on AVG: BLACK with: " << setprecision(4) << data.raceCounter[0]/(double)data.size_*100 << "%." << endl;
  81.    }
  82.    if(data.raceCounter[1] > data.raceCounter[0])
  83.    {
  84.     cout << "RACE on AVG: WHITE with: " << setprecision(4) << data.raceCounter[1]/(double)data.size_*100 << "%." << endl;
  85.    }
  86.    if(data.raceCounter[0] == data.raceCounter[1])
  87.     cout << "Races are equal with WHITES: " << data.raceCounter[1] << " and BLACKS: " << data.raceCounter[0] << "." << endl;
  88. }
  89.  
  90.  
  91. void runApp(People & data, ifstream & file){
  92.     fileInput(file, data); //calling function called "fileInput" with following arguments: file(file's name) and data(struct)
  93.     //then it goes to fileIput ^
  94.     cout << avgHeight(data) << endl;
  95.     raceParser(data);
  96.     showRacesAnalyze(data);
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement