Advertisement
Guest User

Untitled

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