unexpected end of file found
shdxu 2006-10-30 01:18:31 #include "stdlib.h"
#include "string.h"
#include "iostream.h" #include "stdafx.h" #define MAX 100
typedef struct //声明堆栈结构
{
int stack[MAX]; //栈顶指针
int top; //栈顶数据
}sqstack;
sqstack operator,operand;
//存入堆栈数据
int push(sqstack *s,int value)
{
if(s->top=MAX-1)
return 0;
s->top++;
s->stack[s->top]=value;
return 1;
}
//从堆栈中取出数据
int pop(sqstack *s)
{
int x;
if(s->top<0)
return NULL;
x=s->stack[s->top];
s->top--;
return x;
}
//检查堆栈是否为空
int empty(sqstack *s)
{
if(s->top<0)
return 1;
else
return 0;
}
//判断是否为运算符
int is_oprator(char operator)
{
switch(operator) //operator是运算符
case '+': case'-': case '*': case '/':
return 1;
default:
return 0;
}
//判断运算符的优先权
int priority(char operator)
{
switch(operator)
{
case '+': case'-':
return 1;break;
case '*': case '/':
return 2;break;
default:return 0;
}
}
//计算任意两个操作数的值
int two_result(int operator,int operand1,int operand2)
{
switch(operator)
{
case '+':return (operand1+operand2);
case '-':return (operand1-operand2);
case '*':return (operand1*operand2);
case '/':return (operand1/operand2);
}
}
//主程序,输入中序表达式后计算出表达式的结果值
void main()
{
char expreession[100];
int position=0;
int op=0;
int operand1=0;
int operand2=0;
operator->top=operand->=-1;
cout<<endl;
cout<<"Please input the inorder expression:";
gets(expression);
while(expression[position]!='\0'&&expression[position]!='\n')
{
if(is_operator(expression[position]))
{
if(!empty(oparetor))
while(priority(expression[position])<=priorty(operator->data)&&!empty(operator))
{
operand1=pop(operand);
operand2=pop(operand);
op=pop(operator);
oprand=push(operand,two_result(op,operand1,operand2);
}
operator=push(operator,expression[position]);
}
else
operand=push(operand,expression[position]-48);
position++;
}
while(!empty(operator))
{
op=pop(operator);
oprand1=pop(operand);
oprand2=pop(operand);
operand=push(operand,two_result(op,operand1,operand2));
}
evaluate=pop(operand);
cout<<"The expression["+expression+"] result is '"+evaluate+"'"<<endl;
}
求中序表达式程序
运行出现unexpected end of file found错误