梦里三年 2016-03-17 15:57 采纳率: 100%
浏览 1905
已采纳

C++用栈实现计算器,不知道怎么错了,求大神

#include
#include
#define maxlen 40
using namespace std;

enum error_code{succes,underflow,overflow};
char opr[]={'+','-','*','/','(',')','#'};
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},
{2,2,2,2,0,2,2},
{1,1,1,1,1,0,3}};
class stack{
public:
stack();
bool empty() const;
bool full() const;
error_code get_top0(char &x);
error_code push0(const char x);
error_code get_top1(int &x);
error_code push1(const int x);
error_code pop0(char &op);
error_code pop1(int &a);
private:
int count;
char data0[maxlen];
int data1[maxlen];
};

stack::stack() {count=0;}

bool stack::empty()const{
if(count==0) return true;
else return false;
}

bool stack::full() const{
if(count==maxlen) return true;
else return false;
}

error_code stack::get_top0(char &x) {
if(empty()) return underflow;
else{
x=data0[count-1];
return succes;
}
}

error_code stack::get_top1(int &x) {
if(empty()) return underflow;
else{
x=data1[count-1];
return succes;
}
}

error_code stack::push0(const char x){
if(full()) return overflow;
else{
data0[count]=x;
count++;
return succes;
}
}
error_code stack::push1(const int x){
if(full()) return overflow;
else{
data1[count]=x;
count++;
return succes;
}
}
error_code stack::pop0(char&op){
if(empty()) return underflow;
else{
op=data0[count-1];
count--;
return succes;}
}
error_code stack::pop1(int&a){
if(empty()) return underflow;
else{
a=data1[count-1];
count--;
return succes;}
}

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."<<endl;
break;
}
return prv;
}

int js(int a,char op,int b){
int result;
switch(op){
case '+':
result=a+b;
break;
case '-':
result=a-b;
break;
case '*':
result=a*b;
break;
case '/':
result=a/b;
break;
}
return result;

}
bool ifopt(char ch)
{
for(int i=0;i<7;i++)
{
if(ch==opr[i])
return true;
}
return false;
}

int jsq()
{
char op,i=0,*str,x=',';
int a,b,temp,num;
stack operater,object;
operater.push0('#');
cout<<"input and end with the'#'"<<endl;
str=(char*)malloc(40*sizeof(char));
gets(str);

while(str[i]!='#'||x!='#'){
operater.get_top0(x);
    if(!ifopt(str[i])){
        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 '=':
            object.pop0(op);i++;
            break;
        case '>':
            operater.pop0(op);
            object.pop1(a);
            object.pop1(b);
            temp=js(a,op,b);
            object.push0(temp);
            break;      
        }
    }
}operater.get_top0(x);
object.get_top1(temp);
return temp;

}

int main()
{

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

}

  • 写回答

3条回答

  • lx624909677 2016-03-18 10:48
    关注

    具体是哪儿错了?用后缀表达式算法的么?

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

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度