C语言表达式计算器
为了方便了解流程,在程序中把计算过程也输出了.而且栈操作的实现部分也是自己实现的.xJ8J,]]nwP+wJ9G程序用两个栈,optr寄存运算符,opnd寄存操作数和运算结果.输入的表达式以等号结束,例如:2*(1+2)=
/**************表达式计算器************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>8~;p%ghT*z?&|6S J
#include <conio.h>
#include <malloc.h>v8H+Zq5LO%O
w6C4l#k%S\3[V
#define STACK_SIZE 100
#define APPEND_SIZE 10
struct SNode{
float data; /*存放操作数或者计算结果*/H%C6V6W$E%p
char ch; /*存放运算符*/a$n$m9A-t|#G$Ko]
};
$In TahB
struct Stack{%~!IR{;V/u0Y ~dqL
SNode *top;
SNode *base;B]3u9x_*VxW
int size;W/R4NXeI bNK9W
};
`Wg&u\
/*栈操作函数*/&oa gHn PLx_
int InitStack(Stack &S); /*创建栈*/
int DestroyStack(Stack &S); /*销毁栈*/_6N3^e,XOd!e
int ClearStack(Stack &S); /*清空栈*/
int GetTop(Stack S, SNode &e); /*取出栈顶结点并返回节点值*/
int Push(Stack &S,SNode e); /*将结点e压入栈*/:Zr&|&[Z'Z
int Pop(Stack &S,SNode &e); /*删除栈顶结点并返回其节点值*/
(i/p9BQkaLS
/*表达式计算器相关函数*/
char get_precede(char s,char c); /*判断运算符s和c的优先级*/
int isOpr(char c); /*判断输入的字符是不是运算符,是则返回0,否返回1*/cf'k!|R2n7Yz&B6E^7P
float operate(float x, char opr, float y); /*计算x和y经过运算符opr计算后的结果*/
float compute(); /*表达式结算器主函数*/M$e}B$s!@O[ M*\
char *killzero(float result); /*去掉结果后面的0*/