How to write the complete C code of inverse Polish calculator
This article introduces you how to write the complete C code of the inverse Polish calculator. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
/ * inverse Polish calculator * / # include#include#include#includetypedef double Elemtype;typedef struct StackNode {Elemtype data; struct StackNode * next;} StackNode, * pStackNode;typedef struct LinkStack {pStackNode top; int count;} LinkStack, * pLinkStack;// chain stack creation and initialization pLinkStack InitStack (void) {pLinkStack s = (pLinkStack) malloc (sizeof (LinkStack)); s-> top = NULL; s-> count = 0 Return s;} / / whether the chain stack is empty int StackEmpty (pLinkStack s) {return (s-> top = = NULL? 1: 0);} / / chain stack void Push (pLinkStack s, Elemtype e) {pStackNode p = (pStackNode) malloc (sizeof (StackNode)); p-> data = e; if (s-> top! = NULL) p-> next = s-> top S-> top = p; s-> count++;} / / chain stack Elemtype Pop (pLinkStack s) {Elemtype temp; pStackNode p; if (StackEmpty (s)) exit (0); temp = s-> top- > data; p = s-> top; s-> top = s-> top- > next; free (p); s-> count-- Return temp;} int main () {char c; Elemtype temp1, temp2; char str [20]; int iTun0; pLinkStack s = InitStack (); printf ("Please enter an expression:\ n"); scanf ("% c", & c) While (c! ='\ n') {while (isdigit (c) | | centering numbers.') {/ / filter digits str [I] = c; str [I] ='\ 0' If (I > = 20) printf ("error: the data entered is too large!\ n"); scanf ("% c", & c); if (c = ='') {Push (s, atof (str)) I = 0; break;}} switch (c) {case'+': temp1 = Pop (s); temp2 = Pop (s) Push (s, temp2 + temp1); break; case'-': temp1 = Pop (s); temp2 = Pop (s); Push (s, temp2-temp1) Break; case'*': temp1 = Pop (s); temp2 = Pop (s); Push (s, temp2 * temp1); break Case'/': temp1 = Pop (s); temp2 = Pop (s); Push (s, temp2 / temp1); break; default: break } scanf ("% c", & c);} printf ("result:% f\ n", Pop (s)); return 0;} how to write the complete C code of the inverse Polish calculator is shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.