注册 登录
编程论坛 C++教室

一个超级简单的计算器

lionmusicyj 发布于 2008-10-18 10:42, 1692 次点击
老师叫敲一个计算器的程序出来,昨晚写的,各位给点意见哈~!欢迎拍砖~!
#include <iostream>
using namespace std;
int main()
{
  void handle(char,double&,double&,double&);
  //double  f(double& a);阶乘函数
  double a,b,c=0.0;                                      //c表示上次运算的结果
  bool n=true;                                  //控制循环是否继续
  char hd;                                      //告诉系统要做什么
  while(n)
  {
     cout<<"*****************************上次运算的结果为 "<<c <<"*************************"<<endl;
     cout<<"请选择您需要的操作:"<<endl;
     cout<<"1.执行加运算"<<endl;
     cout<<"2.执行减运算"<<endl;
     cout<<"3.执行乘运算"<<endl;
     cout<<"4.执行除运算"<<endl;
     cout<<"5.执行a的b次方运算"<<endl;
     cout<<"0.退出程序"<<endl;
     cin>>hd;
     if(hd=='0') break;
     cout<<"请输入数据:"<<endl;
          cin>>a>>b;               
     handle(hd,a,b,c);
  }
}
void handle(char hd,double& a,double& b,double& c)
{
     if(hd=='1')                                        //加
      {
          c=a+b;                                      
          system("cls");                               //清理屏幕                                
      }
     if(hd=='2')                                       //减
      {
          c=a-b;
          system("cls");
      }
      if(hd=='3')                                      //乘
      {
          c=a*b;
          system("cls");
      }
      if(hd=='4')                                      //除
      {
          c=a/b;
          system("cls");
      }
      if(hd=='5')                                      //a的b次方
      {
          c=a;
          for(int i=1;i<b;i++)
          c*=a;
          system("cls");
      }
      
     
}
6 回复
#2
shdlshmm2008-10-18 12:19
int main()
{
  void handle(char,double&,double&,double&);
  //double  f(double& a);阶乘函数
  double a,b,c=0.0;                                      //c表示上次运算的结果
  bool n=true;                                  //控制循环是否继续
  char hd;                                      //告诉系统要做什么
  while(n)
  {
     cout<<"*****************************上次运算的结果为 "<<c <<"*************************"<<endl;
     cout<<"请选择您需要的操作:"<<endl;
     cout<<"1.执行加运算"<<endl;
     cout<<"2.执行减运算"<<endl;
     cout<<"3.执行乘运算"<<endl;
     cout<<"4.执行除运算"<<endl;
     cout<<"5.执行a的b次方运算"<<endl;
     cout<<"0.退出程序"<<endl;
     cin>>hd;
     if(hd=='0') break;
     cout<<"请输入数据:"<<endl;
          cin>>a>>b;               
     handle(hd,a,b,c);
  }
return 0;  // 在这里加上返回值
}
#3
zxwangyun2008-10-18 12:51
执行除运算时应判断除数是否为零
#4
lockhawk2008-10-18 18:28
万一在输入hd=6。会有什么结果??ha=7呢?
#5
happycplusplus2008-10-18 23:43
我不喜欢用太多if  我用switch帮你写了个,代码如下:
#include <iostream.h>
#include <stdlib.h>
double cf(double x,double n)
{
    double a=1;
    for(int i=1;i<=n;i++)
        a*=x;
        return a;
}

void main()
{
  double a,b,c;                                 
  int hd;                                    
  do
  {
     
     cout<<"\t\t请选择您需要的操作:"<<endl;
     cout<<"\t**********************************"<<endl;
     cout<<"\t1.执行加运算"<<endl;
     cout<<"\t2.执行减运算"<<endl;
     cout<<"\t3.执行乘运算"<<endl;
     cout<<"\t4.执行除运算"<<endl;
     cout<<"\t5.执行a的b次方运算"<<endl;
     cout<<"\t0.退出程序"<<endl<<endl;
     cout<<"\t请选择序号:";
     cin>>hd;
    
     switch(hd)
     {
         case 1:
              cout<<"\t请输入数据(以空格分开):";
              cin>>a;cin>>b;            
              c=a+b;                                      
               cout<<"\t运算的结果为 "<<c<<endl;        
             break;
         case 2:
              cout<<"\t请输入数据(以空格分开):";
              cin>>a>>b;            
              c=a-b;
              cout<<"\t运算的结果为 "<<c<<endl;         
             break;
         case 3:
              cout<<"\t请输入数据(以空格分开):";
              cin>>a>>b;     
              c=a*b;
              cout<<"\t运算的结果为 "<<c<<endl;         
             break;
         case 4:
              cout<<"\t请输入数据(以空格分开):";
              cin>>a>>b;
              if(b==0||a==0)
              {
                  cout<<"\t被除数或除数等于0,计算没有意义,请重新输入!"<<endl;
              }
              else
              {
                c=a/b;
                cout<<"\t运算的结果为 "<<c<<endl;
              }     
             break;
         case 5:
              cout<<"\t请输入数据(以空格分开):";
              cin>>a>>b;     
              c=cf(a,b);
              cout<<"\t运算的结果为 "<<c<<endl;
             break;
         case 0:
             break;
         default:
                cout<<"\t选择错误!请重新选择!"<<endl;
                break;
     }
            system("pause");
            system("cls");
  }while(1);
}

[[it] 本帖最后由 happycplusplus 于 2008-10-18 23:55 编辑 [/it]]
#6
lionmusicyj2008-10-19 13:01
呵呵~!
小弟的这个程序已经改了,其实我也觉得HD应该用switch,不要的话不好控制非法输入~!
谢谢大家的宝贵意见哈~!
#7
zj7041025182009-12-29 10:26
hao
1