梦里三年 2016-04-06 14:36 采纳率: 100%
浏览 1463
已采纳

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条回答

  • 梦里三年 2016-04-06 14:40
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗