Advertisement
thevipershowita

EserciziInformaticaC

Mar 11th, 2020
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. //es1
  7. int main() {
  8.     srand(time(NULL));
  9.     int x, min = 10, max = 0;
  10.  
  11.     do {
  12.         x = (rand() % 10);
  13.         if (max < x) {
  14.             max = x;
  15.         }
  16.         if (min > x && x != 0) {
  17.             min = x;
  18.         }
  19.     } while (x != 0);
  20.  
  21.     printf("MIN: %d MAX: %d", min, max);
  22.     return 0;
  23. }
  24.  
  25.  
  26. //es2
  27. int main() {
  28.     srand(time(NULL));
  29.     int x[20], min = 101, k = 0;
  30.     for (int z = 0; z < 20; z++) {
  31.         x[z] = rand() % 100;
  32.         if (min > x[z]) {
  33.             min = x[z];
  34.         }
  35.     }
  36.     for (int r = 0; r < 20; r++) {
  37.         if (x[r] == min)
  38.             k++;
  39.     }
  40.     printf("\nMIN: %d Inserito %d volte", min, k);
  41. }
  42.  
  43. //es 3
  44. int main() {
  45.     srand(time(NULL));
  46.     int x, max = -1;
  47.     for (int i = 0; i < 20; i++) {
  48.         x = (rand() % 99) + 1;
  49.         if (x % 2 == 0) {
  50.             if (max < x) {
  51.                 max = x;
  52.             }
  53.         }
  54.     }
  55.     if (max == -1) {
  56.         printf("NESSUN NUMERO PARI GENERATO.");
  57.     } else {
  58.         printf("Maggiore dei pari: %d", max);
  59.     }
  60. }
  61.  
  62. //es4
  63. int main() {
  64.     srand(time(NULL));
  65.     int randGen = rand() % 999, input = -1, k = 0;
  66.     while (input != randGen && k < 15) {
  67.         printf("Inserisci combinazione:");
  68.         scanf("%d", &input);
  69.         k++;
  70.     }
  71.     if (input != randGen) {
  72.         printf("Hai perso.");
  73.     } else {
  74.         printf("Hai trovato la combinazione! (%d)", randGen);
  75.     }
  76. }
  77.  
  78. //es 5
  79. #define PLAYER "Player"
  80. #define COMPUTER "Computer"
  81. #define PAREGGIO "Pareggio"
  82. #define SASSO "sasso"
  83. #define CARTA "carta"
  84. #define FORBICE "forbici"
  85. #define INVALIDO "invalido"
  86.  
  87. char *generaScelta() {
  88.     srand(time(NULL));
  89.     int choice = (rand() % 2) + 1;
  90.     switch (choice) {
  91.         case 1:
  92.             return SASSO;
  93.         case 2:
  94.             return CARTA;
  95.         case 3:
  96.             return FORBICE;
  97.         default:
  98.             return "";
  99.     };
  100. }
  101.  
  102. char *determinaVincitore(char playerInput[], char computerInput[]) {
  103.     if (strcmp(playerInput, SASSO) == 0) {
  104.         if (strcmp(computerInput, FORBICE) == 0) {
  105.             return PLAYER;
  106.         } else if (strcmp(computerInput, CARTA) == 0) {
  107.             return COMPUTER;
  108.         } else {
  109.             return PAREGGIO;
  110.         }
  111.     } else if (strcmp(playerInput, FORBICE) == 0) {
  112.         if (strcmp(computerInput, CARTA) == 0) {
  113.             return PLAYER;
  114.         } else if (strcmp(computerInput, SASSO) == 0) {
  115.             return COMPUTER;
  116.         } else {
  117.             return PAREGGIO;
  118.         }
  119.     } else if (strcmp(playerInput, CARTA) == 0) {
  120.         if (strcmp(computerInput, SASSO)) {
  121.             return PLAYER;
  122.         } else if (strcmp(computerInput, FORBICE)) {
  123.             return COMPUTER;
  124.         } else {
  125.             return PAREGGIO;
  126.         }
  127.     } else {
  128.         return INVALIDO;
  129.     }
  130. }
  131.  
  132. void punteggio(int *giocatore, int *computer, char *risultato) {
  133.     if (strcmp(risultato, PLAYER) == 0) {
  134.         (*giocatore)++;
  135.     } else if (strcmp(risultato, COMPUTER) == 0) {
  136.         (*computer)++;
  137.     }
  138. }
  139.  
  140. void clearScreen() {
  141.     if (system("CLS"))
  142.         system("clear");
  143. }
  144.  
  145. int main() {
  146.     int playerScore = 0, computerScore = 0;
  147.     char *playerInput = (char *) calloc(50, sizeof(char));
  148.     do {
  149.         printf("Β» Punteggi: [%d-%d]", playerScore, computerScore);
  150.         printf("\nInserisci giocata [sasso|carta|forbici]:\n");
  151.         scanf("%s", playerInput);
  152.         char *input = determinaVincitore(playerInput, generaScelta());
  153.         punteggio(&playerScore, &computerScore, input);
  154.         clearScreen();
  155.     } while (playerScore < 3 && computerScore < 3);
  156.     if (playerScore == 3 && computerScore == 3) {
  157.         printf("Pareggio fra Giocatore e Computer!");
  158.     } else if (playerScore > computerScore) {
  159.         printf("Giocatore , hai vinto!");
  160.     } else {
  161.         printf("Il computer ha vinto!");
  162.     }
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement