|
老师教了不太明白,
有没有人能帮帮忙?
我想做个简单的,
只需要输入号码和() , 然后算出来。
我的 header file 是
#define SIZE 10
typedef int StackType;
struct Stack{
StackType Item[SIZE];
int top;
};
void InitializeStack(Stack &);
int EmptyStack(Stack);
int FullStack(Stack);
void Push(StackType, Stack &);
void Pop(Stack &, StackType &);
implementation 是#include <iostream.h>
#include "Stack.h"
void InitializeStack(Stack &S){
S.top=0;
}
int EmptyStack(Stack S){
return (S.top==0);
}
int FullStack(Stack S){
return (S.top==SIZE);
}
void Push(StackType x, Stack &S){
if (FullStack(S)) {
cout<<"Error : Stack Fulled"<<endl;
}else {
S.Item[S.top]=x;
S.top++;
}
}
void Pop(Stack &S, StackType &x){
if (EmptyStack(S)){
cout<<"Error : Stack Empty!"<<endl;
}else {
S.top--;
x=S.Item[S.top];
}
}
然后我的code 是
#include <iostream.h>
#include "Stack.h"
main(){
Stack S;
StackType x , y;
int result=0;
InitializeStack(S);
cout<<"Enter Number : ";
cin>>y;
for (int i=0; i<'\n'; i++)
Push(y,S);
while (!EmptyStack(S)){
Pop(S,x);
switch (x){
case '+': result = result+x;
break;
case '-': result = result-x;
break;
case '*': result = result*x;
break;
case '/': result = result/x;
}
if(x!=0){
if (x=='('){
Push(x,S);
}
else if( x==')' ){
if(!EmptyStack(S)){
Pop(S,x);
switch (x){
case '+': result = result+x;
break;
case '-': result = result-x;
break;
case '*': result = result*x;
break;
case '/': result = result/x;
}
}
}
}
cout<<"Result : ";
cout<<result<<endl;
return 0;
}}
拿不到答案, 都是0 ; 那一边出了问题呢? |
|