ホームページ  >  記事  >  バックエンド開発  >  簡単な電卓 C/C++ プログラムを作成する

簡単な電卓 C/C++ プログラムを作成する

王林
王林転載
2023-09-02 22:49:12870ブラウズ

簡単な電卓 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 = &#39;+&#39;;
   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 &#39;+&#39;:
         cout <<"Sum of"<<a<<"and"<<b<<"is"<< a+b;
      break;
      case &#39;-&#39;:
         cout <<"Difference of"<<a<<"and"<<b<<"is"<<a-b;
      break;
      case &#39;*&#39;:
         cout <<"Product of"<<a<<"and"<<b<<"is"<<a*b;
      break;
      case &#39;/&#39;:
         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/C++ プログラムを作成するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。