Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Lev:
- // Laba5.cpp: определяет точку входа для консольного приложения.
- //
- // ArithmeticExpr.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- char signs[] = { '*', '/', '+', '-', '=' };
- bool isOperator(char symb)
- {
- if (symb == '+' || symb == '-' || symb == '*' || symb == '/' || symb == '(' || symb == ')')
- return true;
- else
- return false;
- }
- void reverse(string& input)
- {
- string output;
- for (int i = input.length() - 1; i > -1; i--)
- output = output + input[i];
- input = output;
- }
- int getPrcd(char symb)
- {
- if (symb == '*' || symb == '/')
- return 4;
- if (symb == '+' || symb == '-')
- return 2;
- if (symb == '(' || symb == ')' || symb == '#')
- return 1;
- }
- void infixToPrefix(string infix, string& prefix)
- {
- int i, j = 0;
- char symb;
- vector<char> opArray;
- opArray.push_back('#');
- reverse(infix);
- for (i = 0; i < infix.length(); i++)
- {
- symb = infix[i];
- if (isOperator(symb) == 0)
- {
- prefix = prefix + symb;
- j++;
- }
- else
- {
- if (symb == ')')
- {
- opArray.push_back(symb);
- }
- else if (symb == '(')
- {
- while (opArray.back() != ')')
- {
- prefix = prefix + opArray.back();
- opArray.pop_back();
- j++;
- }
- opArray.pop_back();
- }
- else
- {
- if (getPrcd(opArray.back()) <= getPrcd(symb))
- {
- opArray.push_back(symb);
- }
- else
- {
- while (getPrcd(opArray.back()) >= getPrcd(symb))
- {
- prefix = prefix + opArray.back();
- opArray.pop_back();
- j++;
- }
- opArray.push_back(symb);
- }
- }
- }
- }
- while (opArray.back() != '#')
- {
- prefix = prefix + opArray.back();
- opArray.pop_back();
- j++;
- }
- }
- string txt;
- string getCode3(bool left)
- {
- char firstChar = txt[0];
- txt = txt.substr(1);
- if (isOperator(firstChar)) {
- bool isLeftOp = false;
- bool isRightOp = false;
- if (isOperator(txt[0]))
- isLeftOp = true;
- string res1 = getCode3(true);
- if (isOperator(txt[0]))
- isRightOp = true;
- string res2 = getCode3(false);
- string oper;
- switch (firstChar) {
- case '+':
- oper = "\nADD AX BX";
- break;
- case '-':
- oper = "\nSUB AX BX";
- break;
- case '/':
- oper = "\nDIV AX BX";
- break;
- case '*':
- oper = "\nMUL AX BX" ;
- break;
- }
- if (!left)
- oper = oper + "\nXCHG AX BX";
- if (isLeftOp && isRightOp)
- return res1 + "\nPUSH AX" + res2 + "\nPOP AX" + oper;
- else if (!isLeftOp && isRightOp)
- return res2 + res1 + oper;
- else
- return res1 + res2 + oper;
- }
- else {
- if (left)
- return "\nMOV AX " + string(1, firstChar);
- else
- return "\nMOV BX " + string(1, firstChar);
- }
- }
- int main()
- {
- string input;
- string output;
- cin >> input;
- infixToPrefix(input, output);
- reverse(output);
- cout << output << endl;
- //genCode(output);
- txt = output;
- //getCode2(output);
- cout << getCode3(true) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment