Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct towar{
- char nazwa[30];
- int numer;
- double cena;
- int ilosc;
- };
- double przecena(void);
- void wydruk(char *nazwa);
- void drukuj(struct towar s);
- void dopisz(char *nazwa);
- int main(){
- FILE *plik;
- plik = fopen("towar3.txt", "w");
- dopisz(plik);
- przecena();//?
- plik = fopen("towar3.txt","r");
- wydruk(plik);
- }
- void dopisz(char *nazwa)
- {
- struct towar s;
- printf("podaj nazwe: ");
- scanf("%s", s.nazwa);
- printf("\npodaj numer: ");
- scanf("%d", &s.numer);
- printf("\npodaj cene: ");
- scanf("%lf", &s.cena);
- printf("\npodaj ilosc: ");
- scanf("%d", &s.ilosc);
- fwrite(&s, sizeof(struct towar),1,nazwa);
- fclose(nazwa);
- }
- double przecena(void){
- struct towar s;
- char znak;
- double procent, rozmiar, straty, cena;
- int ilosc=0;
- FILE *plik;
- plik = fopen("towar3.txt", "r+");
- fread(&s, sizeof(struct towar), 1, plik);
- rozmiar=ftell(plik);
- rewind(plik);
- while(fread(&s, sizeof(struct towar), 1, plik)==1){
- ilosc++;
- printf("przecenic?(t) ");
- znak=getche();
- if(znak=='t'){
- cena=s.cena;
- printf("\no ile procent? ");
- fflush(stdin);
- scanf("%lf", &procent);
- straty=((s.cena)*(procent)/100);
- printf("\nStraty: %lf", straty);
- s.cena=s.cena*(1-procent/100);
- fseek(plik, (rozmiar*(ilosc-1)), SEEK_SET);
- fwrite(&s, sizeof(struct towar), 1, plik);
- fseek(plik, (rozmiar*ilosc), SEEK_SET);
- }
- }
- rewind(plik);
- FILE *plik2;
- plik2 = fopen("towar32.txt", "w");
- while(fread(&s, sizeof(struct towar), 1, plik)){
- if(s.cena!=0){
- fwrite(&s, sizeof(struct towar), 1, plik2);
- }
- }
- fclose(plik);
- fclose(plik2);
- remove("towar3.txt");
- rename("towar32.txt", "towar3.txt");
- return 1;
- }
- void drukuj(struct towar s)
- {
- printf("\nnazwa: %s", s.nazwa);
- printf("\nnr: %d", s.numer);
- printf("\ncena: %lf", s.cena);
- printf("\nilosc: %d", s.ilosc);
- }
- void wydruk(char *nazwa)
- {
- struct towar s;
- while(fread(&s, sizeof(struct towar),1,nazwa)==1)
- drukuj(s);
- fclose(nazwa);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement