c语言,中缀表达式转后缀表达式并计算

  1 //c语言中缀表达式计算
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 #include <stdbool.h>
  6 #include <math.h>
  7 typedef struct{
  8     char data[85];
  9     int top;
 10 }stack;
 11 typedef struct{
 12     int data[85];
 13     int top;
 14 }nstack;
 15 int priority(char);
 16 char pop(stack*);
 17 int npop(nstack*);
 18 int ntop(nstack*);
 19 char top(stack*);
 20 void push(stack*,char);
 21 void npush(nstack*,char);
 22 bool isnumber(char);
 23 bool isempty(stack*);
 24 int main(void){
 25     int i,j,len,cnt;
 26     stack *st=(stack*)malloc(sizeof(stack));
 27     nstack *nst=(nstack*)malloc(sizeof(nstack));
 28
 29     char str[85];
 30     char out[85];
 31
 32     scanf("%s",str);
 33
 34     nst->top=-1;
 35     st->top=-1;
 36     cnt=0;
 37     len=strlen(str);
 38
 39     for(i=0;i<len;i++)
 40     {
 41         if(isnumber(str[i]))
 42             out[cnt++]=str[i];
 43         else
 44         {
 45             //
 46             if(str[i]==‘(‘||isempty(st))
 47             {
 48                 push(st,str[i]);
 49                 continue;
 50             }
 51             //
 52             if(str[i]==‘)‘)
 53             {
 54                 while(top(st)!=‘(‘)
 55                 {
 56                     out[cnt++]=top(st);
 57                     pop(st);
 58                 }
 59                 pop(st);
 60                 continue;
 61             }
 62             //
 63             while(!isempty(st)&&top(st)!=‘(‘&& priority(str[i])<=priority(top(st)))
 64             {
 65                 out[cnt++]=top(st);
 66                 pop(st);
 67             }
 68             //
 69             push(st,str[i]);
 70         }
 71     }
 72     while(!isempty(st)){
 73         out[cnt++]=top(st);
 74         pop(st);
 75     }
 76     out[cnt]=‘\0‘;
 77     for(i=0;i<cnt;++i)
 78         printf("%c ",out[i]);
 79     printf("\n");
 80     for(i=0;i<cnt;i++)
 81     {
 82         if(isnumber(out[i])){
 83             npush(nst,out[i]);
 84             continue;
 85         }else if(out[i]==‘+‘){
 86             nst->data[nst->top-1]+=ntop(nst);
 87             npop(nst);
 88         }else if(out[i]==‘-‘){
 89             nst->data[nst->top-1]-=ntop(nst);
 90             npop(nst);
 91         }else if(out[i]==‘*‘){
 92             nst->data[nst->top-1]*=ntop(nst);
 93             npop(nst);
 94         }else if(out[i]==‘/‘){
 95             nst->data[nst->top-1]/=ntop(nst);
 96             npop(nst);
 97         }else if(out[i]==‘^‘){
 98             nst->data[nst->top-1]=pow(nst->data[nst->top-1],ntop(nst));
 99             npop(nst);
100         }
101         for(j=0;j<=nst->top;++j)
102             printf("%d ",nst->data[j]);
103         for(j=i+1;j<cnt;++j)
104             printf("%c ",out[j]);
105         printf("\n");
106     }
107     return 0;
108 }
109 bool isnumber(char ch){
110     if(ch>=‘0‘&&ch<=‘9‘)
111         return true;
112     else
113         return false;
114 }
115 bool isempty(stack *s){
116     if(s->top==-1)
117         return true;
118     else
119         return false;
120 }
121 void push(stack *s,char ch){
122     s->data[++s->top]=ch;
123 }
124 void npush(nstack *s,char ch){
125     s->data[++s->top]=ch-‘0‘;
126 }
127 char pop(stack *s){
128     return s->data[s->top--];
129 }
130 int npop(nstack *s){
131     return s->data[s->top--];
132 }
133 int priority(char ch){
134     if(ch==‘(‘)
135         return 0;
136     if(ch==‘+‘||ch==‘-‘)
137         return 1;
138     if(ch==‘*‘||ch==‘/‘)
139         return 2;
140     if(ch==‘^‘)
141         return 3;
142     return 0;
143 }
144 char top(stack *s){
145     return s->data[s->top];
146 }
147 int ntop(nstack *s){
148     return s->data[s->top];
149 }

原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10730274.html

时间: 2024-10-22 19:33:52

c语言,中缀表达式转后缀表达式并计算的相关文章

中缀表达式转后缀表达式c语言实现

1.运行环境:VS2015/Win7 2.头文件: stack.h 1 typedef char ElementType; 2 /* START: */ 3 #ifndef _Stack_h 4 #define _Stack_h 5 6 struct StkNode; 7 typedef struct StkNode *PtrStk; 8 typedef PtrStk Stack; 9 10 int IsEmpty(Stack S); 11 Stack CreateStack(void); 12

栈的应用之中缀表达式转后缀表达式

1,中缀表达式的定义及为什么要将中缀表达式转换为后缀表达式? 中缀表达式(中缀记法) 中缀表达式是一种通用的算术或逻辑公式表示方法,操作符以中缀形式处于操作数的中间.中缀表达式是人们常用的算术表示方法. 虽然人的大脑很容易理解与分析中缀表达式,但对计算机来说中缀表达式却是很复杂的,因此计算表达式的值时,通常需要先将中缀表达式转换为前缀或后缀表达式,然后再进行求值.对计算机来说,计算前缀或后缀表达式的值要比中缀表达式简单. 比如,计算机计算后缀表达式的过程如下----后缀表达式的计算机求值: 从左

数据结构中缀表达式转后缀表达式以及后缀转中缀表达式

最近一直在看数据结构这本书,我相信,对于每个程序员来说,数据结构都尤为重要.为什么要学,可以看看这位博友的认识http://blog.csdn.NET/sdkfjksf/article/details/54380659 直入主题:将中缀表达式转为后缀表达式 以及将后缀表达式转为前缀表达式的实现. 关于后缀转中缀,中缀转后缀的理论介绍,请先阅读其互转的理论知识,或者我转发的这篇文章,这里不再累赘,最好参考<数据结构与算法描述Java语言版>,接下来将会用java写. 一.首先,怎么实现中缀表达式

中缀表达式转为后缀表达式

** * 中缀表达式转后缀表达式 * * 作用:将一长串计算表达式转换为计算机易于操作的字符序列,用于计算器的设计 *  * 参与转换运算符 * +-/*()^% * * * 使用StringBuilder来保存转换出的后缀表达式 * 使用栈来操作运算符 * * * 转换原则 * 1.上述字符中()没有优先级值,+-优先级值为1,/*%优先级值为2,^优先级值为3 * 2.对于一个待计算的表达式,从左向右逐个检查每个字符 * 3.遇到数字,直接append到StringBuilder * 4.遇

ZH奶酪:Python 中缀表达式转换后缀表达式

实现一个可以处理加减乘数运算的中缀表达式转换后缀表达式的程序: 一个输入中缀表达式inOrder 一个输出池pool 一个缓存栈stack 从前至后逐字读取inOrder 首先看一下不包含括号的: (1)操作数:直接输出到pool (2)操作符:判断当前操作符与stack[top]操作符的优先级 <1>当前操作符优先级高于stack[top]:将当前操作符添加到stack中: <2>当前操作符优先级低于或等于stack[top]:从stack[top]开始出栈,直到stack[to

经典白话算法之中缀表达式和后缀表达式

一.后缀表达式求值 后缀表达式也叫逆波兰表达式,其求值过程可以用到栈来辅助存储. 假定待求值的后缀表达式为:6  5  2  3  + 8 * + 3  +  *,则其求值过程如下: (1)遍历表达式,遇到的数字首先放入栈中,依次读入6 5 2 3 此时栈如下所示: (2)接着读到"+",则从栈中弹出3和2,执行3+2,计算结果等于5,并将5压入到栈中. (3)然后读到8(数字入栈),将其直接放入栈中. (4)读到"*",弹出8和5,执行8*5,并将结果40压入栈中

[转]中缀表达式、前缀表达式、后缀表达式的相互转换

--------------------------------后缀转中缀---------------------------------------------- 1.建立一个栈,从左向右扫描后缀表达式,遇到运算数则压入栈: 2.遇到运算符就把栈顶两个元素出栈,执行运算,得到的结果作为新的运算符再压入栈: 3.依次走到表达式结尾: 例:把逆波兰式(即后缀表达式)ab+c*转换为中缀表达式: 1)a入栈(0位置) 2)b入栈(1位置) 3)遇到运算符"+",将a和b出栈,执行a+b的

数据结构——栈——中缀表达式和后缀表达式

什么是中缀表达式,什么是后缀表达式 我们一般看见的多项式计算都是中缀表达式构成的:1+2*3+4/3 类似这种,为什么说是中缀呢?因为它的计算符号都是在两个数中间的. 那么自然而然的明白了后缀表达式是一种计算符号在两个数后面的. 如123*+43/+ 中缀表达式和后缀表达式有什么关系? 其实仔细你就会发现,上面给出的式子其实都是一样的,只是计算的顺序在后缀表达式中你看不懂而已. 因为我们习惯去看中缀表达式的计算. 其实他们之间是可以互相转换的.他们也可以表达同一个意思,同一个计算式子. 为什么会

表达式的计算(中缀表达式转为后缀表达式或逐步计算)

算数表达式的计算,也是很基础的一个问题.花了点时间写了下. 网上很多正确代码.但没有详细说明.虽然不复杂,但是还是写详细点.只有仔细思考过.问题才会在头脑中,觉得简单. 基本有2种方法. 1)中缀表达式转为后缀表达式,是最简洁有力的方法. 2)符合人的计算思路的逐步方法,不推荐使用,只适合锻炼下逻辑能力. 一.中缀表达式转为后缀表达式,是最简洁有力的方法. //更简洁通用的算法,就是把中缀表达式转换为后缀表达式.后缀表达式:不包含括号,运算符放在两个运算对象的后面. //一,无括号的n级符号算法

将中缀表达式转换为后缀表达式,然后利用栈对表达式求值。

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="js.js"></script> </head> <body> 输入中缀表达式空格分隔 例如 2 + 3 <input type=