Advertisement
Egor_Vakar

lab2_4(C++)

Oct 19th, 2021 (edited)
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. string takeInPath() {
  2.     string path;
  3.     bool isIncorrect;
  4.     cout << "Enter file path: ";
  5.     do {
  6.         isIncorrect = true;
  7.         cin >> path;
  8.         ifstream fin(path);
  9.         if (fin.is_open()) {
  10.             isIncorrect = false;
  11.             fin.close();
  12.         }
  13.         else {
  14.             cout << "File is not found\nEnter file path: ";
  15.         }
  16.         if (!isIncorrect && ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt")))) {
  17.             cout << "File is found, but it is not \".txt\" type file\nEnter file path : ";
  18.             isIncorrect = true;
  19.         }
  20.     } while (isIncorrect);
  21.     return path;
  22. }
  23.  
  24. string takeOutPath() {
  25.     string path;
  26.     bool isIncorrect;
  27.     cout << "Enter file path: ";
  28.     do
  29.     {
  30.         isIncorrect = false;
  31.         cin >> path;
  32.         if ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt"))) {
  33.             cout << "It should be a \".txt\" file\nEnter file path: ";
  34.             isIncorrect = true;
  35.         }
  36.     } while (isIncorrect);
  37.     return path;
  38. }
  39.  
  40. int takeSize() {
  41.     bool isIncorrect;
  42.     int size = 0;
  43.     cout << "Enter sequence's size: ";
  44.     do
  45.     {
  46.         isIncorrect = false;
  47.         cin >> size;
  48.         if (!cin.good())
  49.         {
  50.             isIncorrect = true;
  51.             cout << "Incorrect input!!!\nEnter size: ";
  52.             while (cin.get() != '\n') {
  53.                 cin.clear();
  54.             }
  55.         }
  56.         if ((!isIncorrect) && (size < 1))
  57.         {
  58.             isIncorrect = true;
  59.             cout << "The size must be higher than 0\nEnter size: ";
  60.         }
  61.     } while (isIncorrect);
  62.     return size;
  63. }
  64.  
  65. double* takeSequenceFromConsole(const int size) {
  66.     bool isIncorrect;
  67.     double *sequence = new double[size];
  68.     for (int i = 0; i < size; i++){
  69.         cout << "Enter element " << (i + 1) << ": ";
  70.         do {
  71.             isIncorrect = false;
  72.             cin >> sequence[i];
  73.             if (!cin.good())
  74.             {
  75.                 isIncorrect = true;
  76.                 cout << "Incorrect input!!!\nEnter element " << (i + 1) << ": ";
  77.                 while (cin.get() != '\n') {
  78.                     cin.clear();
  79.                 }
  80.             }
  81.         } while (isIncorrect);
  82.     }
  83.     return sequence;
  84. }
  85.  
  86. double* takeSequenceFromFile(const string path,const int size) {
  87.     ifstream fin(path);
  88.     double* sequence = new double[size];
  89.     string sizeLine;
  90.     fin >> sizeLine;
  91.     for (int i = 0; i < size; i++) {
  92.         fin >> sequence[i];
  93.     }
  94.     fin.close();
  95.     return sequence;
  96. }
  97.  
  98. double* takeSequence(const string source,const int size) {
  99.     string inPath;
  100.     double* sequence;
  101.     if (source == "1") {
  102.         sequence = takeSequenceFromConsole(size);
  103.     }
  104.     else {
  105.         inPath = takeInPath();
  106.         sequence = takeSequenceFromFile(inPath, size);
  107.     }
  108.     return sequence;
  109. }
  110.  
  111. string findAnswer(const int size,const double* sequence) {
  112.     int i = 0;
  113.     bool isCorrect;
  114.     do {
  115.         isCorrect = (sequence[i] >= sequence[i + 1]);
  116.         i++;
  117.     } while ((isCorrect) && (i < size - 1));
  118.     if (isCorrect) {
  119.         return "The sequence is non-growing";
  120.     }
  121.     else {
  122.         return "The sequence isn't non-growing";
  123.     }
  124. }
  125.  
  126. void outputToFile(const string path,const string answer) {
  127.     ofstream fout;
  128.     fout.open(path);
  129.     fout << answer;
  130.     fout.close();
  131.     cout << "Comlete!";
  132. }
  133.  
  134. void output(const string source,const string answer) {
  135.     string outPath;
  136.  
  137.     if (source == "1") {
  138.         cout << answer;
  139.     }
  140.     else {
  141.         outPath = takeOutPath();
  142.         outputToFile(outPath, answer);
  143.     }
  144. }
  145.  
  146. int main() {
  147.     cout << "Welcome to the program that checks is a given sequence of numbers  non-growing\n";
  148.     int size = takeSize();
  149.     string inputSource = takeSource("entering the sequence of numbers");
  150.     double* sequence = takeSequence(inputSource, size);
  151.     string answer = findAnswer(size, sequence);
  152.     string outputSource = takeSource("output the answer");
  153.     output(outputSource, answer);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement