1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <stdio.h>
void purge (void)
{
int c;
while ((c = getchar ()) != '\n' && c != EOF)
{
}
}
int main (void)
{
/* déclaration variable */
int nombre1, nombre2;
char choix;
/* on demande à l'utilisateur 2 entiers */
printf ("choisir deux entiers \n");
scanf ("%d", &nombre1); /*lire l'entiers */
purge ();
scanf ("%d", &nombre2);
purge ();
/* choix de l'opération par l'utilisateur */
printf ("===choix===\n");
printf (" + \n");
printf (" - \n");
printf (" * \n");
printf (" / \n");
scanf ("%c", &choix);
if (choix != '\n')
{
purge ();
}
/* pour chaque cas on effectue l'operation demandé par l'utilisateur */
switch (choix)
{
case '+':
printf ("%d %c %d = %d", nombre1, choix, nombre2, nombre1 + nombre2);
break;
case '-':
printf ("%d %c %d = %d", nombre1, choix, nombre2, nombre1 - nombre2);
break;
case '*':
printf ("%d %c %d = %d", nombre1, choix, nombre2, nombre1 * nombre2);
break;
case '/':
if (nombre2 != 0)
{
printf ("%d %c %d : %d", nombre1, choix, nombre2, nombre1 / nombre2);
}
else
{
printf ("le denominateur doit etre <> 0\n");
}
break;
default:
printf ("erreur\n");
}
return 0;
} |
Partager