Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <map>
- #include <vector>
- #include <cstring>
- #include <sstream>
- using namespace std;
- map<char, string> T;
- vector<string> result;
- bool isOperation(char c)
- {
- return c == '-' || c == '+' || c == '*' || c == '/';
- }
- //шаг 1, если операнд переменная из таблицы Т то заменить на соответствующее значение
- string Case1(string str)
- {
- if (isOperation(str[0]))
- {
- string tstr;
- tstr += str.substr(0, 2);
- int i = 2;
- if (isalpha(str[i]))
- {
- if (!T[str[i]].empty()) tstr += T[str[i]];
- i++;
- }
- else
- {
- while (str[i] != ',')
- {
- tstr += str[i++];
- }
- }
- tstr += ',';
- i++;
- if (isalpha(str[i]))
- {
- if (!T[str[i]].empty()) tstr += T[str[i]];
- }
- else
- {
- while (str[i] != ')')
- {
- tstr += str[i++];
- }
- }
- tstr += ')';
- return tstr;
- }
- return str;
- }
- //шаг 2, если операнд ссылка на с(к,0)
- string Case2(string str)
- {
- int ref1 = 0;
- string tstr;
- tstr += str.substr(0, 2);
- int i = 2;
- if (str[i] == '^')
- {
- i++;
- while (str[i] != ',')
- {
- ref1 = ref1 * 10 + (str[i++] - '0');
- }
- i++;
- if (isdigit(result[ref1 - 1][0]))
- {
- tstr += result[ref1 - 1];
- }
- else
- {
- tstr += '^';
- stringstream ss;
- ss << ref1;
- tstr += ss.str();
- }
- tstr += ',';
- }
- else
- {
- while (str[i] != ',')
- {
- tstr += str[i++];
- }
- tstr += ',';
- i++;
- }
- if (str[i] == '^')
- {
- i++;
- ref1 = 0;
- while (str[i] != ')')
- {
- ref1 = ref1 * 10 + (str[i++] - '0');
- }
- i++;
- if (isdigit(result[ref1 - 1][0]))
- {
- tstr += result[ref1 - 1];
- }
- else
- {
- tstr += '^';
- stringstream ss;
- ss << ref1;
- tstr += ss.str();
- }
- tstr += ')';
- }
- else
- {
- while (str[i] != ')')
- {
- tstr += str[i++];
- }
- tstr += ')';
- }
- return tstr;
- }
- //если все операнды триады константы - то сворачиваем ее
- string Case3(string str)
- {
- string tstr;
- int i = 2;
- int ref1 = 0;
- int ref2 = 0;
- if (isdigit(str[i]))
- {
- while (str[i] != ',')
- {
- ref1 = ref1 * 10 + (str[i++] - '0');
- }
- i++;
- }
- else
- return str;
- if (isdigit(str[i]))
- {
- while (str[i] != ')')
- {
- ref2 = ref2 * 10 + (str[i++] - '0');
- }
- stringstream ss;
- switch (str[0])
- {
- case '+':
- ref1 = ref1 + ref2;
- ss << ref1;
- tstr += ss.str();
- break;
- case '-':
- ref1 = ref1 - ref2;
- ss << ref1;
- tstr += ss.str();
- break;
- case '*':
- ref1 = ref1 * ref2;
- ss << ref1;
- tstr += ss.str();
- break;
- case '/':
- ref1 = ref1 / ref2;
- ss << ref1;
- tstr += ss.str();
- break;
- }
- return tstr;
- }
- else
- return str;
- }
- //если триада присваивание =(a,b)
- //a) если b константа, то заносим пару <a,b> в таблицу T
- //b) если b не константа, пара со старым значением <a,...> удаляется из табицы Т
- void Case4(string str)
- {
- char variable;
- string res;
- if (str[0] == '=')
- {
- variable = str[2];
- int i = 4;
- if (isdigit(str[4]))
- {
- while (str[i] != ')')
- {
- res += str[i++];
- }
- T[variable] = res;
- }
- else
- if (!T[variable].empty())
- T[variable].clear();
- }
- }
- int main()
- {
- ifstream in;
- ofstream out;
- in.open("input.txt");
- string S;
- while (getline(in, S))
- {
- cout << "T" << endl;
- for (map<char,string>::iterator i = T.begin(); i != T.end(); i++)
- {
- cout << "<" << (*i).first << ":" << (*i).second << endl;
- }
- cout << ";" << endl;
- cout << S << endl;
- //шаг 1, если операнд переменная из таблицы Т то заменить на соответствующее значение
- S = Case1(S);
- cout << S << endl;
- S = Case2(S);
- cout << S << endl;
- S = Case3(S);
- cout << S << endl;
- Case4(S);
- cout << S << endl;
- result.push_back(S);
- }
- cout << "end" << endl;
- in.close();
- for (int i = 0; i < result.size(); i++) cout << result[i] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement