描述
利用栈编写表达式求值程序:输入含有“+”、“-”、“*”、“/”四则运算的表达式,其中负数要用(0-正数)表示,并以=结束。要求输出表达式的值(两运算符号的优先关系见教材表3.1)。此题目可选做。输入格式
第一行:一个算术表达式
输出格式
第一行:算术表达式的值
输入样例1
3*(9-7)=输出样例1
6输入样例2
(0-12)*((5-3)*3)/(2+2)=
输出样例2
-18
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define ERROR 0
#define OK 1
struct SNum
{
int *base;
int *top;
int stacksize;
};//数字栈
struct SChar
{
char *base;
char *top;
int stacksize;
};//字符栈
int InitSNum(struct SNum &S)
{
//初始化数字栈
S.base = (int *)malloc(STACK_INIT_SIZE * sizeof(int));
if(!S.base)
return 0;
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
int InitSChar(struct SChar &S)//初始化字符栈
{
S.base = (char *)malloc(STACK_INIT_SIZE * sizeof(char));
if(!S.base)
return 0;
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
int PushN(SNum &S,int e) //插入数字栈
{
if(S.top-S.base>=S.stacksize)
{
S.base = (int *)realloc(S.base,(S.stacksize + STACKINCREMENT)*sizeof(int));
if(!S.base)
return 0;
S.top = S.base + S.stacksize;
S.stacksize