Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <cassert>
  6. #include <iterator>
  7. #include <zconf.h>
  8.  
  9. #define assertm(exp, msg) assert(((void)msg, exp))
  10.  
  11. struct DescriptionLine {
  12.     std::vector<int> wspolrzedne;
  13.     double wspolczynnik;
  14. };
  15.  
  16. struct Description {
  17.     int k = 0;
  18.     int n = 0;
  19.     std::vector<DescriptionLine> descriptionLines;
  20. };
  21.  
  22. struct InputLine {
  23.     std::vector<double> wsp;
  24. };
  25.  
  26. struct Input {
  27.     std::vector<InputLine> inputLines;
  28. };
  29.  
  30. Description readDescription(std::string fileName) {
  31.     Description tempDesc;
  32.     bool first_red = false;
  33.     std::ifstream input(fileName);
  34.  
  35.     for( std::string line; getline( input, line ); )
  36.     {
  37.         std::istringstream stream(line);
  38.         std::vector<std::string> results(std::istream_iterator<std::string>{stream},std::istream_iterator<std::string>());
  39.  
  40.         if(!first_red) {
  41.             assertm(results.size() == 2, "FIRST DESC LINE HAS TO BE 2 NUMBERS!");
  42.             tempDesc.k = atoi(results[0].c_str());
  43.             tempDesc.n = atoi(results[1].c_str());
  44.             first_red = true;
  45.             continue;
  46.         }
  47.  
  48.         assertm(results.size() == tempDesc.n + 1 , "DescLine error!");
  49.         DescriptionLine tempDescLine;
  50.  
  51.         for (auto i = 0; i<results.size()-1; i++) {
  52.             tempDescLine.wspolrzedne.push_back(atoi(results[i].c_str()));
  53.         }
  54.         tempDescLine.wspolczynnik = atof(results[results.size()-1].c_str());
  55.         tempDesc.descriptionLines.push_back(tempDescLine);
  56.     }
  57.     return tempDesc;
  58. }
  59.  
  60. Input readInput(std::string fileName) {
  61.     Input tempInput;
  62.  
  63.     std::ifstream input(fileName);
  64.     for( std::string line; getline( input, line ); )
  65.     {
  66.         std::istringstream stream(line);
  67.         std::vector<std::string> results(std::istream_iterator<std::string>{stream},std::istream_iterator<std::string>());
  68.         InputLine tempInputLine;
  69.         for (auto s : results) {
  70.             tempInputLine.wsp.push_back(atof(s.c_str()));
  71.         }
  72.         tempInput.inputLines.push_back(tempInputLine);
  73.     }
  74.     return tempInput;
  75. }
  76.  
  77. void writeOutput(std::vector<double> results, std::string fileName) {
  78.     std::ofstream myfile(fileName);
  79. //    myfile.open ("example.txt");
  80.     for (auto r : results) {
  81.         myfile << r << std::endl;
  82.     }
  83.     myfile.close();
  84. }
  85.  
  86.  
  87. int main(int argc, const char * argv[]) {
  88. //    assertm(argc == 7, "Unexpected args! error!");
  89.     std::string descriptionFileName = "description2.txt";
  90.     std::string inputFileName = "in2.txt";
  91.     std::string outputFileName = "out2.txt";
  92. //
  93. //    for (int i = 0 ; i < argc; i++) {
  94. //        auto arg = std::string(argv[i]);
  95. //
  96. //        if(arg.compare("-d")) { descriptionFileName = std::string(argv[i+1]); continue; }
  97. //        if(arg.compare("<")) { inputFileName = std::string(argv[i+1]); continue; }
  98. //        if(arg.compare(">")) { outputFileName = std::string(argv[i+1]); }
  99. //    }
  100. //
  101.  
  102.     auto description = readDescription(descriptionFileName);
  103.     auto input = readInput(inputFileName);
  104.     auto result = std::vector<double>();
  105.  
  106.     for(int i = 0; i < input.inputLines.size();i++){
  107.  
  108.         result.push_back(0.0);
  109.         result[i] = 0.0;
  110.         for(int j = 0; j < description.descriptionLines.size(); j++){
  111.             double poly = description.descriptionLines[j].wspolczynnik;
  112.             for(int l = 0; l < description.descriptionLines[j].wspolrzedne.size(); l++)
  113.             {
  114.                 int iter = description.descriptionLines[j].wspolrzedne[l]-1;
  115.                 if(iter > -1)
  116.                 {
  117.                     poly *= input.inputLines[i].wsp[iter];
  118.                 }
  119.             }
  120.             result[i] += poly;
  121.         }
  122.     }
  123.  
  124.  
  125.  
  126.  
  127.     writeOutput(result, outputFileName);
  128.  
  129. //    auto line = "2 3 4 2 1 2 3.0";
  130. //    std::istringstream stream(line);
  131. //    std::vector<std::string> results(std::istream_iterator<std::string>{stream},std::istream_iterator<std::string>());
  132. //    auto i = atoi(results[0].c_str());
  133. //
  134.  
  135. //    char * dir = getcwd(NULL, 0); // Platform-dependent, see reference link below
  136. //    printf("Current dir: %s", dir);
  137.  
  138. //    auto d = readDescription("description.txt");
  139.  
  140.  
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement