Advertisement
Egor_Vakar

lab2_3(C++)

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