34+324=358연산자−'-'=>3874-324=3550연산자−'*'=>76*24=1824O"/> 34+324=358연산자−'-'=>3874-324=3550연산자−'*'=>76*24=1824O">

 >  기사  >  백엔드 개발  >  간단한 계산기 C/C++ 프로그램 작성

간단한 계산기 C/C++ 프로그램 작성

王林
王林앞으로
2023-09-02 22:49:12872검색

간단한 계산기 C/C++ 프로그램 작성

간단 계산기는 "+", "-", "*", "/"와 같은 몇 가지 기본 연산을 수행하는 계산기입니다. 계산기는 기본 작업을 빠르게 수행할 수 있습니다. 우리는 계산기를 만들기 위해 스위치 문을 사용할 것입니다.

Example

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;
}

Output

The operator is +
a = 63 b = 7
Sum of63 and 7 is 70

위 내용은 간단한 계산기 C/C++ 프로그램 작성의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제