Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- ifstream fin;
- ofstream fout;
- const int SIZE = 6;
- const char brakes [SIZE] = { '[', ']', '{', '}', '(', ')' };
- const int SQUARE_BRACKET = 0;
- const int BRACE = 2;
- const int BRACKET = 4;
- const int* counters = new int[6];
- bool choose() {
- int inputNumber;
- bool isIncorrect;
- const int MIN_NUM = 0;
- const int MAX_NUM = 1;
- do {
- cin >> inputNumber;
- isIncorrect = false;
- if (cin.fail()) {
- isIncorrect = true;
- cout << "Please, enter a number." << endl;
- cin.clear();
- while (cin.get() != '\n');
- }
- if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
- cout << "You are out of input range!" << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- if (inputNumber == 1)
- return true;
- else
- return false;
- }
- // консольный ввод и вывод
- string consoleInputString() {
- string strInput;
- bool isIncorrect;
- do {
- cin >> strInput;
- isIncorrect = false;
- if (cin.fail()) {
- cout << "Please, enter a string." << endl;
- isIncorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- return strInput;
- }
- void countBrackets(int j, int i, char* brakes, int* counters, string str) {
- if (str[i] == brakes[j])
- counters[j]++;
- if ((str[i] == brakes[j + 1]))
- counters[j + 1]++;
- }
- static void loop(char* brakes, int* counters, string str) {
- for (int i = 0; i < str.length(); i++) {
- countBrackets(SQUARE_BRACKET, i, brakes, counters, str);
- if (counters[SQUARE_BRACKET + 1] > counters[SQUARE_BRACKET])
- break;
- countBrackets(BRACE, i, brakes, counters, str);
- if (counters[BRACE + 1] > counters[BRACE])
- break;
- countBrackets(BRACKET, i, brakes, counters, str);
- if (counters[BRACKET + 1] > counters[BRACKET])
- break;
- }
- }
- static void check(int* counters) {
- if ((counters[SQUARE_BRACKET] == counters[SQUARE_BRACKET + 1]) && (counters[BRACE] == counters[BRACE + 1]) && (counters[BRACKET] == counters[BRACKET + 1]))
- cout << "Congratulations! Balance is exist!" << endl;
- else
- cout << "There is no balance there." << endl;
- }
- string inputFilePath() {
- const int EXTENSION_SIZE = 4;
- string path;
- string ext;
- bool isIncorrect;
- do {
- isIncorrect = false;
- cout << "Input path to file: " << endl;
- cin >> path;
- fin.open(path);
- if (path.size() > EXTENSION_SIZE) {
- ext = path.substr(path.size() - EXTENSION_SIZE, EXTENSION_SIZE);
- }
- else {
- cout << "Incorrect file name." << endl;
- isIncorrect = true;
- } if (!isIncorrect && ext != ".txt") {
- cout << "Must have .txt!" << endl;
- isIncorrect = true;
- }
- else if (!isIncorrect && !fin.is_open()) {
- cout << "Wrong way to file." << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- fin.close();
- return path;
- }
- string FileInputString(string path) {
- string strInput;
- bool isIncorrect;
- do {
- isIncorrect = false;
- fin.open(path);
- fin >> strInput;
- if (cin.fail()) {
- isIncorrect = true;
- cout << "Check file" << endl;
- path = inputFilePath();
- }
- } while (isIncorrect);
- return strInput;
- }
- void fileOutput(string path, int * counters) {
- bool isIncorrect;
- fout.open(path);
- do {
- isIncorrect = false;
- try {
- if ((counters[SQUARE_BRACKET] == counters[SQUARE_BRACKET + 1]) && (counters[BRACE] == counters[BRACE + 1]) && (counters[BRACKET] == counters[BRACKET + 1]))
- fout << "Congratulations!Balance is exist!" << endl;
- else
- fout << "There is no balance there." << endl;
- }
- catch (exception e) {
- isIncorrect = true;
- fout << "Mistake of output in file. Input path." << endl;
- path = inputFilePath();
- }
- } while (isIncorrect);
- cout << "Successful output in file." << endl;
- fout.close();
- }
- int main() {
- cout << "Check if the given text has a balance of opening and closing brackets." << endl;
- cout << "Enter type of input." << endl << "1 is console input, 0 is file input." << endl;
- bool chose = choose();
- string strInput;
- int* counters = new int[6];
- char brakes[SIZE] = { '[', ']', '{', '}', '(', ')' };
- string path;
- if (chose) {
- cout << "Input string: " << endl;;
- strInput = consoleInputString();
- }
- else {
- path = inputFilePath();
- strInput = FileInputString(path);
- }
- loop(brakes, counters, strInput);
- cout << "Enter type of input." << endl << "1 is console output, 0 is file output." << endl;
- chose = choose();
- if (chose) {
- check(counters);
- }
- else {
- path = inputFilePath();
- fileOutput(path, counters);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement