Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string takeInPath() {
- string path;
- bool isIncorrect;
- cout << "Enter file path: ";
- do {
- isIncorrect = true;
- cin >> path;
- ifstream fin(path);
- if (fin.is_open()) {
- isIncorrect = false;
- fin.close();
- }
- else {
- cout << "File is not found\nEnter file path: ";
- }
- if (!isIncorrect && ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt")))) {
- cout << "File is found, but it is not \".txt\" type file\nEnter file path : ";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- string takeOutPath() {
- string path;
- bool isIncorrect;
- cout << "Enter file path: ";
- do
- {
- isIncorrect = false;
- cin >> path;
- if ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt"))) {
- cout << "It should be a \".txt\" file\nEnter file path: ";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- int takeSize() {
- bool isIncorrect;
- int size = 0;
- cout << "Enter sequence's size: ";
- do
- {
- isIncorrect = false;
- cin >> size;
- if (!cin.good())
- {
- isIncorrect = true;
- cout << "Incorrect input!!!\nEnter size: ";
- while (cin.get() != '\n') {
- cin.clear();
- }
- }
- if ((!isIncorrect) && (size < 1))
- {
- isIncorrect = true;
- cout << "The size must be higher than 0\nEnter size: ";
- }
- } while (isIncorrect);
- return size;
- }
- double* takeSequenceFromConsole(const int size) {
- bool isIncorrect;
- double *sequence = new double[size];
- for (int i = 0; i < size; i++){
- cout << "Enter element " << (i + 1) << ": ";
- do {
- isIncorrect = false;
- cin >> sequence[i];
- if (!cin.good())
- {
- isIncorrect = true;
- cout << "Incorrect input!!!\nEnter element " << (i + 1) << ": ";
- while (cin.get() != '\n') {
- cin.clear();
- }
- }
- } while (isIncorrect);
- }
- return sequence;
- }
- double* takeSequenceFromFile(const string path,const int size) {
- ifstream fin(path);
- double* sequence = new double[size];
- string sizeLine;
- fin >> sizeLine;
- for (int i = 0; i < size; i++) {
- fin >> sequence[i];
- }
- fin.close();
- return sequence;
- }
- double* takeSequence(const string source,const int size) {
- string inPath;
- double* sequence;
- if (source == "1") {
- sequence = takeSequenceFromConsole(size);
- }
- else {
- inPath = takeInPath();
- sequence = takeSequenceFromFile(inPath, size);
- }
- return sequence;
- }
- string findAnswer(const int size,const double* sequence) {
- int i = 0;
- bool isCorrect;
- do {
- isCorrect = (sequence[i] >= sequence[i + 1]);
- i++;
- } while ((isCorrect) && (i < size - 1));
- if (isCorrect) {
- return "The sequence is non-growing";
- }
- else {
- return "The sequence isn't non-growing";
- }
- }
- void outputToFile(const string path,const string answer) {
- ofstream fout;
- fout.open(path);
- fout << answer;
- fout.close();
- cout << "Comlete!";
- }
- void output(const string source,const string answer) {
- string outPath;
- if (source == "1") {
- cout << answer;
- }
- else {
- outPath = takeOutPath();
- outputToFile(outPath, answer);
- }
- }
- int main() {
- cout << "Welcome to the program that checks is a given sequence of numbers non-growing\n";
- int size = takeSize();
- string inputSource = takeSource("entering the sequence of numbers");
- double* sequence = takeSequence(inputSource, size);
- string answer = findAnswer(size, sequence);
- string outputSource = takeSource("output the answer");
- output(outputSource, answer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement