alansam

FZ-Homework

Dec 2nd, 2020 (edited)
769
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <string>
  7. #include <cstdlib>
  8. #include <vector>
  9.  
  10. std::string const file_name_i("./input.txt");
  11. std::string const file_name_o("./output.txt");
  12.  
  13. std::vector<int> read_data(std::string const file_name);
  14. void analysis(std::vector<int> collect,
  15.               std::string const file_name,
  16.               bool serialize = false);
  17.  
  18. int main() {
  19.   std::vector<int> collect;
  20.  
  21.   collect = read_data(file_name_i);
  22.  
  23.   //  analysis
  24.   analysis(collect, file_name_o, true);
  25.  
  26.   return 0;
  27. }
  28.  
  29. // FZ
  30.  
  31. std::vector<int> read_data(std::string const file_name) {
  32.   std::vector<int> collect;
  33.   std::ifstream inStream;
  34.  
  35.   inStream.open(file_name);
  36.   if (!inStream.is_open()) {
  37.     std::string emsg("File open error: ");
  38.     emsg += file_name;
  39.     throw std::runtime_error(emsg);
  40.   }
  41.  
  42.   while (!inStream.eof()) {
  43.     int x_;
  44.     //  just read the file and save data into a vector
  45.     //  allows analysis of the data independent of I/O
  46.     inStream >> x_;
  47.     collect.push_back(x_);
  48.   }
  49.  
  50.   //  resource cleanup
  51.   inStream.close();
  52.  
  53.   return collect;
  54. }
  55.  
  56. void analysis(std::vector<int> collect,
  57.               std::string const file_name,
  58.               bool serialize) {
  59.   std::ofstream outStream;
  60.   std::streambuf * coutbuf;
  61.  
  62.   if (serialize) {
  63.     outStream.open(file_name);
  64.     if (!outStream.is_open()) {
  65.       std::string emsg("File open error: ");
  66.       emsg += file_name;
  67.       throw std::runtime_error(emsg);
  68.     }
  69.     coutbuf = std::cout.rdbuf();
  70.     std::cout.rdbuf(outStream.rdbuf());
  71.   }
  72.  
  73.   std::cout << "=== === ============" << std::endl;
  74.   std::cout << " x  x^2 Current Sum." << std::endl;
  75.   std::cout << "=== === ============" << std::endl;
  76.  
  77.   int sum = 0;
  78.   for (auto xx : collect) {
  79.     sum += xx;
  80.     double pwr = pow(xx, 2);
  81.     std::cout << std::setw(2)
  82.               << xx
  83.               << std::setw(5)
  84.               << pwr
  85.               << std::setw(8)
  86.               << sum
  87.               << std::endl; // isspace(int, pow)
  88.   }
  89.  
  90.   double avg = (static_cast<double>(sum) / collect.size());
  91.   std::cout << '\n'
  92.             << "The average of these "
  93.             << collect.size()
  94.             << " numbers is: "
  95.             << avg
  96.             << std::endl;
  97.  
  98.   if (serialize) {
  99.     //  resource cleanup
  100.     std::cout.rdbuf(coutbuf);
  101.     outStream.close();
  102.   }
  103. }
  104.  
RAW Paste Data