| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 408 人关注过本帖, 1 人收藏
标题:求助!!关于C++计算器问题!!!有的地方编译不通
只看楼主 加入收藏
my19891115
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-6-15
结帖率:50%
收藏(1)
已结贴  问题点数:20 回复次数:2 
求助!!关于C++计算器问题!!!有的地方编译不通
【stack.h文件】
const int maxstack=30;
typedef double Stack_operand;
typedef char Stack_operate;
enum Error_code{overflow,success,underflow};
enum Error_code outcome;

class Stack
{
public:
    Stack();
    bool empty() const;
    bool full() const;
    Error_code pop();                            //出栈
private:
    int count;
};


class Stack_d:public Stack
{public:
    Error_code top(Stack_operand &item_d) const;                   //取操作数栈顶元素
    Error_code push(const Stack_operand &item_d);            //操作数栈压栈
 private:
    Stack_operand entry[maxstack];
};


class Stack_t:public Stack
{public:
    Error_code top(Stack_operate &item_t) const;                   //取运算符栈栈顶元素
    Error_code push(const Stack_operate &item_t);            //运算符栈压栈
 private:
    Stack_operate entry[maxstack];
};





【caculator.cpp文件】
#include<iostream>
#include<stack>
#include"stack.h"


//以下为Stack类定义

Stack::Stack()                                      //constructor
{count=0;}

bool Stack::empty() const
{bool outcome=true;
 if(count>o)outcome=false;
 return outcome;
}

bool Stack::full() const;
{bool outcome=false;
 if(count=maxstack-1) outcome=true;
 return outcome;
}

Error_code Stack::pop();                              
{Error_code outcome=success;
 if(count=0)
    outcome=underflow;
 else --count;
 return outcome;
}


Error_code Stack_d::top(Stack_operand &item_d)const;              //取操作数栈顶元素item_d
{Stack_operand &item_d;
 Error_code outcome=success;
 if(count=0)
    outcome=underflow;
 else item_d=entry[count-1];
 return outcome;
}


Error_code Stack_t::top(Stack_operate &item_t)const;              //取运算符栈栈顶元素item_t
{Stack_operate &item_t
 Error_code outcome=success;
 if(count=0)
    outcome=underflow;
 else item_t=entry[count-1];
 return outcome;
}


Error_code Stack_d::push(const Stack_operand &item_d);            //将数据item_d压入操作数栈中
{const Stack_operand &item_d
 Error_code outcome=success;
 if(count>=maxtrack)
    outcome=overflow;
 else entry[count++]=item_d;
 return outcome;
}


Error_code Stack_t::push(const Stack_operate &item_t);            //将数据item_t压入运算符栈中
{const Stack_operate &item_t
 Error_code outcome=success;
 if(count>=maxtrack)
    outcome=overflow;
 else entry[count++]=item_t;
 return outcome;
}




//以下为调用函数定义

int judge(char ch)                                          //判断函数:判断ch是运算符号还是运算数据
{if(ch='0'||ch='1'||ch='2'||ch='3'||ch='4'||ch='5'||ch='6'||ch='7'||ch='8'||ch='9'||ch='.')
    return 1;
 if(ch='('||ch=')'||ch='+'||ch='-'||ch='*'||ch='/'||ch='#')
    return 0;

}




int qiumi(int a,int b)                                  //求幂函数:a的b次幂(既10的b次幂)(目的:将数组转化为十进制数后压栈)
{int a,b;
 int i,temp=1;
 for(int i=0;i<b;i++)
 {temp*=a;}
return temp;
}


double deci_sim(int t)                                      //求值:将数组转化为十进制数后压栈回运算数据
{int mi[10],q;
 double operand=0;
 for(q=0;t=0;q++,t--)
 {
    mi[q]=int qiumi(10,q)
    operand=operand+temp[t]*mi[q];
 }
    return operand;




double deci_comp(int n)
{int n
 double sum;
 
 double mi[10],operand_xs;                                     //小数点后面转化成十进制数
 int p;
 for(p=-1;j+1=i;p--)
 {
     double temp[10];
     mi[p]=int qiumi(10,p);
     operand_xs=operand_xs+temp[j+1]*mi[p];
     j=j+1;
 }

 sum=deci_sim(n)+operand_xs;
 return sum;
}



         
char compare(t.top,ch)                                   //判断函数:判断运算符栈运算符号的优先级      
{
if (t.top=='+'&&ch='+')return '>';
if (t.top=='+'&&ch='-')return '>';
if (t.top=='-'&&ch='+')return '>';
if (t.top=='-'&&ch='-')return '>';
if (t.top=='*'&&ch='*' return '>';
if (t.top=='/'&&ch='/')return '>';
if (t.top=='*'&&ch='/')return '>';
if (t.top=='/'&&ch='*')return '>';
if (t.top=='*'&&ch='+')return '>';
if (t.top=='*'&&ch='-')return '>';
if (t.top=='/'&&ch='+')return '>';
if (t.top=='/'&&ch='-')return '>';
if (t.top=='+'&&ch=')')return '>';
if (t.top=='+'&&ch='#')return '>';
if (t.top=='-'&&ch=')')return '>';
if (t.top=='-'&&ch='#')return '>';
if (t.top=='*'&&ch=')')return '>';
if (t.top=='*'&&ch='#')return '>';
if (t.top=='/'&&ch=')')return '>';
if (t.top=='/'&&ch='#')return '>';
if (t.top==')'&&ch='+')return '>';
if (t.top==')'&&ch='-')return '>';
if (t.top==')'&&ch='*')return '>';
if (t.top==')'&&ch='/')return '>';
if (t.top==')'&&ch='(')return '>';
if (t.top==')'&&ch='#')return '>';


if (t.top=='+'&&ch='*')return '<';
if (t.top=='+'&&ch='/')return '<';
if (t.top=='+'&&ch='(')return '<';
if (t.top=='-'&&ch='*')return '<';
if (t.top=='-'&&ch='/')return '<';
if (t.top=='-'&&ch='(')return '<';
if (t.top=='*'&&ch='(')return '<';
if (t.top=='/'&&ch='(')return '<';
if (t.top=='('&&ch='+')return '<';
if (t.top=='('&&ch='-')return '<';
if (t.top=='('&&ch='*')return '<';
if (t.top=='('&&ch='/')return '<';
if (t.top=='('&&ch='(')return '<';
if (t.top=='#'&&ch='+')return '<';
if (t.top=='#'&&ch='-')return '<';
if (t.top=='#'&&ch='*')return '<';
if (t.top=='#'&&ch='/')return '<';
if (t.top=='#'&&ch='(')return '<';


if (t.top=='('&&ch=')')return '=';
if (t.top=='#'&&ch='#')return '=';
}

double execute(Stack_operand &a,Stack_operate &op,Stack_operand &b)                                    
{double a,b;
 char op;
 switch
 {case'op='+'':return b+a;break;
  case'op='-'':return b-a;break;
  case'op='*'':return b*a;break;
  case'op='/'':return b/a;break;
 }
}

}

【main.cpp文件】
typedef char Stack_entry;
#include "stack.h"
#include<iostream>
#include<stack>
using namespace std;

int main()
{
Stack_d d;                                                               //定义一个操作数栈d
Stack_t t;                                                                 //定义一个运算符栈t
char ch;
double sum_f;
   
t.push('#');                                                                   //将#压入运算符栈栈底
cout<<"Please input an expression(Ending with #)"<<endl;
ch=getchar();


while(ch!='#'||t.top(Stack_operate &item_t)!='#')
{if(judge(ch)=1)                                                               //判断ch是否为操作数
    {int i;
     int m=1
     int temp[10];
     for(i=0;(i<=9)&&(m=1);i++)
        {temp[i]=ch;
         ch=getchar();
         if(judge(ch)=1) m=1;
         else
         {m=-1;i--;}
        }
   
    int j;   
    double sum_m;
    if (for(j=0;j<t;j++) temp[j]=='.')                              //判断数组temp中是否有小数点
       {j=j-1;
        sum_m=double deci_comp(j-1);                                         //有小数点,进入函数deci_comp,进行十进制转换
       }                                                  
    else {double deci_sim(i);                                        //无小数点,进入函数deci_sim,进行十进制转换
          int t=i;
          sum_m=double deci_sim(i);
          }

    d.push(sum_m);
    }



else
 {if(judge(ch)=0)
  {switch(compare(t.top,ch))
    {case'<':t.push(ch);
             ch=getchar();break;  
     case'=':t.pop(ch);
             ch=getchar();break;
     case'>':Stack_operand &a,&b;
             Stack_operate &op;
             d.top(&a);
             d.top(&b);
             t.top(&op);
             double v,&u=v;
            
             v=execute(a,op,b);
             d.push(&u);
             break;
   
   }  
  }
  else ch=getchar();
 }
}


sum_f=t.top(Stack_operand &item_d);                                                    //若运算符栈是两个#,则直接输出操作数栈栈顶
return sum_f;
}


在while语句中的判断条件总是编译不过去,请问是什么问题!
还有,我的算法可能比较繁琐 请问有没有更好的算法??
搜索更多相关主题的帖子: 计算器 编译 
2010-06-15 15:52
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:20 
对于这个程序,没有谁比你更了解它的细节,所以要改错你自己是最佳的人选。
相信你自己 !

南国利剑
2010-06-15 16:01
my19891115
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-6-15
收藏
得分:0 
回复 2楼 南国利剑
对是对的呀。。。。 这是我学计算机以来第一个程序。感觉真是很费脑筋。。。。以前都没有编过。。。
2010-06-16 20:08
快速回复:求助!!关于C++计算器问题!!!有的地方编译不通
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016289 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved