x2311

Untitled

May 2nd, 2022
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     ifstream file;
  9.     file.open("..\\tess.txt");
  10.     //error checking
  11.     if (!file.is_open()) {
  12.         cout << "ERROR\n\n";
  13.         return 1;
  14.     }
  15.  
  16.  
  17.     string str; //читаю строку
  18.     int k = 0;
  19.     int rez = NULL; //результат
  20.     int old_sum = 0;
  21.     while (getline(file, str)) {
  22.         str += " ";
  23.         string::size_type sz = 0;
  24.         int sum = 0;
  25.         int index = 0;
  26.  
  27.         while (sz < str.length()) {
  28.             int i_dec = stoi(str, &sz);
  29.             sum += i_dec;
  30.             str = str.substr(sz + 1);
  31.             index++;
  32.         }
  33.         cout << "In:" << k << "\tsum: " << sum << endl;
  34.         cout << "\n";
  35.         if (k == 0) {
  36.             old_sum = sum;
  37.             rez = k;
  38.         } else if (old_sum > sum) {
  39.             old_sum = sum;
  40.             rez = k;
  41.         }
  42.         k++;
  43.         sum = 0;
  44.     }
  45.     cout << "REZ: " << rez << endl << endl;
  46.     file.close();
  47.  
  48.     ifstream fileNew;
  49.     file.open("..\\tess.txt");
  50.     //error checking
  51.     if (!file.is_open()) {
  52.         cout << "ERROR\n\n";
  53.         return 1;
  54.     }
  55.  
  56.     k = 0;
  57.     while (getline(file, str)) {
  58.         if (k == rez) {
  59.             cout << str;
  60.             break;
  61.         }
  62.         k++;
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment