Cours:Cin/Cout correction : Différence entre versions
(Page créée avec « <accesscontrol>Acces:Prof</accesscontrol> <source lang=cpp> #include <iostream> using namespace std; #include <stdlib.h> #include <stdio.h> #include <time.h> int main()... ») |
|||
Ligne 1 : | Ligne 1 : | ||
<accesscontrol>Acces:Prof</accesscontrol> | <accesscontrol>Acces:Prof</accesscontrol> | ||
+ | |||
+ | <source lang=cpp> | ||
+ | #include <iostream> | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | double n1,n2; | ||
+ | char operation; | ||
+ | cout <<"saisir nombre 1 : "; | ||
+ | cin >> n1; | ||
+ | cout <<"saisir nombre 2 : "; | ||
+ | cin >> n2; | ||
+ | cout << "choix opération + - * /"; | ||
+ | cin >> operation; | ||
+ | |||
+ | double res; | ||
+ | switch (operation) | ||
+ | { | ||
+ | case '+': res = n1+n2; | ||
+ | break; | ||
+ | case '-': res = n1-n2; | ||
+ | break; | ||
+ | case 'x': | ||
+ | case '*': res = n1*n2; | ||
+ | break; | ||
+ | case '/': res = n1/n2; | ||
+ | break; | ||
+ | default : cout << "mauvais choix"<<endl; | ||
+ | return -1; | ||
+ | } | ||
+ | cout << n1 << operation << n2 << "="<<res<<endl; | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | |||
<source lang=cpp> | <source lang=cpp> |
Version du 24 novembre 2021 à 19:49
Il s’agit d’une page protégée.
#include <iostream>
using namespace std;
int main()
{
double n1,n2;
char operation;
cout <<"saisir nombre 1 : ";
cin >> n1;
cout <<"saisir nombre 2 : ";
cin >> n2;
cout << "choix opération + - * /";
cin >> operation;
double res;
switch (operation)
{
case '+': res = n1+n2;
break;
case '-': res = n1-n2;
break;
case 'x':
case '*': res = n1*n2;
break;
case '/': res = n1/n2;
break;
default : cout << "mauvais choix"<<endl;
return -1;
}
cout << n1 << operation << n2 << "="<<res<<endl;
return 0;
}
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
int val,n;
// initialisation du générateur de nombre aléatoire
srand (time (NULL));
// obtenir une valeur aléatoire comprise entre [0 et RAND_MAX]
val = rand();
// obtenir une valeur aléatoire comprise entre [0 et 9]
val = rand()%20;
do
{
cout << "saisir un nombre entre 0 et 20: ";
cin >> n;
if (n>val) cout << "trop grand !"<<endl;
if (n<val) cout << "trop petit !"<<endl;
} while (n!=val);
cout <<"gagné :-)"<<endl;
return 0;
}