Advertisement
informaticage

Esercizio potenze / radici c++

Mar 16th, 2021
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1.  
  2. #include <cmath> // equivale a math.h #include <math.h>
  3. #include <iostream>
  4.  
  5. #define LIMITE_OPERAZIONI 10
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.   int numero_radici = 0;
  11.   int numero_potenze = 0;
  12.  
  13.   double radici_calcolate[LIMITE_OPERAZIONI];
  14.   double potenze_calcolate[LIMITE_OPERAZIONI];
  15.  
  16.   int scelta_utente;
  17.   cout << "1) Per calcolare la radice quadrata." << endl;
  18.   cout << "2) Per calcolare la potenza" << endl;
  19.   cout << "0) Per uscire dal programma" << endl;
  20.   cout << "Puoi effettuare un limite di 10 volte ogni operazione" << endl;
  21.  
  22.   do {
  23.     cout << "Effettua una scelta: ";
  24.     cin >> scelta_utente;
  25.     // Se non devo uscire
  26.     if (scelta_utente != 0) {
  27.       // O devo fare una radice o una potenza
  28.       if (scelta_utente == 1) {
  29.         // Devo fare una radice quadrata
  30.         if (numero_radici < LIMITE_OPERAZIONI) {
  31.           cout << "Hai a disposizione ancora " << LIMITE_OPERAZIONI - numero_radici << " radici." << endl;
  32.           cout << "Inserire numero di cui calcolare la radice quadrata: ";
  33.           double radicando;
  34.           cin >> radicando;
  35.  
  36.           // Codice che calcola la radice quadrata
  37.           radici_calcolate[numero_radici] = sqrt(radicando);
  38.           numero_radici++;
  39.         } else {
  40.           // Ho superato il limite di operazioni consentite
  41.           cout << "Non puoi più effettuare operazioni di radice quadrata!"
  42.                << endl;
  43.         }
  44.       }
  45.  
  46.       if (scelta_utente == 2) {
  47.         if (numero_potenze < LIMITE_OPERAZIONI) {
  48.           // Devo fare una potenza
  49.           cout << "Hai a disposizione ancora " << LIMITE_OPERAZIONI - numero_potenze << " potenze." << endl;
  50.           cout << "Inserire base ed esponente: ";
  51.           double base, esponente;
  52.           cin >> base >> esponente;
  53.  
  54.           // Codice che calcola la potenza
  55.           potenze_calcolate[numero_potenze] = pow(base, esponente);
  56.           numero_potenze++;
  57.         } else {
  58.           cout << "Non puoi più effettuare operazioni di potenza!" << endl;
  59.         }
  60.       }
  61.     }
  62.   } while (scelta_utente != 0);
  63.  
  64.   cout << "Elenco radici calcolate:" << endl;
  65.   for (int i = 0; i < numero_radici; i++) {
  66.     cout << radici_calcolate[i] << " ";
  67.   }
  68.  
  69.   cout << endl << "Elenco potenze calcolate:" << endl;
  70.   for (int i = 0; i < numero_potenze; i++) {
  71.     cout << potenze_calcolate[i] << " ";
  72.   }
  73.  
  74.   cout << endl << "Grazie per aver utilizzato il programma :D" << endl;
  75.   return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement