Advertisement
maha_kaal

Radice Quadrata Intera

Jun 15th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #define DIM 4
  3. #define EXIT 0
  4. #define INSERT_ERROR -1
  5. #define NO_CHAR -2
  6. #define NEG_ESP -3
  7.  
  8. /** funzione elevamento a potenza, per simulare base^esponente. ritorno = 1 * base ==> base * base **/
  9. int pow(int base, int esp){
  10.     int i = 0,
  11.         ritorno = 1;
  12.  
  13.     if(esp < 0)
  14.         return NEG_ESP;
  15.  
  16.     for(i = 0; i < esp; i++){
  17.         ritorno = ritorno * base;
  18.     }
  19.  
  20.     return ritorno;
  21. }
  22.  
  23. /** prende in input un char e ritorna il rispettivo int **/
  24. int convert_char(char c){
  25.     switch(c){
  26.         case '0':
  27.             return 0;
  28.             break;
  29.         case '1':
  30.             return 1;
  31.             break;
  32.         case '2':
  33.             return 2;
  34.             break;
  35.         case '3':
  36.             return 3;
  37.             break;
  38.         case '4':
  39.             return 4;
  40.             break;
  41.         case '5':
  42.             return 5;
  43.             break;
  44.         case '6':
  45.             return 6;
  46.             break;
  47.         case '7':
  48.             return 7;
  49.             break;
  50.         case '8':
  51.             return 8;
  52.             break;
  53.         case '9':
  54.             return 9;
  55.             break;
  56.         default:
  57.             return NO_CHAR;
  58.             break;
  59.     }
  60. }
  61.  
  62. /** prende in input un array di char, calcola la dimensione effettiva dell'array poi utilizzando la combinazione
  63.  delle funzioni convert_char e pow, trasforma l'array di char nell'intero corrispondente**/
  64. int convert_str(char array[]){
  65.     int total = 0,
  66.         i = 0,
  67.         cifra = 0,
  68.         numero = 0,
  69.         potenza = 0;
  70.    
  71. //effettiva dimensione
  72.     for(i = 0; i < DIM+1; i++){
  73.         if(array[i] == '\0'){
  74.             total = i;
  75.             break;
  76.         }
  77.     }
  78. //se non si è inserito alcun numero
  79.     if( total == 0 )
  80.         return INSERT_ERROR;
  81. /* conversione effettiva */
  82.     for(i = 0; i < total; i++){
  83.         cifra = convert_char(array[i]); //estraggo un char alla volta e mediante convert_char arrivo alla cifra intera
  84.         potenza = pow(10, total - 1 - i);//calcolo la posizione in base 10, in quanto tutti i numeri sono espremibili nella forma num * 10^pos, dove pos indica, per numeri > 0, se è unità - decine - centinaia o migliaia
  85.         if(potenza == NEG_ESP){ //se si è dato un esponente negativo
  86.             return NEG_ESP;
  87.         } else {
  88.             numero = numero + (cifra * potenza); //questo mi calcola il numro del quale dovrò calcolare la radice intera
  89.         }
  90.     }
  91.    
  92.     return numero ;
  93. }
  94.    
  95.    
  96. int inserisci(void){
  97.     char input[DIM+1];
  98.     scanf("%s", &input);
  99.  
  100.     if((input[0] == 'f' || input[0] == 'F') &&
  101.        (input[1] == 'i' || input[1] == 'I') &&
  102.        (input[2] == 'n' || input[2] == 'N') &&
  103.        (input[3] == 'e' || input[3] == 'E')){
  104.            return EXIT;
  105.        } else {
  106.            return convert_str(input);
  107.        }
  108. }
  109.  
  110. int radice(int numero){
  111.     int i = 0;
  112.     while(1){
  113.         if(pow(i+1,2) > numero){
  114.             break;
  115.         }
  116.         i++;
  117.     }
  118.  
  119.     return i;
  120. }
  121.    
  122. int main()
  123. {
  124.     int num = 0;
  125.    
  126.     printf("+--------------------------------------------------+\n");
  127.     printf("|-------- R A D I C E ---- Q U A D R A T A --------|\n");
  128.     printf("+--------------------------------------------------+\n");
  129.     while(1){
  130.         printf("Inserire un numero intero di al massimo 4 cifre oppure 'FINE' :>");
  131.         num = inserisci();
  132.         if(num == 0){
  133.             break;
  134.         }
  135.        
  136.         if(num != NEG_ESP){
  137.             printf("La radice quadrata del numero %d e' %d \n", num, radice(num));
  138.         } else {
  139.             printf("numero non valido... ");
  140.         }
  141.     }
  142.  
  143.    
  144.     return (0);
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement