一个简单的计算器(c++)

#include <iostream>
#include <cmath>
using namespace std;

class calc   //类名:calc(计算器)
{
private:
  int a,b;
  int p,m;
public:
  calc(){}   //不带参数的构造函数

  void input();
  void input1();
  void input2();
  void input3();
  void input4();
  void input5();
  double input6();

  int jia();
  int jian();
  int mul();
  int div();
  //long pow(); //平方

  void display1(); //加
  void display2(); //减
  void display_mul();
  void display_div();
  int display_pow(int);      //以上均为成员函数
};

//以下是对成员函数的定义
int calc::jia()
{
  //int sum;
  //sum=a+b;
  //return sum;
  return a+b;
}

int calc::jian()
{
  return a-b;
}

int calc::mul()
{
  return a*b;
}

int calc::div()
{
  if(b==0)       //除数不能为0
  {
    cout<<"除数为零!请重新输入..."<<endl;
    cout<<"请输入两个整数:";
    cin>>a>>b;
    return 1;
  }
  else
    return a/b;
}

//long calc::pow()
//{
// return p*p;
//}

void calc::input()
{
  cout<<"请输入两个整数:";
  cin>>a>>b;
}

void calc::input1()
{
  input();
  jia();
}

void calc::input2()
{
  input();
  jian();
}

void calc::input3()
{
  input();
  mul();
}

void calc::input4()
{
  input();
  div();
}
/*
void calc::input5()
{
  input();
  pow();
}
*/
double calc::input6()
{
  cout<<"请输入一个整数,求该数的平方根:";
  cin>>m;
  double q=sqrt(m);
  return q;
}

void calc::display1()
{
  cout<<"两数之和="<<jia()<<endl;
}

void calc::display2()
{
  cout<<"两数之差="<<jian()<<endl;
}

void calc::display_mul()
{
  cout<<"两数之积="<<mul()<<endl;
}

void calc::display_div()
{
  cout<<"两数之商="<<div()<<endl;
}

int calc::display_pow(int q)
{
  return q*q;
}

//菜单
void menu()
{
  cout<<"*********************************"<<endl;
  cout<<endl;
  cout<<" ===简单计算器==="<<endl;
  cout<<endl;
  cout<<" ---1.加---"<<endl;
  cout<<endl;
  cout<<" ---2.减---"<<endl;
  cout<<endl;
  cout<<" ---3.乘---"<<endl;
  cout<<endl;
  cout<<" ---4.除---"<<endl;
  cout<<endl;
  cout<<" ---5.平方---"<<endl;
  cout<<endl;
  cout<<" ---6.平方根---"<<endl;
  cout<<endl;
  cout<<" ---0.退出---"<<endl;
  cout<<endl;
  cout<<"*********************************"<<endl;
}

//退出菜单
void _exit()
{
  cout<<"*********************************"<<endl;
  cout<<endl;
  cout<<" 欢迎使用本系统"<<endl;
  cout<<endl;
  cout<<endl;
  cout<<" ---再见---"<<endl;
  cout<<endl;
  cout<<"*********************************"<<endl;
}

////////////主函数/////////////
int main()
{
  calc add;
  calc sub;
  calc mul;
  calc div;
  calc pow;
  calc sqrt;   //以上用类定义对象

  int c;
  menu(); //调用主菜单

  while(1)
  {
    cout<<"请选择:";
    cin>>c;
    switch(c)
    {
    case 1:
      add.input1();
      add.display1();
      break;
    case 2:
      sub.input2();
      sub.display2();
      break;
    case 3:
      mul.input3();
      mul.display_mul();
      break;
    case 4:
      div.input4();
      div.display_div();
      break;
    case 5:
      //pow.input5();
      int p;
      cout<<"请输入一个正整数:";
      cin>>p;
      cout<<"该数的平方是:"<<pow.display_pow(p)<<endl;
      break;
    case 6:
      cout<<"该数的平方根为:"<<sqrt.input6()<<endl;
      break;
    case 0:
      system("cls");
      _exit();
      exit(1);
      break;
    default:
      cout<<"输入错误!"<<endl;
      break;
    }
  }
  return 1;
}

运行结果:

 

一个简单的计算器(c++)

 

原文链接: https://www.cnblogs.com/duanqibo/p/11111044.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    一个简单的计算器(c++)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/298024

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午7:12
下一篇 2023年2月15日 下午7:12

相关推荐