Advertisement
Guest User

zadanie1pisi

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