Stack of C-Language Data Structure: Computing of infix expressions and infix expressions in Data Structures

Source: Internet
Author: User
Tags stack pop

Stack of C-Language Data Structure: Computing of infix expressions and infix expressions in Data Structures

* Note: I am not technically competent. I just take the code out and check it out. The code is full of loopholes and there is no optimization at all. It mainly depends on temperament, right?

 

Having learned the data structure-stack, of course there is no need for exercises. The most difficult and interesting part of the exercise is the computation of the infix expression (+-*/and ^, of course, parentheses can be included ). After a long time, I finally got it. Briefly describe the program principle:

Because the infix expression is basically not computation (even if it can, it will certainly time out), you have to convert the infix expression into a suffix expression.

The program is divided into two steps

Step 1: Convert the infix expression into a suffix expression

Create a string, such as get, to store the suffix expression

First, enter a string to read a single character one by one. If it is a number, it is directly saved to get. If it is an operator, then:

Create a stack, such as chas

While loop when the top stack operator priority is greater than or equal to the current operator, the top stack element is displayed and assigned to get until it finds that the priority is less than the top stack operator of the current operator, the stack top operator smaller than the current operator does not pop up and then pushes the current operator into the stack.

When the current operator is '(', it is directly pushed into the stack.

When the current operator is ')', the while LOOP pops up the stack top operator and assigns the value to get until the pop-up is '(' only pops up without assigning a value) Note ,') 'do not press into Stack

 

Part 2: Calculate get

Create an int array stack, such as ints

Read get one by one

When the read number is pushed into the ints

When the operator is read, two ints stack top elements are displayed and calculated accordingly. The calculated value is pushed into the ints stack.

 

At last, when get is read, there is only one element left in the ints array and the output is.

 

Source code (c) Written by myself ):

1 # include <stdio. h> 2 # include <string. h> 3 # include <math. h> 4 5 int ints [10000]; 6 int intt; // top 7 char chas [10000] of ints; 8 int chat; // top 9 int I = 0, ii = 1; 10 char c; 11 char get [10000]; // input infix expression 12 char get2 [10000]; // calculated suffix expression 13 14 void intpush (x) // integer stack pressure stack 15 {16 intt ++; ints [intt] = x; 17} 18 void chapush (x) // gossip stack pressure 19 {20 chat ++; chas [chat] = x; 21} 22 int intpop () // integer stack pop-up 23 {24 intt- -; Return ints [intt + 1]; 25} 26 char chapop () // type stack pop-up 27 {28 chat --; return chas [chat + 1]; 29} 30 void intadd (int x) // Add a new bit number to the top element of the integer stack 31 {32 ints [intt] * = 10; ints [intt] + = x; 33} 34 35 int find () // get2 is added to operator 36 {37 c = chapop (); 38 get2 [ii] = ''; 39 get2 [ii + 1] = c; 40 ii + = 2; 41 if (chat = 0) return 0; 42 return 1; 43} 44 45 int main () 46 {47 intt = 0; chat = 0; 48 gets (get); 49 int lengets = strlen (get); 50 For (I = 0; I <= lengets-1; I ++) // read the infix expression 51 one by one {52 if (isdigit (get [I]) // when get [I] is a number, 53 {54 get2 [ii] = get [I]; 55 ii ++; 56} 57 else 58 {59 if (get [I] = '(') chapush ('); 60 if (get [I] =' ^ ') chapush ('^'); 61 if (get [I] = ') 62 {63 c = chapop (); 64 while (c! = '(') 65 {66 get2 [ii] = ''; 67 get2 [ii + 1] = c; 68 ii + = 2; 69 c = chapop (); 70} 71} 72 if (get [I] = '+ ') 73 {74 while (chas [chat] = '+' | chas [chat] = '-' | chas [chat] = '*' | chas [chat] = '/' | chas [chat] = '^ ') 75 {76 if (find () = 0) break; 77} 78 chapush ('+'); 79} 80 if (get [I] = '-') 81 {82 while (chas [chat] = '+' | chas [chat] = '-' | chas [chat] = '*' | chas [chat] = '/' | chas [chat] = '^ ') 83 {84 if (find () = 0) break; 85} 86 chapush ('-'); 87} 88 if (get [I] = '*') 89 {90 while (chas [chat] = '*' | chas [chat] = '/' | chas [chat] = '^ ') 91 {92 if (find () = 0) break; 93} 94 chapush ('*'); 95} 96 if (get [I] = '/') 97 {98 while (chas [chat] = '*' | chas [chat] = '/' | chas [chat] = '^ ') 99 {100 if (find () = 0) break; 101} 102 chapush ('/'); 103} 104 get2 [ii] = ''; 105 ii ++; 106} 107 108} 109 while (chat> 0) // The remaining operator in the output stack is 110 {111 int c = chapop (); 112 Get2 [ii] = ''; 113 get2 [ii + 1] = c; 114 ii + = 2; 115} 116 get2 [ii] = '@'; // Add the terminator 117 I = 1; 118 while (get2 [I]! = '@') // When you see that the terminator stops computing 119 {120 if (get2 [I] = '+' | get2 [I] = '-' | get2 [I] = = '*' | get2 [I] = '/' | get2 [I] = '^ ') 121 {122 int a = intpop (); int B = intpop (); int c; 123 if (get2 [I] = '+') c = a + B; 124 if (get2 [I] = '-') c = B-a; 125 if (get2 [I] = '*') c = a * B; 126 if (get2 [I] = '/') c = B/a; 127 if (get2 [I] = '^') c = pow (B, a); 128 intpush (c); 129} 130 else131 {132 if (get2 [I]! = '') 133 {134 intpush (get2 [I]-48); 135 ii = 1; 136 while (get2 [I + ii]! = '') 137 {138 intadd (get2 [I + ii]-48); 139 ii ++; 140} 141 I + = II-1; 142} 143} 144 I ++; 145} 146 printf ("% d", ints [1]); 147 return 0; 148}

 

Sample input:

1 + (3 + 2) * (7 ^ 2 + 6*9)/(2)

 

Sample output:

258

 

Well, you can write it here. (Original question: informatics osai yiben Tong C ++ Data Structure stack machine practice 4 calc) // don't ask me why I wrote C ++ book exercises, I won't tell you that it's because the file name can be less than two p

 

By the way, in fact, there is no need to write this thing so long. The most NB method:

Right-click desktop: Create a text file

Double-click to open the new text file. bat and enter the following script:

@ Echo offset/p get = set/a ans = % get % echo % ans % pauseexit: You can skip this exit statement.

 

Right-click to create a text file and rename it calc. bat

Double-click to open

Sample input:

1 + (3 + 2) * (7*7 + 6*9)/(2)

 

Sample output:

258

 

Except for the multiplication method, all others are the same.

Oh, I don't want anything more ......

Related Article

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.