vertexofvortex

lr11lukyanov

Oct 29th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. /*Дан файл, содержащий натуральные числа. Определить количество чисел
  2. этого файла, оканчивающихся заданной цифрой.*/
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. int main() {
  11.     setlocale(LC_ALL, "rus");
  12.  
  13.     string path;
  14.     char digit;
  15.     string line;
  16.     int counter = 0;
  17.  
  18.     cout << "Введите путь к файлу: ";
  19.     cin >> path;
  20.  
  21.     cout << "Введите цифру для поиска: ";
  22.     cin >> digit;
  23.  
  24.     ifstream file(path);
  25.  
  26.     if (!file.is_open()) {
  27.         cout << "Ошибка открытия файла" << endl;
  28.         system("pause");
  29.         exit(0);
  30.     }
  31.  
  32.     cout << endl;
  33.     for (int i = 0; !file.eof(); i++) {
  34.         getline(file, line);
  35.  
  36.         cout << i + 1 << ")\t" << line;
  37.         if (line[line.length() - 1] == digit) {
  38.             cout << "*";
  39.             counter++;
  40.         }
  41.         cout << endl;
  42.     }
  43.  
  44.     cout << endl << "Чисел, заканчивающихся на цифру " << digit << ": " << counter << " шт." << endl;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment