Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* link di supporto lettura da file: https://youtu.be/idn-J0Jf_W4?list=PL43F4019E88196A2B */
- /* link di supporto errori lettura file: https://youtu.be/eji_OVpDiuk?list=PL43F4019E88196A2B */
- #include <stdio.h>
- #include <stdlib.h>
- #define N 20
- struct articolo{
- char ope;
- int num;
- float prezzo;
- struct articolo *pun;
- };
- int menu(int);
- void registra(char, int *);
- void carica();
- void calcola(struct articolo *);
- int main()
- {
- int n=0, opz=-1;
- char nome[N];
- printf("Inserire nome attivita': ");
- scanf("%[^\n]", nome); nome[N]='\0';
- while(opz!=0){
- system("cls"); printf("\t%s", nome);
- opz=menu(n);
- switch(opz){
- case 0:{ break;}
- case 1:{ registra('A', &n); break;}
- case 2:{ registra('V', &n); break;}
- case 3:{ carica(); break;}
- default:{ printf("\nOpzione non ammessa!"); getch();}}}
- }
- int menu(int n)
- {
- int opz;
- printf("\n\n1. Carica merce comprata");
- if(n!=0) printf("\n\n2. Scarica merce comprata");
- printf("\n\n3. Calcola resoconto anno fiscale\n\n0. EXIT ");
- scanf("%d", &opz);
- if(n==0 && opz==2) opz=-1;
- return opz;
- }
- void registra(char tipo, int *n)
- {
- struct articolo x;
- FILE *scrivi;
- scrivi=fopen("C:\\Users\\user\\Desktop\\magazzino.txt", "a");
- if(scrivi==NULL) printf("\nErrore nell'apertura\\creazione file");
- else{
- printf("\nInserire quantita' oggetti ");
- scanf("%d", &x.num); getchar();
- if(tipo=='A'){ printf("\nInserire costo articolo ");
- scanf("%f", &x.prezzo); (*n)+=x.num;}
- else{ x.prezzo=0; (*n)-=x.num;}
- if(*n<0 && tipo=='V'){
- printf("\nErrore merce esaurita, mancano %d prodotti", -(*n)); getchar();}
- else fprintf(scrivi, "%c\t%3d %.2f\n", tipo, x.num, x.prezzo);
- fclose(scrivi);}
- }
- void carica()
- {
- struct articolo x, *paus, *p=NULL;
- int n=1, tot;
- FILE *scrivi;
- scrivi=fopen("C:\\Users\\user\\Desktop\\magazzino.txt", "r");
- if(scrivi==NULL){ printf("\n Errore nell'apertura del file"); getchar();}
- else{
- while(n>0){
- n=fscanf(scrivi, "%c %d %f\n", &x.ope, &x.num, &x.prezzo);
- if(n<1) printf("\nFine lettura");
- else{
- if(x.ope=='A'){ if(p==NULL){ p=(struct articolo *)malloc(sizeof(struct articolo)); p->pun=NULL;}
- else{ paus=p; p=(struct articolo *)malloc(sizeof(struct articolo)); p->pun=paus;}
- p->ope=x.ope; p->num=x.num; p->prezzo=x.prezzo;}
- else{ tot=x.num; x.prezzo=p->prezzo; while((tot)>0){
- if((tot)<(p->num)){ (p->num)-=tot; tot=0;}
- else{ tot-=(p->num); paus=p->pun; free(p); p=paus;}}}
- printf("\n%s di %3d unita' al prezzo di euro %.2f cad.", (x.ope=='A')?"Acquisto":"Vendita", x.num, x.prezzo);}}
- fclose(scrivi); calcola(p);}
- }
- void calcola(struct articolo *p)
- {
- int n=0;
- float prezzo=0;
- if(p==NULL) printf("\nNessun articolo rimasto in magazzino");
- else while(p!=NULL){ n+=(p->num); prezzo+=(p->num)*(p->prezzo); p=p->pun;}
- printf("\n\nSono rimasti %d prodotti, per un valore di euro %.2f", n, prezzo);
- getchar(); getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement