2 qq 32122775 qq_32122775 于 2016.04.06 22:36 提问

c++链栈问题,求大神,大一无力啊

程序运行的时候出错,自己弄了好久不知道怎么办,百度了也不行#include
#include
using namespace std;

enum error_code{success,underflow,overflow}; //定义枚举
char opr[]={'+','-','*','/','(',')','#'};
struct node{
char data0;
int data1;
node *next;
};
int comp[7][7]= //优先级矩阵的实现
{
{2,2,1,1,1,2,2},
{2,2,1,1,1,2,2},
{2,2,2,2,1,2,2},
{2,2,2,2,1,2,2},
{1,1,1,1,1,3,0},
{0,0,0,0,0,0,0},
{1,1,1,1,1,0,3}};
class stack0{ //栈的实现
public:
stack0();
bool empty() const; //判空
error_code get_top0(char &x); //运算符取顶
error_code push0(const char x); //运算符压栈
error_code pop0(char&op); //运算符弹栈
void dest();
private:
int count; //辅助空间
node *top;

};

class stack1{ //栈的实现
public:
stack1();
bool empty() const; //判空
error_code get_top1(double &x); //操作数取顶
error_code push1(const double x);//操作数压栈
error_code pop1(double&a); //操作数弹栈
void dest();
private:
int count; //辅助空间
node *top;

};

stack0::stack0() {count=0;top=0;} //初始化
stack1::stack1() {count=0;top=0;} //初始化
void stack0::dest()
{
node *u=new node;
while(top)
{
u=top;
top=top->next;
delete u;
}
}
void stack1::dest()
{
node *u=new node;
while(top)
{
u=top;
top=top->next;
delete u;
}
}

bool stack0::empty()const{
return count==0;
}

bool stack1::empty()const{
return count==0;
}

error_code stack0::get_top0(char &x) {
if(empty()) return underflow;
x=top->data0;
return success;
}

error_code stack1::get_top1(double &x) {
if(empty()) return underflow;
x=top->data1;
return success;
}

error_code stack0::push0(const char x){
node *s=new node;
s->data0=x;
s->next=top;
top=s;
count++;
return success;

}
error_code stack1::push1(const double x){
node *s=new node;
s->data1=x;
s->next=top;
top=s;
count++;
return success;

}
error_code stack0::pop0(char&op){
if(empty()) return underflow;
else{
node *u=new node;
u=top;
top->next=u->next;
delete u;
count--;
return success;
}
}
error_code stack1::pop1(double&a){
if(empty()) return underflow;
else{node *u=new node;
u=top;
top->next=u->next;
delete u;
count--;
return success;
}
}

char cp(char ch1,char ch2) //比较两个运算符的优先级函数
{
int i,m,n;
char prv;
int prio;
for(i=0;i {
if(ch1==opr[i])
m=i;
if(ch2==opr[i])
n=i;
}
prio=comp[m][n];
switch(prio){
case 2:
prv='>';
break;
case 3:
prv='=';
break;
case 1:
prv='<';
break;
case 0:
prv='@';
cout<<"wrong."< break;
}
return prv; //返回'>' '>' '='
}

double js(double a,char op,double b){ //将弹栈的数进行运算
double result;
switch(op){
case '+':
result=a+b;
break;
case '-':
result=b-a;
break;
case '*':
result=a*b;
break;
case '/':
result=b/a;
break;
}
return result;

}
bool ifopt(char ch) //判断是否为字符
{
for(int i=0;i<7;i++)
{
if(ch==opr[i])
return true;
}
return false;
}

double jsq() //计算的主要功能的实现
{
char op,i=0,*str,x='#';
double a,b,temp,num;
stack0 operater;stack1 object; //创建类对象
operater.push0('#'); //提前将#压栈
cout<<"input and end with the #"<<endl;
str=(char*)malloc(40*sizeof(char)); //习惯用c语言的方式
gets(str); //读取字符

while(str[i]!='#'||x!='#'){  //判断条件

    if(!ifopt(str[i])){  //将字符型的数转化成double型并压栈
        num=str[i]-'0';
        i++;
        while(!ifopt(str[i])){
            num=num*10+str[i]-'0';
            i++;
        }
     object.push1(num);
    }
    else{
        switch(cp(x,str[i]))  
        {
        case '<':
            operater.push0(str[i]);
            i++;
            break;
        case '=':
            operater.pop0(x);
            i++;
            break;
        case '>':
            operater.pop0(op);
            object.pop1(a);
            object.pop1(b);
            temp=js(a,op,b);
            object.push1(temp);
            break;      
        }
    }operater.get_top0(x); //取顶
}
object.get_top1(temp);
return temp;  //返回结果
//object.dest();
//operater.dest();

}

int main()//主函数
{

double result=jsq();
cout<<result<<endl;
return 0;

}

图片

2个回答

qq_32122775
qq_32122775   2016.04.06 22:40
已采纳

头文件粘贴的时候掉了,#include''iostream''和include''cstdio''

zr1076311296
zr1076311296   2016.04.06 23:50

这代码你是手写的还是粘上去的,有语法错误,你先调试一下吧
图片说明

qq_32122775
qq_32122775 粘贴的时候,掉了一些东西,不知道怎么搞的,用顺序栈的代码改过来的
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!