Advertisement
Dany1858

inf. Compito 2 appello gennaio (completo)

Jan 29th, 2015
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.28 KB | None | 0 0
  1. /* link di supporto lettura da file: https://youtu.be/idn-J0Jf_W4?list=PL43F4019E88196A2B */
  2. /* link di supporto errori lettura file: https://youtu.be/eji_OVpDiuk?list=PL43F4019E88196A2B */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #define N 20
  7.  
  8. struct articolo{
  9.        char ope;
  10.        int num;
  11.        float prezzo;
  12.        struct articolo *pun;
  13. };
  14.  
  15. int menu(int);
  16. void registra(char, int *);
  17. void carica();
  18. void calcola(struct articolo *);
  19.  
  20.  
  21. int main()
  22. {
  23.     int n=0, opz=-1;
  24.     char nome[N];
  25.     printf("Inserire nome attivita':  ");
  26.     scanf("%[^\n]", nome); nome[N]='\0';
  27.    
  28.     while(opz!=0){
  29.         system("cls"); printf("\t%s", nome);
  30.         opz=menu(n);
  31.         switch(opz){
  32.         case 0:{ break;}
  33.         case 1:{ registra('A', &n); break;}
  34.         case 2:{ registra('V', &n); break;}
  35.         case 3:{ carica(); break;}
  36.         default:{ printf("\nOpzione non ammessa!"); getch();}}}
  37. }
  38.  
  39. int menu(int n)
  40. {
  41.     int opz;
  42.     printf("\n\n1. Carica merce comprata");
  43.     if(n!=0) printf("\n\n2. Scarica merce comprata");
  44.     printf("\n\n3. Calcola resoconto anno fiscale\n\n0. EXIT  ");
  45.     scanf("%d", &opz);
  46.     if(n==0 && opz==2) opz=-1;
  47.     return opz;
  48. }
  49.  
  50. void registra(char tipo, int *n)
  51. {
  52.      struct articolo x;
  53.      FILE *scrivi;
  54.      scrivi=fopen("C:\\Users\\user\\Desktop\\magazzino.txt", "a");
  55.      if(scrivi==NULL) printf("\nErrore nell'apertura\\creazione file");
  56.      else{
  57.          printf("\nInserire quantita' oggetti  ");
  58.          scanf("%d", &x.num); getchar();
  59.          if(tipo=='A'){ printf("\nInserire costo articolo ");
  60.              scanf("%f", &x.prezzo); (*n)+=x.num;}
  61.          else{ x.prezzo=0; (*n)-=x.num;}
  62.          if(*n<0 && tipo=='V'){
  63.          printf("\nErrore merce esaurita, mancano %d prodotti", -(*n)); getchar();}
  64.          else fprintf(scrivi, "%c\t%3d %.2f\n", tipo, x.num, x.prezzo);
  65.          fclose(scrivi);}
  66. }
  67.  
  68. void carica()
  69. {
  70.      struct articolo x, *paus, *p=NULL;
  71.      int n=1, tot;
  72.      FILE *scrivi;
  73.      scrivi=fopen("C:\\Users\\user\\Desktop\\magazzino.txt", "r");
  74.      if(scrivi==NULL){ printf("\n Errore nell'apertura del file"); getchar();}
  75.      else{
  76.          while(n>0){
  77.              n=fscanf(scrivi, "%c %d %f\n", &x.ope, &x.num, &x.prezzo);
  78.              if(n<1) printf("\nFine lettura");
  79.              else{
  80.                  if(x.ope=='A'){ if(p==NULL){ p=(struct articolo *)malloc(sizeof(struct articolo)); p->pun=NULL;}
  81.                      else{ paus=p; p=(struct articolo *)malloc(sizeof(struct articolo)); p->pun=paus;}
  82.                      p->ope=x.ope; p->num=x.num; p->prezzo=x.prezzo;}
  83.                  else{ tot=x.num; x.prezzo=p->prezzo; while((tot)>0){
  84.                      if((tot)<(p->num)){ (p->num)-=tot; tot=0;}
  85.                      else{ tot-=(p->num); paus=p->pun; free(p); p=paus;}}}
  86.                  printf("\n%s di %3d unita' al prezzo di euro %.2f cad.", (x.ope=='A')?"Acquisto":"Vendita", x.num, x.prezzo);}}
  87.          fclose(scrivi); calcola(p);}
  88. }
  89.  
  90. void calcola(struct articolo *p)
  91. {
  92.      int n=0;
  93.      float prezzo=0;
  94.      if(p==NULL) printf("\nNessun articolo rimasto in magazzino");
  95.      else while(p!=NULL){ n+=(p->num); prezzo+=(p->num)*(p->prezzo); p=p->pun;}
  96.      printf("\n\nSono rimasti %d prodotti, per un valore di euro %.2f", n, prezzo);
  97.      getchar(); getchar();
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement