佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 978|回复: 1

MS C++ stack 的问题。

[复制链接]
发表于 30-12-2006 08:52 PM | 显示全部楼层 |阅读模式
老师教了不太明白,
有没有人能帮帮忙?
我想做个简单的,
只需要输入号码和() , 然后算出来。
我的 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 ; 那一边出了问题呢?
回复

使用道具 举报


ADVERTISEMENT

发表于 30-12-2006 11:27 PM | 显示全部楼层
觉的你的pass by reference没处理好。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 28-8-2025 04:31 AM , Processed in 0.123943 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表