Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <string.h>
  5.  
  6. struct produse_electronice{
  7.     char prod[20], model[20], categ[20];
  8.     int pret;
  9.    
  10.     char reteaua[20], culoare[20];
  11.     int g, buc1;
  12.  
  13.     char tip[20], ram[20];
  14.     int diag, buc2;
  15. };
  16.  
  17. void afisare(struct produse_electronice t[100], int *n) {
  18.     int i;
  19.     for (i = 0; i < *n; i++)
  20.     {
  21.         printf("%s %s %d %s ", t[i].prod, t[i].model, t[i].pret, t[i].categ);
  22.         if (stricmp(t[i].categ, "TELEFOANE") == 0)
  23.             printf("%s %s %d %d\n", t[i].reteaua, t[i].culoare, t[i].g, t[i].buc1);
  24.         else
  25.             printf("%s %s %d %d\n", t[i].tip, t[i].ram, t[i].diag, t[i].buc2);
  26.     }
  27. }
  28.  
  29. int adaugare(struct produse_electronice t[100], int *n) {
  30.     FILE *f;
  31.     if (fopen_s(&f, "stoc.txt", "rt")) {
  32.         printf("Error");
  33.     }
  34.     else
  35.     {
  36.         int sw = 0;
  37.         while (!feof(f))
  38.         {
  39.             sw = 1;
  40.             fscanf(f, "%s", t[*n].prod, _countof(t[*n].prod));
  41.             fscanf(f, "%s", t[*n].model, _countof(t[*n].model));
  42.             fscanf(f, "%d", &t[*n].pret);
  43.             fscanf(f, "%s", t[*n].categ, _countof(t[*n].categ));
  44.             if (stricmp(t[*n].categ, "TELEFOANE") == 0) {
  45.                 fscanf(f, "%s", t[*n].reteaua, _countof(t[*n].reteaua));
  46.                 fscanf(f, "%s", t[*n].culoare, _countof(t[*n].culoare));
  47.                 fscanf(f, "%d", &t[*n].g);
  48.                 fscanf(f, "%d", &t[*n].buc1);
  49.             }
  50.             else
  51.             {
  52.                 fscanf(f, "%s", t[*n].tip, _countof(t[*n].tip));
  53.                 fscanf(f, "%s", t[*n].ram, _countof(t[*n].ram));
  54.                 fscanf(f, "%d", &t[*n].diag);
  55.                 fscanf(f, "%d", &t[*n].buc2);
  56.             }
  57.             (*n)++;
  58.         }
  59.  
  60.         if (sw)
  61.             afisare(t, n);
  62.         fclose(f);
  63.         return sw;
  64.     }
  65.     fclose(f);
  66. }
  67. void salvare(struct produse_electronice t[100], int *n) {
  68.     FILE *g;
  69.     fopen_s(&g, "stoc.txt", "wt");
  70.     int i;
  71.     for (i = 0; i <= (*n); i++) {
  72.         fprintf(g, "%s %s %d %s ", t[i].prod, t[i].model, t[i].pret, t[i].categ);
  73.         if (stricmp(t[i].categ, "TELEFOANE") == 0)
  74.         {
  75.             fprintf(g, "%s %s %d %d", t[i].reteaua, t[i].culoare, t[i].g, t[i].buc1);
  76.             if (i != (*n))
  77.                 fprintf(g, "\n");
  78.         }
  79.         else
  80.         {
  81.             fprintf(g, "%s %s %d %d", t[i].tip, t[i].ram, t[i].diag, t[i].buc2);
  82.             if (i != (*n))
  83.                 fprintf(g, "\n");
  84.         }
  85.     }
  86.     (*n)++;
  87. }
  88. void introducere(struct produse_electronice t[100], int *n) {
  89.     printf("Producator: "); scanf_s("%s", t[*n].prod, _countof(t[*n].prod));
  90.     printf("Model: "); scanf_s("%s", t[*n].model, _countof(t[*n].model));
  91.     printf("Pret: "); scanf_s("%d", &t[*n].pret);
  92.     printf("Categorie: "); scanf_s("%s", t[*n].categ, _countof(t[*n].categ));
  93.     if (stricmp(t[*n].categ, "TELEFOANE") == 0) {
  94.         printf("Reteaua: "); scanf_s("%s", t[*n].reteaua, _countof(t[*n].reteaua));
  95.         printf("Culoarea: "); scanf_s("%s", t[*n].culoare, _countof(t[*n].culoare));
  96.         printf("Greutate: "); scanf_s("%d", &t[*n].g);
  97.         printf("Bucati: "); scanf_s("%d", &t[*n].buc1);
  98.     }
  99.     else
  100.     {
  101.         printf("Tip: "); scanf_s("%s", t[*n].tip, _countof(t[*n].tip));
  102.         printf("Ram: "); scanf_s("%s", t[*n].ram, _countof(t[*n].ram));
  103.         printf("Diagonala: "); scanf_s("%d", &t[*n].diag);
  104.         printf("Bucati: "); scanf_s("%d", &t[*n].buc2);
  105.     }
  106.     salvare(t, n);
  107. }
  108.  
  109. int main() {
  110.     struct produse_electronice t[100];
  111.     int n = 0;
  112.     int opt;
  113.     do {
  114.         system("cls");
  115.         printf("1.Citire+Afisare\n2.Afisare\n3.Introducere+Salvare\n0.Iesire\n");
  116.         printf("Optiune: "); scanf_s("%d", &opt);
  117.         switch (opt) {
  118.         case 1: if (adaugare(t, &n) == 0) printf("Nu exista elemente in fisier");
  119.             system("pause");
  120.             break;
  121.         case 2: afisare(t, &n); system("pause"); break;
  122.         case 3: introducere(t, &n); break;
  123.         default: break;
  124.         }
  125.     } while (opt != 0);
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement