Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define NCIFRE 1000
- #define STRLNG 1024
- typedef unsigned* punsigned;
- typedef FILE* pfile;
- void main(void)
- {
- punsigned cifre;
- char s[STRLNG];
- pfile add;
- unsigned i, j, c=1, tmp, lastdigit=0, n_addendi=0;
- int k;
- if((add=fopen("addendi.txt","r"))==NULL)
- {
- printf("Errore aperture file");
- goto exiterror;
- }
- if((cifre=(punsigned)malloc(NCIFRE*sizeof(unsigned)))==NULL)
- {
- printf("Errore allocazio memoria");
- goto exiterror;
- }
- for(i=0;i<NCIFRE;i++)
- {
- cifre[i]=0;
- }
- i=0;
- do
- {
- fscanf(add,"%s",s);
- switch(s[0])
- {
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- printf("%d",tmp=(unsigned)(s[0]-'0'));
- cifre[i]+=tmp;
- j=i;
- while(cifre[j]>=10)
- {
- cifre[j++]-=10;
- if(j>NCIFRE)
- {
- printf("\nErrore memoria");
- goto exiterror;
- }
- else
- {
- cifre[j]+=1;
- }
- }
- if(j>lastdigit) lastdigit=j;
- if((++i)>NCIFRE)
- {
- printf("\nErrore memoria");
- goto exiterror;
- }
- break;
- case 'e':
- printf("\n");
- i=0;
- n_addendi++;
- break;
- case 's':
- c=0;
- break;
- }
- }while(c);
- fclose(add);
- printf("\n");
- printf("La somma totale e' pari a: ");
- for(k=lastdigit;k>=0;k--)
- {
- printf("%d",cifre[k]);
- }
- printf("\nAddendi letti: %d\n\n",n_addendi);
- free(cifre);
- exiterror: ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement