Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <cctype>
- using namespace std;
- // C:\onlyformydoggers\h.txt
- bool isConst(string str)
- {
- bool checkConst = true;
- if (isdigit(str[0]))
- {
- for (int i = 0; i < str.length() && checkConst; i++)
- if (!(isdigit(str[i]))) checkConst = false;
- if (checkConst) return true;
- else return false;
- }
- else checkConst = false;
- return checkConst;
- }
- bool isVariable(string str)
- {
- if (isConst(str)) return false;
- else return true;
- }
- bool isSymbols(string str)
- {
- return str.find("=") != -1;
- }
- string getString(string &s)
- {
- ifstream workFile;
- string result = "", str = "";
- workFile.open(s);
- while (!workFile.eof())
- {
- getline(workFile, str);
- result += str + " ";
- str = "";
- }
- workFile.close();
- return result += ";";
- }
- enum states { START, CONST, CLEAR, SKIP };
- int main()
- {
- string str = "", resultString = "", word = "", path = "";
- cin >> path;
- str = getString(path);
- states state = START;
- while (!str.empty())
- {
- int k = 1;
- bool loop = true;
- while (loop)
- {
- switch (state)
- {
- case START:
- {
- if (!str.empty() && isSymbols(str) && k > 0)
- {
- k = str.find("=");
- if (k > 0)
- k--;
- else state = SKIP;
- while (isspace(str[k]) && k > 0) k--;
- while (k >= 0 && isalnum(str[k]))
- {
- word += str[k];
- k--;
- }
- if (word.empty()) state = SKIP;
- else
- {
- reverse(word.begin(), word.end());
- if (isVariable(word))
- {
- resultString += word + " = ";
- word = "";
- state = CONST;
- }
- else
- {
- str.erase(0, str.find("=") + 1);
- state = START;
- resultString = "";
- }
- break;
- }
- }
- else state = SKIP;
- break;
- }
- case CONST:
- {
- k = str.find("=");
- k++;
- while (isspace(str[k]) && k < str.length()) k++;
- while (k < str.length() && isalnum(str[k]))
- {
- word += str[k];
- k++;
- }
- if (isConst(word) && !word.empty())
- {
- state = CLEAR;
- resultString += word;
- cout << resultString << "\n";
- str.erase(0, k);
- }
- else state = SKIP;
- }
- break;
- case CLEAR:
- {
- word = "";
- resultString = "";
- state = START;
- }
- break;
- case SKIP:
- {
- word = "";
- resultString = "";
- loop = false;
- if (k < str.length() && isSymbols(str))
- str.erase(0, str.find("=") + 1);
- else str = "";
- state = START;
- }
- break;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement