34+324=358Opérateur−'-'=>3874-324=3550Opérateur−'*'=>76*24=1824O"/> 34+324=358Opérateur−'-'=>3874-324=3550Opérateur−'*'=>76*24=1824O">

Maison  >  Article  >  développement back-end  >  Écrire un programme de calculatrice simple en C/C++

Écrire un programme de calculatrice simple en C/C++

王林
王林avant
2023-09-02 22:49:12870parcourir

Écrire un programme de calculatrice simple en C/C++

La calculatrice simple est une calculatrice qui effectue certaines opérations de base, telles que "+", "-", "*", "/". La calculatrice peut effectuer rapidement des opérations de base. Nous utiliserons une instruction switch pour créer une calculatrice.

Exemple

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

Exemple de code

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

Sortie

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

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer