C语言表达式计算器
为了方便了解流程,在程序中把计算过程也输出了.而且栈操作的实现部分也是自己实现的.程序用两个栈,optr寄存运算符,opnd寄存操作数和运算结果.输入的表达式以等号结束,例如:2*(1+2)=
/**************表达式计算器************/&K\ny%o'@
#include <stdio.h>]JuEk6T,c
#include <stdlib.h>
#include <string.h> aX Upw$n*i
#include <conio.h>
#include <malloc.h>
#define STACK_SIZE 100
#define APPEND_SIZE 10jH}!e-QG0Y{\F
%p%U%j9r}i}L
struct SNode{
float data; /*存放操作数或者计算结果*/Q^]M s,e
char ch; /*存放运算符*/
};6_!V$H,a-e?
struct Stack{uK$R#F!Hg
SNode *top; H Ya ZF'u
SNode *base; ~c4e"dqUT+n E
int size;]:^9F(Q] a
};0rFlQoI/Wc/v
%{+E0G2dE}3T*^C
/*栈操作函数*/
int InitStack(Stack &S); /*创建栈*/
int DestroyStack(Stack &S); /*销毁栈*/ ]1Z+Q[sF3}t
int ClearStack(Stack &S); /*清空栈*/
int GetTop(Stack S, SNode &e); /*取出栈顶结点并返回节点值*/
int Push(Stack &S,SNode e); /*将结点e压入栈*/
int Pop(Stack &S,SNode &e); /*删除栈顶结点并返回其节点值*/
2r5p |IIrP?
/*表达式计算器相关函数*/;k(U-R;bN(R6Tw
char get_precede(char s,char c); /*判断运算符s和c的优先级*/
int isOpr(char c); /*判断输入的字符是不是运算符,是则返回0,否返回1*/
float operate(float x, char opr, float y); /*计算x和y经过运算符opr计算后的结果*/
float compute(); /*表达式结算器主函数*//fH ~@,Hwj
char *killzero(float result); /*去掉结果后面的0*/ $BT4}x"RZ.s5h:~@f
8Ih e6dc4{6US
int InitStack(Stack &S)8H"klpH3E p#a9n
{'F1N{KT$dn
S.base=(SNode *)malloc(STACK_SIZE * sizeof(struct SNode));