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;
- }
- 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;
- }
- string inputCheck() {
- bool isIncorrect;
- string str;
- int i;
- do {
- cout
- << "Input the string consisting of numbers, letters of the English alphabet and symbols '-', '+', '.' "
- << endl;
- isIncorrect = false;
- cin >> str;
- for (i = 0; i < str.length(); i++) {
- if (!(str[i] == 43 || str[i] == 45 || str[i] == 46 || (str[i] > 47 && str[i] < 58) ||
- (str[i] > 64 && str[i] < 91) || (str[i] > 96 && str[i] < 123))) {
- cout << "Incorrect string format\n";
- isIncorrect = true;
- }
- }
- } while (isIncorrect);
- return str;
- }
- string fileCheckString() {
- string path, str;
- bool isIncorrect;
- int i;
- do {
- path = checkInputFilePath();
- ifstream fin(path);
- isIncorrect = false;
- fin >> str;
- if (fin.fail()) {
- cout << "The data is incorrect" << endl;
- isIncorrect = true;
- }
- if (!isIncorrect && (str.length() < 1)) {
- cout << "The string is empty" << endl;
- isIncorrect = true;
- }
- if (!fin.eof()) {
- cout << "There should be only one string" << endl;
- isIncorrect = true;
- }
- if (!isIncorrect) {
- for (i = 0; i < str.length(); i++) {
- if (!((str[i] == 43 || str[i] == 45 || str[i] == 46 || (str[i] > 47 && str[i] < 58) ||
- (str[i] > 64 && str[i] < 91) || (str[i] > 96 && str[i] < 123)))) {
- isIncorrect = true;
- }
- }
- }
- if (isIncorrect) {
- cout << "Incorrect string format" << endl;
- }
- fin.close();
- } while (isIncorrect);
- return str;
- }
- string inputChoice() {
- int choice;
- string str;
- cout << "Choose input option:\n1.Input through console\n2.Input through file" << endl;
- choice = choiceCheck();
- if (choice == 1) {
- str = inputCheck();
- } else {
- str = fileCheckString();
- }
- return str;
- }
- string checkOutputFilePath() {
- string path;
- bool isIncorrect;
- cout
- << "Input file path and the name of the file for\nexample C:\\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 findConsoleSubStr(string str) {
- string subStr;
- int i;
- bool isFound;
- isFound = false;
- subStr = "";
- for (i = 0; i < str.length(); i++) {
- if (str[i] == '+' || str[i] == '-') {
- if (subStr.length() > 1) {
- cout << subStr << endl;
- } else {
- if (i>1) && (str[i - 1] == '0') {
- cout << "0" << endl;
- }
- }
- subStr = "";
- subStr += str[i];
- isFound = true;
- } else {
- if (str[i] == 46 || (str[i] > 64 && str[i] < 91) || (str[i] > 96 && str[i] < 123)) {
- isFound = false;
- if (subStr.length() > 1) {
- cout << subStr << endl;
- } else {
- if (i>1) && (str[i - 1] == '0') {
- cout << "0" << endl;
- }
- }
- subStr = "";
- } else {
- if (str[i] == 48) {
- if (isFound && !(subStr[subStr.length() - 1] == '+' || subStr[subStr.length() - 1] == '-')) {
- subStr += str[i];
- }
- } else {
- if (str[i] > 48 && str[i] < 58) {
- if (isFound) {
- subStr += str[i];
- }
- }
- }
- }
- }
- }
- if (isFound && subStr.length() > 1) {
- cout << subStr << endl;
- }
- }
- void findFileSubStr(string str) {
- string subStr, path;
- int i;
- bool isFound;
- isFound = false;
- subStr = "";
- path = checkOutputFilePath();
- ofstream fout(path);
- subStr = "";
- for (i = 0; i < str.length(); i++) {
- if (str[i] == '+' || str[i] == '-') {
- if (subStr.length() > 1) {
- fout << subStr << endl;
- } else {
- if (i>1) && (str[i - 1] == '0') {
- fout << "0" << endl;
- }
- }
- subStr = "";
- subStr += str[i];
- isFound = true;
- } else {
- if (str[i] == 46 || (str[i] > 64 && str[i] < 91) || (str[i] > 96 && str[i] < 123)) {
- isFound = false;
- if (subStr.length() > 1) {
- fout << subStr << endl;
- } else {
- if (i>1) && (str[i - 1] == '0') {
- fout << "0" << endl;
- }
- }
- subStr = "";
- } else {
- if (str[i] == 48) {
- if (isFound && !(subStr[subStr.length() - 1] == '+' || subStr[subStr.length() - 1] == '-')) {
- subStr += str[i];
- }
- } else {
- if (str[i] > 48 && str[i] < 58) {
- if (isFound) {
- subStr += str[i];
- }
- }
- }
- }
- }
- }
- if (isFound && subStr.length() > 1) {
- fout << subStr << endl;
- }
- cout << "Successful output\n";
- }
- void outputChoice(string str) {
- int choice;
- cout << "Choose output option:\n1.Output through console\n2.Output through file" << endl;
- choice = choiceCheck();
- if (choice == 1) {
- findConsoleSubStr(str);
- } else {
- findFileSubStr(str);
- }
- }
- int main() {
- string str;
- str = inputChoice();
- outputChoice(str);
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement