Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // massiv.cpp: определяет точку входа для консольного приложения.
- //
- #include <iostream>
- #include <fstream>
- using namespace std;
- int solution(int[], int);
- int main()
- {
- const char *arr[5] = { "data1.txt", "data2.txt", "data3.txt", "data4.txt", "data5.txt" };
- int f = 1;
- while (f)
- {
- cout << "Choose file: " << endl;
- int count = sizeof(arr) / sizeof(char*);
- for (int i = 0; i < count; i++)
- {
- cout << i+1 << ". " << arr[i] << endl;
- }
- int num = -1;
- while (num < 1 || num > count)
- {
- cout << "Enter number >> ";
- cin >> num;
- }
- fstream fin = fstream(arr[num - 1]);
- if (!fin)
- {
- cout << "Could not open file" << endl;
- continue;
- }
- int n = 0;
- fin >> n;
- if (!n)
- {
- fin.close(); // закрыть файл с данными
- cout << "Error in file format" << endl;
- continue;
- }
- int* a = new int[n];
- int k = 0;
- cout << "Array elements:" << endl;
- while (!fin.eof() && k < n)
- {
- fin >> a[k];
- cout << a[k++] << " ";
- }
- cout << endl;
- fin.close();
- if (k < n)
- {
- cout << "More elements declared than found: declared " << n << ", found " << k << endl;
- continue;
- }
- int res = solution(a, n);
- switch (res)
- {
- case 1:
- cout << "Sequence is ascending";
- break;
- case 0:
- cout << "Sequence is not ascending";
- break;
- case -1:
- cout << "Less than 2 elements";
- break;
- }
- fin.close(); // закрыть файл с данными
- cout << endl;
- cout << "Type 0 if you want to exit, 1 if you want to run program again >> ";
- cin >> f;
- }
- return 0;
- }
- int solution(int b[], int m)
- {
- if (m < 2)
- return -1;
- bool asc = true;
- int prev = b[0];
- for (int i = 1; i < m; i++)
- {
- if (prev > b[i])
- asc = false;
- prev = b[i];
- }
- return asc;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement