Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <map>
  5. #include <vector>
  6. #include <cstring>
  7. #include <sstream>
  8. using namespace std;
  9. map<char, string> T;
  10. vector<string> result;
  11. bool isOperation(char c)
  12. {
  13.     return c == '-' || c == '+' || c == '*' || c == '/';
  14. }
  15. //шаг 1, если операнд переменная из таблицы Т то заменить на соответствующее значение
  16. string Case1(string str)
  17. {
  18.     if (isOperation(str[0]))
  19.     {  
  20.         string tstr;
  21.         tstr += str.substr(0, 2);
  22.         int i = 2;
  23.         if (isalpha(str[i]))
  24.         {
  25.             if (!T[str[i]].empty()) tstr += T[str[i]];
  26.             i++;
  27.         }
  28.         else
  29.         {
  30.             while (str[i] != ',')
  31.             {
  32.                 tstr += str[i++];
  33.             }
  34.         }
  35.         tstr += ',';
  36.         i++;
  37.         if (isalpha(str[i]))
  38.         {
  39.             if (!T[str[i]].empty()) tstr += T[str[i]];
  40.         }
  41.         else
  42.         {
  43.            
  44.             while (str[i] != ')')
  45.             {
  46.                 tstr += str[i++];
  47.             }
  48.         }
  49.         tstr += ')';
  50.         return tstr;
  51.     }
  52.     return str;
  53. }
  54. //шаг 2, если операнд ссылка на с(к,0)
  55. string Case2(string str)
  56. {
  57.     int ref1 = 0;
  58.     string tstr;
  59.     tstr += str.substr(0, 2);
  60.     int i = 2;
  61.     if (str[i] == '^')
  62.     {
  63.         i++;
  64.         while (str[i] != ',')
  65.         {
  66.             ref1 = ref1 * 10 + (str[i++] - '0');
  67.         }
  68.         i++;
  69.         if (isdigit(result[ref1 - 1][0]))
  70.         {
  71.             tstr += result[ref1 - 1];
  72.         }
  73.         else
  74.         {
  75.             tstr += '^';
  76.             stringstream ss;
  77.             ss << ref1;
  78.             tstr += ss.str();
  79.         }
  80.         tstr += ',';
  81.     }
  82.     else
  83.     {
  84.         while (str[i] != ',')
  85.         {
  86.             tstr += str[i++];
  87.         }
  88.         tstr += ',';
  89.         i++;
  90.     }
  91.     if (str[i] == '^')
  92.     {
  93.         i++;
  94.         ref1 = 0;
  95.         while (str[i] != ')')
  96.         {
  97.             ref1 = ref1 * 10 + (str[i++] - '0');
  98.         }
  99.         i++;
  100.         if (isdigit(result[ref1 - 1][0]))
  101.         {
  102.             tstr += result[ref1 - 1];
  103.         }
  104.         else
  105.         {
  106.             tstr += '^';
  107.             stringstream ss;
  108.             ss << ref1;
  109.             tstr += ss.str();
  110.         }
  111.         tstr += ')';
  112.     }
  113.     else
  114.     {
  115.         while (str[i] != ')')
  116.         {
  117.             tstr += str[i++];
  118.         }
  119.         tstr += ')';
  120.     }
  121.     return tstr;
  122. }
  123. //если все операнды триады константы - то сворачиваем ее
  124. string Case3(string str)
  125. {
  126.     string tstr;
  127.     int i = 2;
  128.     int ref1 = 0;
  129.     int ref2 = 0;
  130.     if (isdigit(str[i]))
  131.     {
  132.        
  133.         while (str[i] != ',')
  134.         {
  135.             ref1 = ref1 * 10 + (str[i++] - '0');
  136.         }
  137.         i++;
  138.     }
  139.     else
  140.         return str;
  141.  
  142.     if (isdigit(str[i]))
  143.     {
  144.         while (str[i] != ')')
  145.         {
  146.             ref2 = ref2 * 10 + (str[i++] - '0');
  147.         }
  148.         stringstream ss;
  149.         switch (str[0])
  150.         {
  151.         case '+':
  152.             ref1 = ref1 + ref2;
  153.             ss << ref1;
  154.             tstr += ss.str();
  155.             break;
  156.         case '-':
  157.             ref1 = ref1 - ref2;
  158.             ss << ref1;
  159.             tstr += ss.str();
  160.             break;
  161.         case '*':
  162.             ref1 = ref1 * ref2;
  163.             ss << ref1;
  164.             tstr += ss.str();
  165.             break;
  166.         case '/':
  167.             ref1 = ref1 / ref2;
  168.             ss << ref1;
  169.             tstr += ss.str();
  170.             break;
  171.         }
  172.         return tstr;
  173.     }
  174.     else
  175.         return str;
  176. }
  177. //если триада присваивание =(a,b)
  178. //a) если b константа, то заносим пару <a,b> в таблицу T
  179. //b) если b не константа, пара со старым значением <a,...> удаляется из табицы Т
  180. void Case4(string str)
  181. {
  182.     char variable;
  183.     string res;
  184.     if (str[0] == '=')
  185.     {
  186.         variable = str[2];
  187.         int i = 4;
  188.         if (isdigit(str[4]))
  189.         {
  190.             while (str[i] != ')')
  191.             {
  192.                 res += str[i++];
  193.             }
  194.             T[variable] = res;
  195.         }
  196.         else
  197.             if (!T[variable].empty())
  198.                     T[variable].clear();
  199.     }
  200. }
  201. int main()
  202. {
  203.     ifstream in;
  204.     ofstream out;
  205.    
  206.     in.open("input.txt");
  207.  
  208.     string S;
  209.    
  210.     while (getline(in, S))
  211.     {
  212.         cout << "T" << endl;
  213.         for (map<char,string>::iterator i = T.begin(); i != T.end(); i++)
  214.         {
  215.             cout << "<" << (*i).first << ":" << (*i).second << endl;
  216.         }
  217.         cout << ";" << endl;
  218.         cout << S << endl;
  219.         //шаг 1, если операнд переменная из таблицы Т то заменить на соответствующее значение
  220.         S = Case1(S);
  221.         cout << S << endl;
  222.         S = Case2(S);
  223.         cout << S << endl;
  224.         S = Case3(S);
  225.         cout << S << endl;
  226.         Case4(S);
  227.         cout << S << endl;
  228.         result.push_back(S);
  229.     }
  230.     cout << "end" << endl;
  231.     in.close();
  232.     for (int i = 0; i < result.size(); i++) cout << result[i] << endl;
  233.     return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement