能不能帮忙做一下?或者给个例子?谢谢!
[此贴子已经被作者于2007-10-26 2:01:51编辑过]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxbuf 80
/*------定义结构体-------*/
struct s_node
{
int data;
struct s_node *next;
};
typedef struct s_node s_list;
typedef s_list *link;
link operator=NULL;
link operand=NULL;
/*-----将数据入堆栈-------*/
link push(link stack,int value)
{
link newnode;
/*配置节点内存*/
newnode=(link) malloc (sizeof(s_list));
if(!newnode)
{
printf("\nERROR");
return NULL;
}
newnode->data=value;
newnode->next=stack;
stack=newnode;
return stack;
}
/*------从堆栈中读取数据-------*/
link pop(link stack,int *value)
{
link top;
if (stack!=NULL)
{
top=stack;
stack=stack->next;
*value=top->data;
free(top);
return stack;
}
else
*value=-1;
}
/*------检查堆栈是否为空------*/
int empty(link stack)
{
if (stack==NULL)
{
return 1;
}
else
return 0;
}
/*-----判断是否为运算符---------*/
int is_operator(char operator)
{
switch(operator)
{
case '+': case '-': case '*': case '/': case '(': case ')': return 1;
default: return 0;
}
}
/*-------判断运算符的优先权--------*/
int priority(char operator)
{
switch (operator)
{
case '(': return 1;
case '+': case '-': return 2;
case '*': case '/': return 3;
default: return 0;
}
};
/*-------计算两个操作树的值---------*/
int two_result(int operator, int operand1,int operand2)
{
switch (operator)
{
case '+': return (operand2+operand1);break;
case '-': return (operand2-operand1);break;
case '*': return (operand2*operand1);break;
case '/': return (operand2/operand1);break; /*这里应该做异常处理*/
}
}
/*------------------------------------------主程序------------------------------*/
int main(int argc, char * argv[])
{
char expression[maxbuf]; /*存放表达式*/
int position=0; /*指针的位置*/
int op=0; /*运算符*/
int operand1=0; /*第一个操作数*/
int operand2=0; /*第二个操作数*/
int result=0; /*结果*/
int flag=0; /*判断操作数是否是多位*/
printf("\n please input the inorder expression:\n");
gets(expression);
while (expression[position]!='\0' && expression[position]!='\n')
{
if (is_operator(expression[position])) /*是运算符时做以下操作*/
{
flag=0;
if(expression[position]=='(')
{
operator=push(operator,expression[position]);
position++;
}
else if(expression[position]==')')
{
while(operator->data !='(')
{
operator=pop(operator,&op);
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);
operand=push(operand,two_result(op,operand1,operand2));
}
if(operator->data =='(')
operator=pop(operator,&op);
position++;
}
else if (!empty(operator)||empty(operator))
{
if (!empty(operator))
while (priority(expression[position])<=priority(operator->data) && !empty(operator))
{
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);
operator=pop(operator,&op);
operand=push(operand,two_result(op,operand1,operand2));
}
operator=push(operator,expression[position]);
position++;
}
}
else /*是数字时进行如下操作*/
if(flag==1)
{ /*将字符串形式的数字转换成数字*/
operand=pop(operand,&operand1);
operand1=operand1*10+expression[position]-48;
operand=push(operand,operand1);
position++;
}
else
{
operand=push(operand,expression[position]-48);
position++;
flag=1;
}
}
while (!empty(operator))
{
operator=pop(operator,&op);
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);
operand=push(operand,two_result(op,operand1,operand2));
}
operand=pop(operand,&result);
printf("the expression %s = %d\n",expression,result);
getchar(); /*加个方法可以显示结果*/
return 0;
}
程序学院的,试下吧