c + + to achieve the methods and code calculators

Author:Anonymous    Updated:2008-10-17 12:12:10

Once the expression into a suffix and then calculated on the expression of very simple. Symbols on the face of a pop-up from his recent two operands and then proceed to the designated operator of the operation ok on the pressure after the completion of the operation ... Stack continued to scan, followed by repeat ... do not wait until the operator. All operations will be over. ... The results will be out no longer have to determine what priority. What the brackets would also be omitted .. Shuangba.

 

 In fact, in the suffix turn up on time can be calculated expression scanning, the completion of the calculation.

 

 Yesterday, the direct source of revised copy .. ..

 

 opt.push ('#');

 int len = exp.length ();

 for (int i = 0; i <len;)

 (

 char ch = exp.at (i);

 if (isalnum (ch))

 (

 long num = 0;

 while (isalnum (ch)) / / converted to an integer

 (

 num = (ch-'0 ') + num * 10;

 if (+ + i> = len)

 break;

 ch = exp.at (i);

 )

 opd.push (num); / / operand Ruzhan

 )

 else / / operator on the judge and push

 (

 if (ch == '(') / / left parenthesis direct push

 opt.push (ch);

 else if (ch == ')') / / there are brackets on the stack to bomb the left encountered until the brackets

 (

 ch = opt.top (); / / Top-made operator

 while (ch! = '(') / / pop up to left brackets

 (

 OP (ch);

 opt.pop ();

 ch = opt.top ();

 )

 opt.pop (); / / pop left in brackets

 )

 else

 (

 int thisPri = GetPri (ch); / / operator of the current priority

 char prevOpt = opt.top (); / / one operator

 int prevPri = GetPri (prevOpt); / / operators on a priority

 while (thisPri <= prevPri)

 (/ / Output in the stack operator face up to than the current operator of a lower priority

 OP (prevOpt);

 opt.pop (); / / output after the pop

 prevOpt = opt.top ();

 prevPri = GetPri (prevOpt);

 )

 opt.push (ch); / / push the current operator

 )

 i + +;

 )

 )

 

 char ch = opt.top (); / / expression after scanning the stack in the remainder of the full output of the operator

 while (ch! = '#')

 (

 OP (ch);

 opt.pop ();

 ch = opt.top ();

 )

 

 

 Yesterday and did not see the code almost no difference, just yesterday, the output of the local operations into the OP (const char & ch); completion of the operation

 

 Need to understand that. Expression scanning, is likely to push the operator not to carry out operation (by the concrete expression of this decision), the operator should be the last stack empty shells, all of the completion of the operation .... Operand stack at this time is the only one of the few is the result of the operation.

 

 

 

 Function OP. To achieve the following. Parameter is the operator ch

 

 

 

 void Caculator:: OP (const char & ch)

 (

 long RightOperand = opd.top ();

 opd.pop ();

 long LeftOperand = opd.top (); / / These two operations can not be wrong in the order of a few

 opd.pop ();

 switch (ch)

 (

 case '+':

 LeftOperand + = RightOperand;

 break;

 case '-':

 LeftOperand -= RightOperand;

 break;

 case '*':

 LeftOperand *= RightOperand;

 break;

 case '/':

 if (RightOperand == 0) / / divisor 0

 (

 

 system ( "pause");

 exit (1);

 )

 else

 LeftOperand / = RightOperand;

 default:

 cout << "illegal operator !!----:"<< ch <<endl;

 system ( "pause");

 exit (1);

 )

 opd.push (LeftOperand); / / find the value of the pressure into the stack

 )

 opd is the operand stack, is a member of the class of variables. is the definition of stack <long> opd; / / can only carry out the whole operation

 

 

 

 Simple ...

Previous:Const completion of the use of heavy-duty
Next:HOOK used to disable the keyboard and mouse click
User Reviews
Site Search
Recommended article
AD