Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #include <stdlib.h>
- #include <iomanip>
- using namespace std;
- class cheat{
- private:
- int x;
- public:
- cheat(int _x): x(_x){};
- int getX()const {return x;}
- ~cheat(){ x = 0;}
- };
- struct People
- {
- size_t size_;
- string * eyeColor;
- string * race;
- double * height;
- int raceCounter[2] = {0, 0};
- };
- void fileInput(ifstream & file, People & data);
- double avgHeight(const People & data);
- void raceParser(People & data);
- void showRacesAnalyze(const People & data);
- void runApp(People & data, ifstream & file);
- int main()
- {
- People data; // struct object
- ifstream file("data.txt"); //input file stream. filename = "data.txt"
- runApp(data, file);
- cheat s(4);
- cout << s.getX();
- return 0;
- }
- void fileInput(ifstream & file, People & data)
- {
- file >> data.size_; //read fro
- data.height = new double[data.size_];
- data.eyeColor = new string[data.size_];
- data.race = new string[data.size_];
- for(size_t i = 0; i < data.size_; i++) //HEIGHT
- {
- file >> data.race[i] >> data.eyeColor[i] >> data.height[i];
- }
- };
- double avgHeight(const People & data){
- double sum = 0;
- for(size_t i = 0; i < data.size_; ++i){
- sum += data.height[i];
- }
- cout << "AVG HEIGHT: ";
- return sum/data.size_;
- };
- void raceParser(People & data){
- for(size_t i = 0; i < data.size_; i++){
- if(data.race[i] == "black")
- data.raceCounter[0]++;
- if(data.race[i] == "white")
- data.raceCounter[1]++;
- }
- }
- void showRacesAnalyze(const People & data){
- if(data.raceCounter[0] > data.raceCounter[1]){
- cout << "RACE on AVG: BLACK with: " << setprecision(4) << data.raceCounter[0]/(double)data.size_*100 << "%." << endl;
- }
- if(data.raceCounter[1] > data.raceCounter[0])
- {
- cout << "RACE on AVG: WHITE with: " << setprecision(4) << data.raceCounter[1]/(double)data.size_*100 << "%." << endl;
- }
- if(data.raceCounter[0] == data.raceCounter[1])
- cout << "Races are equal with WHITES: " << data.raceCounter[1] << " and BLACKS: " << data.raceCounter[0] << "." << endl;
- }
- void runApp(People & data, ifstream & file){
- fileInput(file, data); //calling function called "fileInput" with following arguments: file(file's name) and data(struct)
- //then it goes to fileIput ^
- cout << avgHeight(data) << endl;
- raceParser(data);
- showRacesAnalyze(data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement