Advertisement
xDefo

Somma numeri

Nov 28th, 2020
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define NCIFRE 1000
  5. #define STRLNG 1024
  6.  
  7. typedef unsigned* punsigned;
  8. typedef FILE* pfile;
  9.  
  10. void main(void)
  11.  {
  12.    punsigned cifre;
  13.    char      s[STRLNG];
  14.    pfile     add;
  15.    unsigned  i, j, c=1, tmp, lastdigit=0, n_addendi=0;
  16.    int       k;
  17.    
  18.    if((add=fopen("addendi.txt","r"))==NULL)
  19.     {
  20.       printf("Errore aperture file");
  21.       goto exiterror;
  22.     }
  23.    
  24.    if((cifre=(punsigned)malloc(NCIFRE*sizeof(unsigned)))==NULL)
  25.     {
  26.      printf("Errore allocazio memoria");
  27.      goto exiterror;
  28.     }
  29.    
  30.    for(i=0;i<NCIFRE;i++)
  31.     {
  32.      cifre[i]=0;
  33.     }
  34.    i=0;
  35.    do
  36.     {
  37.      fscanf(add,"%s",s);
  38.      switch(s[0])
  39.       {
  40.        case '1':
  41.        case '2':
  42.        case '3':
  43.        case '4':
  44.        case '5':
  45.        case '6':
  46.        case '7':
  47.        case '8':
  48.        case '9':
  49.                 printf("%d",tmp=(unsigned)(s[0]-'0'));
  50.                 cifre[i]+=tmp;
  51.                 j=i;
  52.                 while(cifre[j]>=10)
  53.                  {
  54.                   cifre[j++]-=10;
  55.                   if(j>NCIFRE)
  56.                    {
  57.                     printf("\nErrore memoria");
  58.                     goto exiterror;
  59.                    }
  60.                   else
  61.                    {
  62.                     cifre[j]+=1;
  63.                    }
  64.                  }
  65.                 if(j>lastdigit) lastdigit=j;
  66.                 if((++i)>NCIFRE)
  67.                  {
  68.                   printf("\nErrore memoria");
  69.                   goto exiterror;
  70.                  }
  71.                 break;
  72.        case 'e':
  73.                 printf("\n");
  74.                 i=0;
  75.                 n_addendi++;
  76.                 break;
  77.        case 's':
  78.                 c=0;
  79.                 break;
  80.       }
  81.     }while(c);
  82.    
  83.     fclose(add);
  84.    
  85.     printf("\n");
  86.     printf("La somma totale e' pari a: ");
  87.     for(k=lastdigit;k>=0;k--)
  88.      {
  89.       printf("%d",cifre[k]);
  90.      }
  91.     printf("\nAddendi letti: %d\n\n",n_addendi);
  92.    
  93.     free(cifre);
  94.    
  95.     exiterror: ;
  96.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement