算术表达式求值算法?

来源:百度知道 编辑:UC知道 时间:2024/09/22 03:38:00
用C语言版 编写一个表达式求值演算的 算法程序
要求:使用栈,需要将算法的过程显示出来 ,输入一连串算术值 如:2*(3+2)# 然后 显示出算术过程 和结果!先将输入的中缀 转换后缀 再求值 程序中使用两个栈! 可以在VC++ 中运行
(高分 等!!!)
程序中要求定义运算符op的优先级。
表达式由键盘输入!

#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<string.h>

#define DEBUG

#define NULL 0
#define ERROR -1
#define STACKSIZE 20

/* 定义字符类型栈 */
typedef struct{
char stackname[20];
char *base;
char *top;
} Stack;

/* ----------------- 全局变量--------------- */
Stack OPTR, OPND; /* 定义前个运算符栈,后个操作数栈 */
char expr[255] = ""; /* 存放表达式串 */
char *ptr = expr;
int step = 0; /* 计算的步次 */

int InitStack(Stack *s, char *name)
{
s->base=(char *)malloc(STACKSIZE*sizeof(char));
if(!s->base) exit (ERROR);
strcpy(s->stackname, name);
s->top=s->base;
return 1;
}

int In(char ch)
{
return(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')'||ch=='#');
}

void OutputStat