Exercitiul 2 – Evaluarea unor expresii si precizarea tipului lor
Sa se scrie un program care introduce de la tastatura variabilele a,b,c si evalueaza urmatoarele expresii afisand pe ecran rezultatul si tipul lor (intreg, real, boolean, etc):
o a + 2.5;
o a <= b;
o a + c > b;
o b / c;
o a / b + b / a
o (a < 3 || b > 5 || c >= 0)
o (a > b && b > c)
o (!a || ++b)
o a + 8.3;
o a >= b+c;
o a + c <= b-1;
o c / a;
o c / b + a / b
o (a < 8 || b > 3|| c >= 1)
o (a < b && b < c)
o (++a || !b)
Răspunsuri la întrebare
#include <iomanip>
#include <iostream>
using std::cout, std::cin, std::endl, std::boolalpha;
int main() {
int a, b, c;
cout << "Introdu a, b si c (de ex 2 3 4) >> ";
cin >> a >> b >> c;
cout << "a + 2.5 = " << a + 2.5 << ", tipul expresiei >> real " << endl;
cout << boolalpha << "a <= b = " << (a <= b) << ", tipul expresiei >> boolean"
<< endl;
cout << "a + c > b = " << (a + c > b) << ", tipul expresiei >> boolean"
<< endl;
cout << "a / b + b / a = " << a / b + b / a
<< ", tipul expresiei >> intreg ( depinde de tipul variabilelor )"
<< endl;
cout << "b / c = " << b / c
<< ", tipul expresiei >> intreg ( depinde de tipul variabilelor )"
<< endl;
cout << "(a < 3 || b > 5 || c >= 0) = " << (a < 3 || b > 5 || c >= 0)
<< ", tipul expresiei >> boolean" << endl;
cout << "(a > b && b > c) = " << (a > b && b > c)
<< ", tipul expresiei >> boolean" << endl;
cout << "(!a || ++b) = " << (!a || ++b) << ", tipul expresiei >> boolean"
<< endl;
cout << "a + 8.3 = " << a + 8.3 << ", tipul expresiei >> real" << endl;
cout << "a >= b+c = " << (a >= b + c) << ", tipul expresiei >> boolean"
<< endl;
cout << "(a + c <= b-1) = " << (a + c <= b - 1)
<< ", tipul expresiei >> boolean" << endl;
cout << "c / a = " << c / a
<< ", tipul expresiei >> intreg ( depinde de tipul variabilelor )"
<< endl;
cout << "c / b + a / b = " << c / b + a / b
<< ", tipul expresiei >> intreg ( depinde de tipul variabilelor )"
<< endl;
cout << "(a < 8 || b > 3|| c >= 1) = " << (a < 8 || b > 3 || c >= 1)
<< ", tipul expresiei >> boolean" << endl;
cout << "(a < b && b < c) = " << (a < b && b < c)
<< ", tipul expresiei >> boolean" << endl;
cout << "(++a || !b) = " << (++a || !b) << ", tipul expresiei >> boolean"
<< endl;
return 0;
}