C语言表达式计算器
为了方便了解流程,在程序中把计算过程也输出了.而且栈操作的实现部分也是自己实现的. RE6P_Wd程序用两个栈,optr寄存运算符,opnd寄存操作数和运算结果.输入的表达式以等号结束,例如:2*(1+2)=
/**************表达式计算器************/
#include <stdio.h>
#include <stdlib.h>q6M:]%yH;P1x/T
#include <string.h>
#include <conio.h>+hFy@v ]N
#include <malloc.h>
#define STACK_SIZE 1009IRL2V;zA2H?
#define APPEND_SIZE 10
struct SNode{
float data; /*存放操作数或者计算结果*/
char ch; /*存放运算符*/*PW[jt~A'G:W4v
};CGN2YmKI?(R"W
)R^M,@p
struct Stack{!a1n(]'wN^F
SNode *top;"K#O%@M`$v1I|
SNode *base;
int size;+I+vQ1{_
};#hk7A` ~.l)y
/*栈操作函数*/1jAN$r-M)YZ
int InitStack(Stack &S); /*创建栈*/
int DestroyStack(Stack &S); /*销毁栈*/QYy!K7S,F
int ClearStack(Stack &S); /*清空栈*/
int GetTop(Stack S, SNode &e); /*取出栈顶结点并返回节点值*/;n2X x"ZY/@Wn3?
int Push(Stack &S,SNode e); /*将结点e压入栈*/\+{ L7~Jjo
int Pop(Stack &S,SNode &e); /*删除栈顶结点并返回其节点值*/nLw:x9J7FT AC
T_TMss~ZL
/*表达式计算器相关函数*/7g7s_nJEM.?V0e
char get_precede(char s,char c); /*判断运算符s和c的优先级*/
int isOpr(char c); /*判断输入的字符是不是运算符,是则返回0,否返回1*/1@#OG&DO6P*_
float operate(float x, char opr, float y); /*计算x和y经过运算符opr计算后的结果*/
float compute(); /*表达式结算器主函数*/YC7i9y;_1n$`.Zi
char *killzero(float result); /*去掉结果后面的0*/