Soluzione:
#include <iostream>
using namespace std;
int main() {
long long a, b;
cout << “Inserisci due interi (a b): “;
if (!(cin >> a >> b)) {
cout << “Input non valido.\n”;
return 0;
}
if (a == 0 && b == 0) {
cout << “Entrambi sono zero (ne’ positivi ne’ negativi).\n”;
} else if (a == 0 || b == 0) {
if (a == 0) {
cout << “Il primo numero e’ zero; il secondo e’ “
<< (b > 0 ? “positivo” : “negativo”) << “.\n”;
} else {
cout << “Il secondo numero e’ zero; il primo e’ “
<< (a > 0 ? “positivo” : “negativo”) << “.\n”;
}
} else if (a > 0 && b > 0) {
cout << “Entrambi positivi.\n”;
} else if (a < 0 && b < 0) {
cout << “Entrambi negativi.\n”;
} else {
cout << “Uno positivo e l’altro negativo.\n”;
}
return 0;
}
con switch:
#include <iostream>
using namespace std;
// Ritorna: 1 se x>0, 0 se x==0, -1 se x<0
int sgn(long long x) { return (x > 0) – (x < 0); }
int main() {
long long a, b;
cout << “Inserisci due interi (a b): “;
if (!(cin >> a >> b)) {
cout << “Input non valido.\n”;
return 0;
}
// Codifica coppia di segni in {0..8} con (sgn+1)*3 + (sgn+1)
int sa = sgn(a), sb = sgn(b);
int key = (sa + 1) * 3 + (sb + 1);
switch (key) {
case 8: // ( +1 , +1 ) -> entrambi positivi
cout << “Entrambi positivi.\n”;
break;
case 0: // ( -1 , -1 ) -> entrambi negativi
cout << “Entrambi negativi.\n”;
break;
case 6: // ( +1 , -1 )
case 2: // ( -1 , +1 )
cout << “Uno positivo e l’altro negativo.\n”;
break;
// Qualunque caso con almeno uno zero
case 4: // ( 0 , 0 )
cout << “Entrambi zero (ne’ positivi ne’ negativi).\n”;
break;
case 7: // ( +1 , 0 )
cout << “Primo positivo, secondo zero.\n”;
break;
case 5: // ( 0 , +1 )
cout << “Primo zero, secondo positivo.\n”;
break;
case 1: // ( -1 , 0 )
cout << “Primo negativo, secondo zero.\n”;
break;
case 3: // ( 0 , -1 )
cout << “Primo zero, secondo negativo.\n”;
break;
}
return 0;
}