SHARE
TWEET

zadanie1pisi

a guest Nov 13th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <cassert>
  7. #include <iterator>
  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() {
  61.     Input tempInput;
  62.     for( std::string line; getline( std::cin, line ); )
  63.     {
  64.         std::istringstream stream(line);
  65.         std::vector<std::string> results(std::istream_iterator<std::string>{stream},std::istream_iterator<std::string>());
  66.         InputLine tempInputLine;
  67.         for (auto s : results) {
  68.             tempInputLine.wsp.push_back(atof(s.c_str()));
  69.         }
  70.         tempInput.inputLines.push_back(tempInputLine);
  71.     }
  72.     return tempInput;
  73. }
  74.  
  75. void writeOutput(std::vector<double> results) {
  76.     for (auto r : results) {
  77.         std::cout << r << std::endl;
  78.     }
  79. }
  80.  
  81.  
  82. int main(int argc, const char * argv[]) {
  83.     assertm(argc == 3, "Unexpected args! error!");
  84.     std::string descriptionFileName;
  85.  
  86.     for (int i = 0 ; i < argc; i++) {
  87.         auto arg = std::string(argv[i]);
  88.         if(arg.compare("-d") == 0) { descriptionFileName = std::string(argv[i+1]); continue; }
  89.     }
  90.  
  91.     auto description = readDescription(descriptionFileName);
  92.     auto input = readInput();
  93.     auto result = std::vector<double>();
  94.  
  95.     for(int i = 0; i < input.inputLines.size();i++){
  96.  
  97.         result.push_back(0.0);
  98.         result[i] = 0.0;
  99.         for(int j = 0; j < description.descriptionLines.size(); j++){
  100.             double poly = description.descriptionLines[j].wspolczynnik;
  101.             for(int l = 0; l < description.descriptionLines[j].wspolrzedne.size(); l++)
  102.             {
  103.                 int iter = description.descriptionLines[j].wspolrzedne[l]-1;
  104.                 if(iter > -1)
  105.                 {
  106.                     poly *= input.inputLines[i].wsp[iter];
  107.                 }
  108.             }
  109.             result[i] += poly;
  110.         }
  111.     }
  112.  
  113.  
  114.     writeOutput(result);
  115.  
  116.  
  117.  
  118.  
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top