Advertisement
Guest User

programma

a guest
Nov 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. #include <stdio.h> // printf
  2. #include <stdlib.h> // malloc, free
  3. #include <string.h> // memset
  4. //#include <math.h> non serve
  5. //#include "FUNZIONI.h"
  6. // typedef nel .h?
  7. typedef enum bool { false, true } bool;
  8.  
  9. void converti_binario(int num, int *a, size_t size){
  10.     for(size_t i = 0; i < size; i++, num >>= 1)
  11.         a[i] = num % 2;
  12. }
  13.  
  14. // set a zero un array
  15. int* pulisci(int *a, size_t size){
  16.     return memset(a, 0, size);
  17.     /*
  18.     for(size_t i = 0; i < size; i++)
  19.         a[i]=0;
  20.     */
  21. }
  22.  
  23. // stampa_array( array, lunghezza array)
  24. // stampa al contrario TODO ???
  25. void stampa_array(int a[], int size){
  26.     for(int i = size - 1; i >= 0; i--)
  27.         printf("%d", a[i]);
  28. }
  29.  
  30. int main(){
  31.     int number; // Non serve = 0 se poi lo sovrascrivi
  32.     int l; // refactor L --> l
  33.     int *v1, *v2;
  34.    
  35.     do{
  36.         printf("Inserisci a quanti bit convertire: ");
  37.         scanf("%d", &l);
  38.         if(l == 0)
  39.             printf("ATTENZIONE BIT NULLI\n");
  40.         else if(l < 0)
  41.             printf("ATTENZIONE IL VALORE E' NEGATIVO\n");
  42.     } while(l <= 0);
  43.    
  44.     // Inizializza dopo aver letto l
  45.     // Non castare la malloc
  46.     // Usa il nome invece del tipo in sizeof
  47.     v1 = /*(int*)*/ malloc(sizeof(/*int*/*v1) * l);
  48.     v2 = malloc(sizeof(*v2) * l);
  49.    
  50.     printf("\nInserisci il numero decimale da convertire: ");
  51.     scanf("%d", &number);
  52.    
  53.     // serve ?
  54.     /*
  55.     pulisci(v1, l);
  56.     pulisci(v2, l);
  57.        
  58.     printf("\nVerifico pulizia di memoria a video:\n");
  59.    
  60.     stampa_array(v1, l);
  61.    
  62.     printf("\n");
  63.    
  64.     stampa_array(v2, l);
  65.    
  66.     printf("\n\n");
  67.     */
  68.     converti_binario(number, v1, l);
  69.     printf("Il numero convertito in binario e':\n");
  70.     stampa_array(v1, l);
  71.    
  72.     free(v1);
  73.     free(v2);    
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement