Untitled
unknown
plain_text
a year ago
1.9 kB
1
Indexable
Never
#include <iostream> #include <fstream> #include <sstream> #include <string> #include "Forests.h" using namespace std; int main() { Forests forest; forest.ReadingFile("Forests.txt"); forest.PercentOfAllArea(); forest.WoodToArea_ratio(); for (int i = 0; i < 7; i++) { if (forest.area[i] > 300000 && forest.percentOfAllArea[i] > 0.1) cout << forest.name[i] << " " << forest.area[i] << " " << forest.wood[i] << " " << forest.percentOfAllArea[i] << " " << forest.woodToAreaRatio[i] << "\n"; } } ----------------------------------------------------------------- #include <iostream> #include <fstream> #include <cmath> #include <sstream> using namespace std; class Forests { public: string name[7]; long long area[7]; long long wood[7]; double percentOfAllArea[7]; double woodToAreaRatio[7]; long allArea = 8900000; void PercentOfAllArea() { for (int i = 0; i < 7; i++) { percentOfAllArea[i] = round((static_cast<double>(area[i]) / allArea)*1000)/1000; } } void WoodToArea_ratio() { for (int i = 0; i < 7; i++) { woodToAreaRatio[i] = static_cast<double>(wood[i]) / area[i]; } } void ReadingFile(string file) { string path = file; ifstream fin; fin.open(path); if (!fin.is_open()) cout << "Error"; else { cout << "File is opened" << endl; string str; string word; for (int i = 0; i < 7; i++) { str.clear(); getline(fin, str); //str - line istringstream ist(str); int index = 0; while (getline(ist, word, '-')) { switch (index) { case 0: name[i] = word; break; case 1: area[i] = stoll(word); break; case 2: wood[i] = stoll(word); break; } index++; } } } } };