前,中,后缀表达式计算 无误c语言.doc
文本预览下载声明
#includestdio.h /*导入需要用到的各种包*/
#includestdlib.h
#includestring.h
typedef struct /*定义结构体用来存储操作符*/
{
char op; /*存储字符*/
int level; /*存储优先级*/
}OpNode;
typedef struct
{
OpNode op[100];
int top;
int size; /*表示栈内元素的个数*/
} stack; /*定义符号栈*/
void init(stack *st) /*初始化栈*/
{
st-size=0;
st-top=0;
}
OpNode pop(stack *a)
{
if (a-size==0) /*如果栈为空结束操作*/
{
exit(-1);
}
a-size--;
return a-op[--(a-top)]; /*取出栈顶元素*/
}
void push(stack *a,OpNode op) /*入栈函数*/
{
a-size++;
a-op[(a-top)++]=op;
}
OpNode top(stack *a) /*观察栈顶函数*/
{
if (a-size==0) /*如果栈为空结束操作*/
{
printf(stack is empty\n);
exit(-1);
}
return a-op[(a-top)-1]; /*只得到栈顶的值而不出栈*/
}
typedef struct /*定义数值栈*/
{
double num[100];
int top; /*栈顶指针*/
int size;
} numstack;
void init2(numstack *st) /*初始化数值栈*/
{
st-size=0;
st-top=0;
}
double pop2(numstack *a) /*数值栈出栈*/
{
if (a-size==0) /*出栈前的判空*/
{
exit(-1);
}
a-size--;
return a-num[--(a-top)]; /*得到栈顶的值*/
}
void push2(numstack *a,double num) /*入栈*/
{
a-size++;
a-num[(a-top)++]=num;
}
int main(void) /*主函数*/
{
void change (char str[],char exp[]); /*声明要用到的各个函数*/
double CalResult(char exp[]); /*声明后缀表达式的计算函数*/
double Directcalresult(char str[]);
int check(char str[],char chestr[100]);
char str[100],exp[100],chestr[100]; /*str存储原算术表达式,exp存储对应的 printf(算术表达式为:\n); 后缀表达式,chestr存储容错字符^*/
gets(str);
if(check(str,chestr))
显示全部