128 lines
3.5 KiB
C++
128 lines
3.5 KiB
C++
/*
|
|
Autore: Manuel Vichi
|
|
Calcolatrice via Terminale (Basilare)
|
|
*/
|
|
#include <iostream>
|
|
#include <cmath>
|
|
#include <cstdlib>
|
|
using namespace std;
|
|
int main(void) {
|
|
cout << "Benvenuto nella calcolatrice via terminale!" << endl;
|
|
int scelta = -1, operandi = -1;
|
|
float risultato, input, num1 = 0, base = 0, esponente = 0;
|
|
while (scelta < 0 || scelta > 5) {
|
|
cout << "Scegli un opzione:" << endl << endl << "1) Addizione" << endl << "2) Sottrazione" << endl << "3) Moltiplicazione" << endl << "4) Divisione" << endl << "5) Potenza" << endl << "0) Esci" << endl;
|
|
cin >> scelta;
|
|
cout << endl;
|
|
if (scelta < 0 || scelta > 5)
|
|
cerr << "Scelta non valida!!" << endl;
|
|
}
|
|
switch(scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
while (operandi < 2) {
|
|
cout << "Inserisci il numero di addendi: ";
|
|
cin >> operandi;
|
|
cout << endl;
|
|
if (operandi < 2)
|
|
cerr << endl << "Numero di addendi non valido!!" << endl;
|
|
}
|
|
for (int i = 0; i < operandi; i++) {
|
|
cout << "Inserisci l'addendo: ";
|
|
cin >> input;
|
|
cout << endl;
|
|
risultato = risultato + input;
|
|
}
|
|
cout << "La somma e': " << risultato << endl;
|
|
system("pause");
|
|
break;
|
|
case 2:
|
|
while (operandi < 2) {
|
|
cout << "Inserisci il numero di numeri da sottrarre: ";
|
|
cin >> operandi;
|
|
cout << endl;
|
|
if (operandi < 2)
|
|
cerr << endl << "Numero di numeri da sottrarre non valido!!" << endl;
|
|
}
|
|
while (num1 == 0) {
|
|
cout << "Inserisci il minuendo: ";
|
|
cin >> num1;
|
|
cout << endl;
|
|
if (num1 == 0)
|
|
cout << endl << "Il minuendo non può essere 0" << endl;
|
|
}
|
|
risultato = num1;
|
|
for (int i = 0; i < operandi; i++) {
|
|
cout << "Inserisci il numero: ";
|
|
cin >> input;
|
|
cout << endl;
|
|
risultato = risultato - input;
|
|
}
|
|
cout << "La differenza e': " << risultato << endl;
|
|
system("pause");
|
|
break;
|
|
case 3:
|
|
while (operandi < 2) {
|
|
cout << "Inserisci il numero di fattori: ";
|
|
cin >> operandi;
|
|
cout << endl;
|
|
if (operandi < 2)
|
|
cerr << endl << "Numero di fattori non valido!!" << endl;
|
|
}
|
|
risultato = 1;
|
|
for (int i = 0; i < operandi; i++) {
|
|
cout << "Inserisci il fattore: ";
|
|
cin >> input;
|
|
cout << endl;
|
|
risultato = risultato * input;
|
|
}
|
|
cout << "Il prodotto e': " << risultato << endl;
|
|
system("pause");
|
|
break;
|
|
case 4:
|
|
while (operandi < 2) {
|
|
cout << "Inserisci il numero di numeri da dividere: ";
|
|
cin >> operandi;
|
|
cout << endl;
|
|
if (operandi < 2)
|
|
cerr << endl << "Numero di numeri da dividere non valido!!" << endl;
|
|
}
|
|
while (num1 == 0) {
|
|
cout << "Inserisci il dividendo: ";
|
|
cin >> num1;
|
|
cout << endl;
|
|
if (num1 == 0)
|
|
cout << endl << "Il dividendo non può essere 0" << endl;
|
|
}
|
|
risultato = num1;
|
|
for (int i = 0; i < operandi; i++) {
|
|
cout << "Inserisci il numero: ";
|
|
cin >> input;
|
|
cout << endl;
|
|
risultato = risultato / input;
|
|
}
|
|
cout << "La differenza e': " << risultato << endl;
|
|
system("pause");
|
|
break;
|
|
case 5:
|
|
while (base == 0 && esponente == 0) {
|
|
cout << "Inserisci la base: ";
|
|
cin >> base;
|
|
cout << endl;
|
|
cout << "Inserisci l'esponente: ";
|
|
cin >> esponente;
|
|
cout << endl;
|
|
if (base == 0 && esponente == 0)
|
|
cerr << endl << "La base e l'esponente non possono essere entrambi 0!!" << endl;
|
|
}
|
|
risultato = pow(base,esponente);
|
|
cout << "La potenza calcolata e': " << risultato << endl;
|
|
system("pause");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return 0;
|
|
}
|