Advertisement
sneyzi

Untitled

Jun 7th, 2021
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     srand(time(NULL));
  10.     int m, n;
  11.     int x, v = 1;
  12.     int max;
  13.  
  14.     while (v) {
  15.         cout << "MENU\n1 - write data to file\n2 - read file and calculate\n";
  16.         cin >> x;
  17.         switch (x) {
  18.         case 1: {
  19.             cout << "input number\n";
  20.             cin >> n;
  21.             ofstream fi("file.txt");
  22.  
  23.             for (int i = 0; i < n; i++) {
  24.                 m = rand() % 51 - 25;
  25.                 cout << m << '\t';
  26.                 fi << m << '\t';
  27.             }
  28.  
  29.             fi.close();
  30.  
  31.             cout << endl;
  32.             break;
  33.         }
  34.         case 2: {
  35.             int s = 0, p;
  36.             ifstream fo("file.txt");
  37.             while (!fo.eof()) {
  38.                 fo >> p;
  39.                 if (!fo.eof()) {
  40.                     if (p % 2 != 0) {
  41.                         max = p;
  42.                         if (max < p) {
  43.                             max = p;
  44.                         }
  45.  
  46.                     }
  47.                 }
  48.             }
  49.  
  50.             fo.close();
  51.             cout << "Maksimalnij element sered neparnih " << max << endl;
  52.             break;
  53.         }
  54.         default: v = 0;
  55.             break;
  56.         }
  57.     }
  58.  
  59.     system("pause");
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement