Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct towar{
  5. char nazwa[30];
  6. int numer;
  7. double cena;
  8. int ilosc;
  9. };
  10.  
  11. double przecena(void);
  12. void wydruk(char *nazwa);
  13. void drukuj(struct towar s);
  14. void dopisz(char *nazwa);
  15.  
  16.  
  17. int main(){
  18. FILE *plik;
  19. plik = fopen("towar3.txt", "w");
  20. dopisz(plik);
  21. przecena();//?
  22. plik = fopen("towar3.txt","r");
  23. wydruk(plik);
  24.  
  25. }
  26.  
  27. void dopisz(char *nazwa)
  28. {
  29. struct towar s;
  30. printf("podaj nazwe: ");
  31. scanf("%s", s.nazwa);
  32. printf("\npodaj numer: ");
  33. scanf("%d", &s.numer);
  34. printf("\npodaj cene: ");
  35. scanf("%lf", &s.cena);
  36. printf("\npodaj ilosc: ");
  37. scanf("%d", &s.ilosc);
  38. fwrite(&s, sizeof(struct towar),1,nazwa);
  39. fclose(nazwa);
  40. }
  41.  
  42. double przecena(void){
  43. struct towar s;
  44. char znak;
  45. double procent, rozmiar, straty, cena;
  46. int ilosc=0;
  47. FILE *plik;
  48. plik = fopen("towar3.txt", "r+");
  49. fread(&s, sizeof(struct towar), 1, plik);
  50. rozmiar=ftell(plik);
  51. rewind(plik);
  52. while(fread(&s, sizeof(struct towar), 1, plik)==1){
  53. ilosc++;
  54. printf("przecenic?(t) ");
  55. znak=getche();
  56. if(znak=='t'){
  57. cena=s.cena;
  58. printf("\no ile procent? ");
  59. fflush(stdin);
  60. scanf("%lf", &procent);
  61. straty=((s.cena)*(procent)/100);
  62. printf("\nStraty: %lf", straty);
  63. s.cena=s.cena*(1-procent/100);
  64. fseek(plik, (rozmiar*(ilosc-1)), SEEK_SET);
  65. fwrite(&s, sizeof(struct towar), 1, plik);
  66. fseek(plik, (rozmiar*ilosc), SEEK_SET);
  67. }
  68. }
  69. rewind(plik);
  70. FILE *plik2;
  71. plik2 = fopen("towar32.txt", "w");
  72. while(fread(&s, sizeof(struct towar), 1, plik)){
  73. if(s.cena!=0){
  74. fwrite(&s, sizeof(struct towar), 1, plik2);
  75. }
  76. }
  77. fclose(plik);
  78. fclose(plik2);
  79. remove("towar3.txt");
  80. rename("towar32.txt", "towar3.txt");
  81. return 1;
  82. }
  83.  
  84. void drukuj(struct towar s)
  85. {
  86. printf("\nnazwa: %s", s.nazwa);
  87. printf("\nnr: %d", s.numer);
  88. printf("\ncena: %lf", s.cena);
  89. printf("\nilosc: %d", s.ilosc);
  90. }
  91.  
  92. void wydruk(char *nazwa)
  93. {
  94. struct towar s;
  95. while(fread(&s, sizeof(struct towar),1,nazwa)==1)
  96. drukuj(s);
  97. fclose(nazwa);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement