C语言表达式计算器
为了方便了解流程,在程序中把计算过程也输出了.而且栈操作的实现部分也是自己实现的.程序用两个栈,optr寄存运算符,opnd寄存操作数和运算结果.输入的表达式以等号结束,例如:2*(1+2)=]GU+{r0j
/**************表达式计算器************/
#include <stdio.h>
#include <stdlib.h>n Un"I7]5L+x
#include <string.h>
#include <conio.h>
#include <malloc.h>Pz6];D0Rb/Z3j_
B$bN;OW/rT
#define STACK_SIZE 100
#define APPEND_SIZE 10
aL:^,OG"Xu z M
struct SNode{)Uq*fPv'S
float data; /*存放操作数或者计算结果*/|f(F6}M8M
char ch; /*存放运算符*/&jHV Ts.K!I"}
};
&rX/HI.C&k
struct Stack{
SNode *top;~V#}9DlW
SNode *base;8vfM}0NY*i`lK
int size;^)iZR3BrR/T1Cm
};u8a?V3R#_Y
/*栈操作函数*//B7D+E5Jp$l
int InitStack(Stack &S); /*创建栈*/N?*fR4e9t
int DestroyStack(Stack &S); /*销毁栈*/~P3~&U(n _ Skd nJ
int ClearStack(Stack &S); /*清空栈*/7|,HHD[r
int GetTop(Stack S, SNode &e); /*取出栈顶结点并返回节点值*/
int Push(Stack &S,SNode e); /*将结点e压入栈*/1J!FWgPo#_q6?s
int Pop(Stack &S,SNode &e); /*删除栈顶结点并返回其节点值*/z,`,yGA6E.`*Y)Er$Z?
GQ#YY {,n{N6Y
/*表达式计算器相关函数*/%Th#olp&Z