卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章17136本站已运行3324

编写一个简单的计算器的C/C++程序

编写一个简单的计算器的C/C++程序

简单计算器是执行一些基本运算的计算器,例如“+”、“-”、“*”、“/”。计算器可以快速完成基本操作。我们将使用 switch 语句来制作一个计算器。

示例

'
Operator − ‘+’ => 34 + 324 = 358
Operator − ‘-’ => 3874 - 324 = 3550
Operator − ‘*’ => 76 * 24 = 1824
Operator − ‘/’ => 645/5 = 129

示例代码

'
#include <iostream<
using namespace std;
int main() {
   char op = '+';
   float a = 63, b= 7;
   // cout << "Enter operator either + or - or * or /: ";
   // cin >> op;
   cout<<"The operator is "<<op<<endl;
   // cout << "Enter two operands: ";
   // cin>> a >> b;
   cout<<"a = "<<a<<" b = "<<b<<endl;
   switch(op) {
      case '+':
         cout <<"Sum of"<<a<<"and"<<b<<"is"<< a+b;
      break;
      case '-':
         cout <<"Difference of"<<a<<"and"<<b<<"is"<<a-b;
      break;
      case '*':
         cout <<"Product of"<<a<<"and"<<b<<"is"<<a*b;
      break;
      case '/':
         cout <<"Division of"<<a<<"and"<<b<<"is"<<a/b;
      break;
      default:
         // If the operator is other than +, -, * or /, error message is shown
         cout << "Error! operator is not correct";
      break;
   }
   return 0;
}

输出

'
The operator is +
a = 63 b = 7
Sum of63 and 7 is 70
卓越飞翔博客
上一篇: 如何在C语言中不使用临时变量交换两个数组的值?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏