Advertisement
iannis123

Untitled

Mar 23rd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. typedef struct {
  4. int codp;
  5. char nume[30];
  6. char gen[20];
  7. float pret;
  8. int cant;
  9.  
  10. }CARTI;
  11.  
  12. void creare() {
  13. FILE *f;
  14. char nume[20];
  15. CARTI c;
  16. printf("introduceti numele fisierului:");
  17. gets(nume);
  18. fopen_s(&f, nume, "wb");
  19. if (!f)
  20. printf("Eroare");
  21. else {
  22. printf("Introduceti codul cartii:");
  23. scanf_s("%d", &c.codp);
  24. while (!feof(stdin)) {
  25. getchar();
  26. printf("Introduceti numele cartii:");
  27. gets(c.nume);
  28. printf("Introduceti genul cartii:");
  29. gets(c.gen);
  30. printf("Introduceti pretul cartii:");
  31. scanf_s("%f", &c.pret);
  32. printf("Introduceti numarul de carti:");
  33. scanf_s("%d", &c.cant);
  34. fwrite(&c, sizeof(CARTI), 1, f);
  35. printf("Introduceti codul cartii:");
  36. scanf_s("%d", &c.codp);
  37. }
  38. fclose(f);
  39. }
  40. }
  41. void cheie_unica() {
  42. FILE *f;
  43. char nume[20];
  44. CARTI c;
  45. printf("Introduceti numele fisierului:");
  46. gets(nume);
  47. fopen_s(&f, nume, "rb+");
  48. if (!f)
  49. printf("Eroare");
  50. else {
  51. int codp;
  52. printf("Introduceti codul cartii:");
  53. scanf_s("%d", &codp);
  54. while (!feof(stdin)) {
  55. fseek(f, 0, 0);
  56. int gasit = 0;
  57. fread(&c, sizeof(CARTI), 1, f);
  58. while (!feof(f) && gasit == 0) {
  59. if (c.codp == codp) {
  60. printf("%s %s %f %d\n", c.nume, c.gen, c.pret, c.cant);
  61. c.pret = c.pret + 0.2*c.pret;
  62. fseek(f, ftell(f) - sizeof(CARTI), 0);
  63. fwrite(&c, sizeof(CARTI), 1, f);
  64. gasit = 1;
  65. }
  66. else {
  67. fread(&c, sizeof(CARTI), 1, f);
  68. }
  69. }
  70. if (gasit == 0)
  71. printf("Cartea nu exista\n");
  72. printf("Introduceti codul:");
  73. scanf_s("%d", &codp);
  74. }
  75. fclose(f);
  76. }
  77. }
  78. void selectie() {
  79. FILE *f;
  80. char nume[20];
  81. CARTI c;
  82. printf("Introduceti numele fisierului:");
  83. gets(nume);
  84. fopen_s(&f, nume, "rb+");
  85. if (!f)
  86. printf("Eroare");
  87. else {
  88. FILE *g;
  89. printf("Introduceti numele fisierului text:");
  90. gets(nume);
  91. fopen_s(&g, nume, "w");
  92. fread(&c, sizeof(CARTI), 1, f);
  93. while (!feof(f)) {
  94. if (c.cant > 200)
  95. {
  96. fprintf(g, "%d %s %s %f %d\n", c.codp, c.nume, c.gen, c.pret, c.cant);
  97. c.pret = 0.9*c.pret;
  98. fseek(f, ftell(f) - sizeof(CARTI), 1, f);
  99. fwrite(&c, sizeof(CARTI), 1, f);
  100. fseek(f, 0, 1);
  101.  
  102. }
  103.  
  104. fread(&c, sizeof(CARTI), 1, f);
  105. }
  106. fclose(g);
  107. fclose(f);
  108. }
  109. }
  110. void raport() {
  111. FILE *f;
  112. char nume[20];
  113. CARTI c;
  114. printf("Introduceti numele fisierului:");
  115. gets(nume);
  116. fopen_s(&f, nume, "rb");
  117. if (!f)
  118. printf("Eroare");
  119. else {
  120. FILE *h;
  121. char nume[20];
  122. printf("Introduceti numele fisierului text : ");
  123. gets(nume);
  124. fopen_s(&h, nume, "w");
  125. fprintf(h, "CodProdus Denumire Gen Pret Cantitate\n");
  126. fseek(f, 0, 0);
  127. fread(&c, sizeof(CARTI), 1, f);
  128. while (!feof(f))
  129. {
  130. fprintf(h, "%5d %10s %-5s %-5.2f %-3d\n", c.codp, c.nume, c.gen, c.pret, c.cant);
  131. fread(&c, sizeof(CARTI), 1, f);
  132. }
  133. fclose(h);
  134. fclose(f);
  135. }
  136. }
  137.  
  138.  
  139.  
  140. void main() {
  141. creare();
  142. cheie_unica();
  143. selectie();
  144. raport();
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement