Advertisement
Alyks

Untitled

Oct 28th, 2019
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. void showMatrix(vector<vector<string>> &matrix) {
  8.     for(int i = 0; i < matrix.size(); i++) {
  9.         for(int j = 0; j < matrix[i].size(); j++) {
  10.             cout << matrix[i][j] << " ";
  11.         }
  12.         cout << endl;
  13.     }
  14. }
  15.  
  16. vector<string> split(string str, string delimiter) {
  17.     vector<string> result;
  18.     int pos = 0;
  19.     while(pos != -1) {
  20.         pos = str.find(delimiter);
  21.         result.push_back(str.substr(0, pos));
  22.         str.erase(0, pos + delimiter.length());
  23.     }
  24.     return result;
  25. }
  26.  
  27. vector<vector<string>> getMatrixFromFile(ifstream& inputFile) {
  28.     vector<vector<string>> matrix;
  29.     vector<string> row;
  30.     string line = "";
  31.     while (getline(inputFile, line)) {
  32.         row = split(line, " ");
  33.         matrix.push_back(row);
  34.     }
  35.     return matrix;
  36. }
  37.  
  38. int checkMatrix(vector<vector<string>> &matrix) {
  39.     int raw = -1;
  40.     int lastMaxZeroesCount = 0;
  41.     for(int i = 0; i < matrix.size(); i++) {
  42.         int zeroes = 0;
  43.         for(int j = 0; j < matrix[i].size(); j++) {
  44.             if(stoi(matrix[i][j]) == 0)
  45.                 zeroes++;
  46.         }
  47.         if(zeroes > lastMaxZeroesCount) {
  48.             lastMaxZeroesCount = zeroes;
  49.             raw = i;
  50.         }
  51.     }
  52.     return raw;
  53. }
  54.  
  55. int main() {
  56.     cout << "Данная программа находит строку матрицы, в которой больше всего нулевых элементов\n" << endl;
  57.     cout << "Введите путь к файлу" << endl;
  58.     bool noException = true;
  59.     string filePath;
  60.     cin >> filePath;
  61.     ifstream inputFile;
  62.     inputFile.open(filePath);
  63.     vector<vector<string>> matrix;
  64.     if(inputFile) {
  65.         matrix = getMatrixFromFile(inputFile);
  66.         inputFile.close();
  67.     } else {
  68.         cout << "Произошла ошибка при чтении файла. Убедитесь, что такой файл существует, либо проверьте имя файла." << endl;
  69.         noException = false;
  70.     }
  71.  
  72.     if(noException) {
  73.         cout << "Получившаяся матрица:\n";
  74.         showMatrix(matrix);
  75.         int result = checkMatrix(matrix)+1;
  76.         string resultText = "";
  77.         if (result == 0)
  78.             resultText = "Нули отсутствуют";
  79.         else
  80.             resultText = "Строка, в которой больше всего нулевых элементов - " + to_string(result);
  81.         cout << resultText << endl;
  82.         ofstream outputFile;
  83.         outputFile.open("output.txt");
  84.         outputFile << resultText;
  85.         cout << "Результат сохранен в файл output.txt" << endl;
  86.     }
  87.  
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement