Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- string getSource(string what) {
- cout << "Select the source for " + what + ":\n1:Console\n2:File\nEnter 1 or 2: ";
- string choice;
- cin >> choice;
- while ((choice != "1") && (choice != "2")) {
- cout << "Incorrect input!!! Select the source for " + what + ":\n1:Console\n2:File\nEnter 1 or 2: ";
- cin >> choice;
- }
- return choice;
- }
- string getInPath() {
- 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 getOutPath() {
- 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 getNumberFromConsole() {
- bool isIncorrect;
- int number = 0;
- cout << "Enter number: ";
- bool isInсorrect;
- do
- {
- isInсorrect = false;
- cin >> number;
- if (!cin.good())
- {
- isInсorrect = true;
- cout << "Incorrect input!!!\nEnter number: ";
- while (cin.get() != '\n') {
- cin.clear();
- }
- }
- if ((!isInсorrect) && (number < 1))
- {
- isInсorrect = true;
- cout << "The number must be more than 0\nEnter number: ";
- }
- } while (isInсorrect);
- return number;
- }
- int getNumberFromFile(string path) {
- ifstream fin(path);
- int number;
- fin >> number;
- if (!fin.eof()) {
- cout << "Incorrect file content!!!\n";
- number = 0;
- }
- else {
- if (number < 1) {
- cout << "Incorrect file content!!!(The number must be more than 0)\n";
- number = 0;
- }
- }
- fin.close();
- return number;
- }
- int getNumber(string source) {
- int number;
- string InPath;
- if (source == "1") {
- number = getNumberFromConsole();
- }
- else {
- InPath = getInPath();
- number = getNumberFromFile(InPath);
- while (number == 0) {
- InPath = getInPath();
- number = getNumberFromFile(InPath);
- }
- }
- return number;
- }
- int getReverse(int number) {
- int reverse = 0;
- while (number > 0) {
- reverse = reverse * 10 + number % 10;
- number /= 10;
- }
- return reverse;
- }
- string getAnswer(int number) {
- string answer;
- if (number == getReverse(number)) {
- answer = "Number is palindrome";
- }
- else {
- answer = "Number isn't palindrome";
- }
- return answer;
- }
- void outputToFile(string path, string answer) {
- ofstream fout;
- fout.open(path);
- fout << answer;
- fout.close();
- cout << "Comlete!";
- }
- void output(string source, string answer) {
- string outPath;
- if (source == "1") {
- cout << answer;
- }
- else {
- outPath = getOutPath();
- outputToFile(outPath, answer);
- }
- }
- int main() {
- cout << "Welcome to the program that checks is a given number a palindrome.\n";
- string inputSource = getSource("entering the number");
- int number = getNumber(inputSource);
- string answer = getAnswer(number);
- string outputSource = getSource("output");
- output(outputSource, answer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement