The suffix expression implements the six operation

Source: Internet
Author: User
Tags bool

The class that implements the suffix expression evaluates to a suffix expression that has already been processed.

#include <iostream> #include <stack> #include <string> using namespace std;
	void Stringdevide (String str,int &num,string st1[]) {for (int i=0;i<100;i++) st1[i][0]= ' + ';
	int n=str.size ();
	int j=0,count=0;
		for (int i=0;i<n;i++) {if (str[i]!= ') {st1[count].push_back (str[i]);
		} else {count++;
}} num=count+1;
	} void Stringtonum (String str,int &num) {num=0;
	int n=str.size ();
		for (int i=0;i<n;i++) {num=num*10;
	num+=str[i]-' 0 ';
	}} class Intertreecomputer {private://Expression to evaluate string m_expresion;

Store the numbers in the stack stack<int> m_num; Public:intertreecomputer (string expression): m_expresion (expression) {}//Determines whether an operator is an operator bool Isoperator (char ch) const
	;
	Gets the two operands to be computed void getoperands (int &left,int &right);
	The two numbers obtained are calculated according to the symbol CH (int computer (int left,int Right,char ch) const;
	Gets an expression string getpostoperation () const;
	void Setpostoperator ();
Evaluates an expression and returns the result int Evaluate ();
}; BOOL Intertreecomputer::isoperator (CHAR ch) const {switch (CH) {case ' + ': Case '-': Case ' * ': Case '/': Case '% ': case ' ^ ': return 1;
	Default:return 0; }} void Intertreecomputer::getoperands (int &left,int &right) {if (M_num.empty ()) {cout<< "num stack is
		Empty! ";
	return;
	} right=m_num.top ();
	M_num.pop (); if (M_num.empty ()) {cout<< "The expression is wrong!"
		<<endl;
	return;
	} left=m_num.top ();
M_num.pop ();
		} int intertreecomputer::computer (int left,int Right,char ch) const {switch (CH) {case ' + ': return left+right;
	Break
		Case '-': return left-right;
	Break
		Case ' * ': return left*right;
	Break
			Case '/': if (right==0) {cout<< "The expression is wrong" <<endl;
		return-1;
		} return left/right;
	Break
		Case '% ': return left%right;
	Break
			Case ' ^ ': if (left==0&&right==0) {cout<< "The expression is wrong" <<endl;
		return-1;
		} int value=1;
			while (right>0) {value*=left;
		right--;} return value;
	Break
}} string Intertreecomputer::getpostoperation () const {return m_expresion;} void Intertreecomputer::setpostoperator ()
	{} int intertreecomputer::evaluate () {string *str=new string[100];
	int num;
	Stringdevide (M_EXPRESION,NUM,STR); for (int i=0;i<num;i++) {if (str[i][0]== ' + ' | | | str[i][0]== '-' | | str[i][0]== ' * ' | | str[i][0]== '/' | | str[i][0]== '% ' | |
			str[i][0]== ' ^ ') {char ch=str[i][0];
			int left,right;
			Getoperands (Left,right);
			int Number=computer (LEFT,RIGHT,CH);
		M_num.push (number);
			} else {int numb=0;
			Stringtonum (STR[I],NUMB);
		M_num.push (numb);
}} return M_num.top (); }


The CPP files tested are as follows:

#include <iostream>
using namespace std;
#include <string>
#include <stack>
#include "InterTreeComputer.h"
int main ()
{
	String Str= "2 5 + 3 * 8 3/-";
	String st1= "2 3 ^ 1 +";
	String St2= "2 2 3 ^ ^ 4/";
	Intertreecomputer Comp (ST2);
	Cout<<comp.getpostoperation () <<endl;
	Cout<<comp.evaluate () <<endl;
	return 0;
}


What is a suffix expression.
Let's first say the ordinary formula 1+2*3-4. Here, the operator +,-, *,/is in the middle of the expression is called infix expression. Infix expression, there is a special feature is that sometimes, you need parentheses to indicate priority.
The suffix expression is a common expression in the computer, also known as the inverse Polish expression (because the Polish expression is a prefix expression, hehe). Because he doesn't have parentheses, it's easier to calculate the expression. For example, the suffix expression for 1+2*3-4 is 1 2 3 * + 4-. His essence is that there is a stack, first pressed into three number 1 2 3, and then found the next is *, the stack of the first two out to do multiplication, that is, 2*3, the result is 6, in the stack is pressed into. And so on.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.