Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- int choiceCheck() {
- int choice;
- bool isIncorrect;
- do {
- isIncorrect = true;
- cin >> choice;
- if (cin.fail() || (cin.get() != '\n')) {
- cout << "Error! Input a number" << endl;
- cin.clear();
- while (cin.get() != '\n');
- } else {
- cin.clear();
- if (choice > 0 && choice < 3) {
- isIncorrect = false;
- } else {
- cout << "Error! Input 1 or 2" << endl;
- }
- }
- } while (isIncorrect);
- return choice;
- }
- int inputCheck() {
- bool isIncorrect;
- int n;
- cout << "Input n" << endl;
- do {
- isIncorrect = true;
- cin >> n;
- if (cin.fail() || (cin.get() != '\n')) {
- cout << "Error! Input a number" << endl;
- cin.clear();
- while (cin.get() != '\n');
- } else {
- cin.clear();
- if (n > 1) {
- isIncorrect = false;
- } else {
- cout << "Error! Input a number greater than 1" << endl;
- }
- }
- } while (isIncorrect);
- return n;
- }
- int *inputCheckArray(int n, int *arr) {
- int i;
- bool isIncorrect;
- for (i = 0; i < n; i++) {
- do {
- isIncorrect = true;
- cin >> arr[i];
- if (cin.fail() || (cin.get() != '\n')) {
- cout << "Error! Input a number" << endl;
- cin.clear();
- while (cin.get() != '\n');
- } else {
- isIncorrect = false;
- }
- } while (isIncorrect);
- }
- return arr;
- }
- string checkInputFilePath() {
- string path;
- bool isInCorrect;
- do {
- cout << "Input path to the file:\n";
- isInCorrect = false;
- cin >> path;
- ifstream fin(path);
- if (!fin.is_open()) {
- cout << "Could not open the file" << endl;
- isInCorrect = true;
- } else {
- fin.close();
- }
- } while (isInCorrect);
- return path;
- }
- int *fileCheckArray(int &n, int *arr) {
- string path;
- bool isIncorrect;
- int i;
- do {
- path = checkInputFilePath();
- ifstream fin(path);
- isIncorrect = false;
- fin >> n;
- if (fin.fail()) {
- cout << "The data is incorrect" << endl;
- isIncorrect = true;
- }
- if (!isIncorrect && (n < 2)) {
- cout << "The data is incorrect" << endl;
- isIncorrect = true;
- }
- if (!isIncorrect) {
- arr = new int [n];
- for (i = 0; i < n; i++) {
- fin >> arr[i];
- if (fin.fail()) {
- cout << "The data is incorrect" << endl;
- isIncorrect = true;
- }
- }
- if (!fin.eof()) {
- cout << "The data is incorrect" << endl;
- isIncorrect = true;
- }
- }
- fin.close();
- } while (isIncorrect);
- return arr;
- }
- int *inputChoice(int &n, int *arr) {
- int choice;
- cout << "Choose input option:\n1.Input through console\n2.Input through file" << endl;
- choice = choiceCheck();
- if (choice == 1) {
- n = inputCheck();
- arr = new int[n];
- arr = inputCheckArray(n, arr);
- } else {
- arr = fileCheckArray(n, arr);
- }
- return arr;
- }
- int *sum(int n, int *arr, int *sumArr) {
- int i;
- for (i = 0; i < n; i++) {
- sumArr[i] = arr[i] + arr[i + 1];
- }
- return sumArr;
- }
- int findMax(int n, int *sumArr) {
- int max, i;
- max = sumArr[0];
- for (i = 0; i < n; i++) {
- if (sumArr[i] > max) {
- max = sumArr[i];
- }
- }
- return max;
- }
- string checkOutputFilePath() {
- string path;
- bool isIncorrect;
- cout
- << "Input file path and the name of the file for\nexample С:\\Projects\\Number\\FileName.txt. If the\nfile does not exist, then it will be created\nautomatically in the root folder of the program:\n";
- isIncorrect = false;
- cin >> path;
- ifstream fin(path);
- if (!fin.is_open()) {
- cout << "Could not open the file or it does not exist" << endl;
- isIncorrect = true;
- } else {
- fin.close();
- }
- if (isIncorrect) {
- cout << "File will be created in the root folder of the program\n";
- path = "Result.txt";
- }
- return path;
- }
- void outputMax(int max) {
- cout << max << endl;
- }
- void outputFile(int max) {
- string path;
- path = checkOutputFilePath();
- ofstream fout(path);
- fout << max << endl;
- fout.close();
- }
- void outputChoice(int &max) {
- int choice;
- cout << "Choose output option:\n1.Output through console\n2.Output through file" << endl;
- choice = choiceCheck();
- if (choice == 1) {
- outputMax(max);
- } else {
- outputFile(max);
- }
- }
- int main() {
- int n, max;
- int *arr;
- int *sumArr;
- arr = inputChoice(n, arr);
- n--;
- sumArr = new int[n];
- sumArr = sum(n, arr, sumArr);
- max = findMax(n, sumArr);
- outputChoice(max);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement