Inverse polish expression learning diary (2)

Source: Internet
Author: User
Given this Wednesday, there are no regular classes in the afternoon. In addition, the suffix results are quite simple. Let's write this first. I have already converted the expression. Now I only need to perform a group of numbers, addition, subtraction, multiplication, division, and so on. (To be honest, it's a bit lazy. Recently, the PM2.5 in Changchun is a little high, I am not interested in ACM at all.) intchange (char * a) {stackint

Given this Wednesday, there are no regular classes in the afternoon. In addition, the suffix results are quite simple. Let's write this first. I have already converted the expression. Now I only need to perform a group of numbers, addition, subtraction, multiplication, division, and so on. (To be honest, it's a bit lazy. Recently, the PM2.5 in Changchun is a little high, I am not interested in ACM at all.) int change (char * a) {stack int

Given this Wednesday, there are no regular classes in the afternoon. In addition, the suffix results are quite simple. Let's write this first.

I have already converted the expression. Now I only need to perform a group of numbers, addition, subtraction, multiplication, division, and so on. (To be honest, it's a bit lazy. Recently, the PM2.5 in Changchun is a little high, I am not interested in ACM at all)

Int change (char *)
{
Stack T;
Int I = 0;
While (a [I]! = '\ 0 ')
{
If (a [I]> = '0' & a [I] <= '9') // Press
{
T. push (int) a [I ++]-'0 ');
}
Else
{
Int num1, num2;
Num1 = t. top (); t. pop ();
Num2 = t. top (); t. pop ();
Switch (a [I ++]) // if there is an operator, extract the number first, and then compress the result.
{
Case '+': num1 + = num2; t. push (num1); break;
Case '-': num2-= num1; t. push (num2); break;
Case '*': num1 * = num2; t. push (num1); break;
Case '/': num2/= num1; t. push (num2); break;
}
}
}
Return t. top (); // finally, the last number left in the stack is the result you want.
}

Because I want to see something behind the data structure, I am anxious to end it. These two functions can only be used for single-digit operations without inserting blank lines in the suffix expression to differentiate numbers, so keep it for me and write it again .. (Sleepy)

Over

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.