Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the complete C code of inverse Polish calculator

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report