Tomki

Laba5lingvo

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