Tomki

Lab5Lingvo

Apr 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. // ArithmeticExpr.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. char signs[] = { '*', '/', '+', '-', '=' };
  12.  
  13. bool isOperator(char symb)
  14. {
  15.     if (symb == '+' || symb == '-' || symb == '*' || symb == '/' || symb == '(' || symb == ')')
  16.         return true;
  17.     else
  18.         return false;
  19. }
  20. void reverse(string& input)
  21. {
  22.     string output;
  23.     for (int i = input.length() - 1; i > -1; i--)
  24.         output = output + input[i];
  25.     input = output;
  26. }
  27. int getPrcd(char symb)
  28. {
  29.     if (symb == '*' || symb == '/')
  30.         return 4;
  31.     if (symb == '+' || symb == '-')
  32.         return 2;
  33.     if (symb == '(' || symb == ')' || symb == '#')
  34.         return 1;
  35. }
  36. void infixToPrefix(string infix, string& prefix)
  37. {
  38.     int i, j = 0;
  39.     char symb;
  40.     vector<char> opArray;
  41.     opArray.push_back('#');
  42.     reverse(infix);
  43.     for (i = 0; i < infix.length(); i++)
  44.     {
  45.         symb = infix[i];
  46.         if (isOperator(symb) == 0)
  47.         {
  48.             prefix = prefix + symb;
  49.             j++;
  50.         }
  51.         else
  52.         {
  53.             if (symb == ')')
  54.             {
  55.                 opArray.push_back(symb);
  56.             }
  57.             else if (symb == '(')
  58.             {
  59.                 while (opArray.back() != ')')
  60.                 {
  61.                     prefix = prefix + opArray.back();
  62.                     opArray.pop_back();
  63.                     j++;
  64.                 }
  65.                 opArray.pop_back();
  66.             }
  67.             else
  68.             {
  69.                 if (getPrcd(opArray.back()) <= getPrcd(symb))
  70.                 {
  71.                     opArray.push_back(symb);
  72.                 }
  73.                 else
  74.                 {
  75.                     while (getPrcd(opArray.back()) >= getPrcd(symb))
  76.                     {
  77.                         prefix = prefix + opArray.back();
  78.                         opArray.pop_back();
  79.                         j++;
  80.                     }
  81.                     opArray.push_back(symb);
  82.                 }
  83.             }
  84.         }
  85.     }
  86.     while (opArray.back() != '#')
  87.     {
  88.         prefix = prefix + opArray.back();
  89.         opArray.pop_back();
  90.         j++;
  91.     }
  92. }
  93. void genCode(string txt)
  94. {
  95.     string code;
  96.     string leftop;
  97.     string rightop;
  98.     string curCode;
  99.     string op;
  100.     int i = 0;
  101.     while (txt[i] != '\0')
  102.     {
  103.         if (txt[i] == '=')
  104.         {
  105.             code = txt[i + 1];
  106.             code = "mov " + code + ",ax\n";
  107.         }
  108.         else if (isOperator(txt[i]))
  109.         {
  110.             if (!isOperator(txt[i + 1]) && !isOperator(txt[i + 2]))
  111.             {
  112.                 leftop = txt[i + 1];
  113.                 rightop = txt[i + 2];
  114.                 switch (txt[i])
  115.                 {
  116.                 case '+': op = "add "; break;
  117.                 case '-': op = "sub "; break;
  118.                 case '*': op = "mul "; break;
  119.                 case '/': op = "div "; break;
  120.                 default:
  121.                     break;
  122.                 }
  123.                 curCode = "mov eax, " + leftop + "\n" + op + "eax, " + rightop;
  124.                 code = curCode + "\n" + code;
  125.             }
  126.             if (isOperator(txt[i + 1]) && !isOperator(txt[i + 2]))
  127.             {
  128.  
  129.             }
  130.         }
  131.     }
  132.     cout << code;
  133. }
  134.  
  135. int main()
  136. {
  137.     string input;
  138.     string output;
  139.     cin >> input;
  140.     infixToPrefix(input, output);
  141.     reverse(output);
  142.     output = "=A" + output;
  143.     cout << output << endl;
  144.     genCode(output);
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment