模拟计算器(MFC)实现

忙了一天终于搞定了。

 

 

 

 

以下为算法类封装

 

//  calDlg.cpp : implementation file
//
// author: baker
// email:baker1203@sina.com
// course designing for data structures and program design in c++
// time:16/5/2006
/*
                         a simulant calculator
description: requiring design a simulant calculator ,which could take operations such as +,-,*,/ ,( ),or functon SQR ,ABS
condition:inputted expression can limit into int type.therefore ,you must check the expression for exactness.if the errors
take place ,the program would give an alarm.


*/


class  compute
{
public:
 
void init(string &);
 
void error(int);
 
void cal(string &);
    
bool is_optr(char);
 
double operat(double,char,double);
 
char precede(char,char);
 
char* get_value(string);

private:
 CString str;
 
char value[50];

 
}
;
void   compute::init( string &  s)      // ³õʼ»¯ÊäÈë±í´ïʽ. 
   {
  
  s
+="#";                      //SÒÔ#½áÊø 
  }
 
 

void  compute::error( int  tag)     // ±¨´íº¯Êý 

 
     
switch(tag)
     
{
                
case 1:
        str
="ÊäÈë´íÎóµÄ×Ö·û£¡ÇëÖØÐÂÊäÈë";
    
                     
break;
                
case 2:
                     str
="ÊäÈë´íÎóµÄ²Ù×÷Êý£¡ÇëÖØÐÂÊäÈ룡";
     
                     
break;

                     
     }

  strcpy(value,(
char *)(const char*)str);

   
     
}
  
    
bool  compute::is_optr( char  c)      // ¼ì²éÊäÈëÊÇ·ñΪ ÔËËã·û 
   {  
 
  
string   optr_string("+-*/()AS#");   
  
for(int   i=0;i<optr_string.size();i++)   
  
if(c==optr_string[i])   
  
return   true;   
  
return   false;   
  }
   
    
char  compute:: precede( char    op1,    char    op2)   
  
{   
  
string   tab[9];   
  tab[
0]=">><<<><<>";   
  tab[
1]=">><<<><<>";   
  tab[
2]=">>>><><<>";   
  tab[
3]=">>>><><<>";   
  tab[
4]="<<<<<=<<E";   
  tab[
5]=">>>>E>>>>";   
  tab[
6]=">>>><>>>>";
  tab[
7]=">>>><>>>>"
  tab[
8]="<<<<<E<<=";
  
string optr_string("+-*/()AS#");   
    
  
int op1_loc,op2_loc;   
    
  
for(op1_loc=0;op1_loc<optr_string.size();op1_loc++)   
        
if(optr_string[op1_loc]==op1)break;   
  
for(op2_loc=0;op2_loc<optr_string.size();op2_loc++)
        
if(optr_string[op2_loc]==op2)break;   
    
  
return   tab[op1_loc][op2_loc];   
  }
   
    
double  compute::operat( double  x, char  op, double  y)   
  
{   
  
switch(op)  
   
{   
  
case   '+':   return   x+y;   break;   
  
case   '-':   return   x-y;   break;   
  
case   '*':   return   x*y;   break;   
  
case   '/':   return   x/y;   break;  
  
case   'A':   return  fabs(y);break;
  
case   'S':   return  sqrt(y);break
  }
   
  
return   -1;   
  }
   
    
    
    
    
    
void    compute::cal( string &    s)   
  
{   
  stack
<char>optr;   
  optr.push(
'#');   
  stack
<double>opnd;  
  
  s
=s+"#";
    
  
char   c=s[0];
  s.erase(
0,1);   
    
  
while(c!='#'||optr.top()!='#')   
  
{   
  
if(!is_optr(c)) 
    
{  
      
if(c>='0'&&c<='9')
      

      
string   num;   
    
      num.insert(num.begin(),c);   
      
int loc=0;   
      
while(!is_optr(s[loc]))   
      loc
++;   
    
      
string num2(s,0,loc);   
      num
+=num2;   
      s.erase(
0,loc);   
    
     
double   x=atof(num.c_str());   
     opnd.push(x);   
     c
=s[0];   
     s.erase(
0,1);
     }

     
else { error(2); return; }   
   }
   
  
else  
  
{   
       
switch(precede(optr.top(),c))  
        
{   
    
case   '<':   optr.push(c);   c=s[0];   s.erase(0,1);    break;   
    
case   '=':   optr.pop();    c=s[0];    s.erase(0,1);   break;   
    
case   '>':   
           
char   op;   
           op
=optr.top();  
           optr.pop();   
           
double   a,b;   
           a
=opnd.top();   
           opnd.pop();   
          
if(op!='S'&&op!='A')
          
{
           b
=opnd.top();   
           opnd.pop();
           }
   
    
           
double   res;   
           res
=operat(b,op,a);   
           opnd.push(res);   
           
break;  
     
case  'E':
           error(
1);
     
return ;
           
        }
   
   }
   
  }
   
    

  sprintf(value,  
"%f", opnd.top()); 
}
   
    
char *   compute::get_value( string  va)
{
   
    cal(va);  
 
 
return value;
    loc
=0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值