Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //struktura z danymi klienta
- typedef struct DANE
- {
- char *nazwa;
- char *adres;
- int *id;
- char *kontakt;
- }dane;
- //struktura z lista
- typedef struct KLIENT klient;
- struct KLIENT
- {
- dane informacje;
- klient *nast;
- };
- // struktura dotyczaca transakcji
- typedef struct TRANSAKCJE
- {
- char *nazwa_tr;
- int *ilosc_zamowionego_towaru;
- int *kwota;
- int *id_klienta;
- }transakcje;
- typedef struct TR tr;
- struct TR
- {
- transakcje trans;
- tr *next;
- };
- //zapisanie do pliku
- void zapisz_do_pliku(klient *head)
- {
- FILE *plik1=NULL;
- klient *tmp;
- if(head==NULL)
- printf("Nie ma elementow do zapisania w pliku");
- else
- {
- plik1=fopen("lista_klientow.txt", "w");
- if(plik1==NULL)
- printf("Wystapil blad");
- else
- {
- tmp=head;
- while(tmp!=NULL)
- {
- fprintf(plik1,"id klienta %d\n",tmp->informacje.id);
- fprintf(plik1, "nazwa: %s\n", tmp->informacje.nazwa);
- fprintf(plik1, "adres: %s\n", tmp->informacje.adres);
- fprintf(plik1, "kontakt: %s\n", tmp->informacje.kontakt);
- tmp = tmp->nast;
- fprintf(plik1, "----------------\n");
- }
- }
- fclose(plik1);
- }
- }
- //zapisanie do pliku
- void zapisz_do_pliku_tr(tr *head1)
- {
- FILE *plik2=NULL;
- tr *tmp;
- if(head1==NULL)
- printf("Nie ma elementow do zapisania w pliku");
- else
- {
- plik2=fopen("lista_transakcji.txt", "w");
- if(plik2==NULL)
- printf("Wystapil blad");
- else
- {
- tmp=head1;
- while(tmp!=NULL)
- {
- fprintf(plik2, "nazwa towaru: %s\n", tmp->trans.nazwa_tr);
- fprintf(plik2, "ilosc: %d\n", tmp->trans.ilosc_zamowionego_towaru);
- fprintf(plik2, "kwota: %d\n", tmp->trans.kwota);
- fprintf(plik2, "id klienta %d\n", tmp->trans.id_klienta);
- tmp = tmp->next;
- fprintf(plik2,"-----------------\n");
- }
- }
- fclose(plik2);
- }
- }
- transakcje* nowa_tr()
- {
- tr *nowy;
- char bufor[30+1];
- char *nazwa_tr;
- int ilosc_zamowionego_towaru,kwota,id_klienta;
- system("cls");
- printf("Podaj nazwe towaru \n");
- gets(bufor);
- nazwa_tr = (char*)malloc(sizeof(char)* (strlen(bufor) + 1));
- strcpy(nazwa_tr, bufor);
- printf("Podaj ilosc zamowionego towaru\n");
- scanf("%d",&ilosc_zamowionego_towaru);
- printf("Podaj kwote\n");
- scanf("%d", &kwota);
- printf("Podaj id klienta\n");
- scanf("%d", &id_klienta);
- nowy=(tr*)malloc(sizeof(tr));
- nowy->next=NULL;
- nowy->trans.nazwa_tr=nazwa_tr;
- nowy->trans.ilosc_zamowionego_towaru=ilosc_zamowionego_towaru;
- nowy->trans.kwota=kwota;
- nowy->trans.id_klienta=id_klienta;
- return nowy;
- }
- //funkcja tworzaca nowy element listy
- int id=1;
- klient* nowy_element()
- {
- klient *nowy;
- char bufor[30+1];
- char *nazwa, *adres;
- char *kontakt;
- system("cls");
- printf("Podaj nazwe klienta \n");
- gets(bufor);
- nazwa = (char*)malloc(sizeof(char)* (strlen(bufor) + 1));
- strcpy(nazwa, bufor);
- printf("Podaj adres klienta\n");
- gets(bufor);
- adres = (char*)malloc(sizeof(char)* (strlen(bufor) + 1));
- strcpy(adres,bufor);
- printf("Podaj numer telefonu klienta\n");
- gets(bufor);
- kontakt = (char*)malloc(sizeof(char)* (strlen(bufor) + 1));
- strcpy(kontakt,bufor);
- nowy=(klient*)malloc(sizeof(klient));
- nowy->nast=NULL;
- nowy->informacje.id=id;
- nowy->informacje.nazwa=nazwa;
- nowy->informacje.adres=adres;
- nowy->informacje.kontakt=kontakt;
- id++;
- return nowy;
- }
- void dodaj_na_koniec_tr(tr **head1)
- {
- tr *pom, *tmp=nowa_tr();
- if (*head1 == NULL)
- *head1 = tmp;
- else
- {
- pom = *head1;
- while (pom->next != NULL)
- pom = pom->next;
- pom->next = tmp;
- }
- }
- //dodawanie na koniec listy
- void dodaj_na_koniec(klient **head)
- {
- klient *pom, *tmp=nowy_element();
- if (*head == NULL)
- *head = tmp;
- else
- {
- pom = *head;
- while (pom->nast != NULL)
- pom = pom->nast;
- pom->nast = tmp;
- }
- }
- //wyswietlanie calej listy
- void wyswietl(klient *head, tr *head1)
- {
- klient *pom;
- tr *tmp;
- system("cls");
- pom=head;
- tmp=head1;
- if(pom==NULL || tmp==NULL)
- printf("nie mozna polaczyc elementow\n");
- else
- {
- while(pom!=NULL)
- {
- while(tmp!=NULL)
- {
- if(tmp->trans.id_klienta==pom->informacje.id)
- printf("nazwa: %s\n adres: %s\n kontak %s\n nazwa towaru %s\n ilosc %d\n kwota %d\n", pom->informacje.nazwa, pom->informacje.adres,
- pom->informacje.kontakt, tmp->trans.nazwa_tr,tmp->trans.ilosc_zamowionego_towaru,tmp->trans.kwota);
- tmp=tmp->next;
- printf("--------------------\n");
- }
- pom=pom->nast;
- tmp=head1;
- }
- getchar();
- }
- }
- void wyswietlanie_listy(klient *head)
- {
- klient *tmp;
- system("cls");
- tmp = head;
- if (tmp == NULL)
- printf("Brak elementow");
- else
- {
- while (tmp != NULL)
- {
- printf("id: %d\n nazwa: %s \nadres: %s \nkontakt: %s\n", tmp->informacje.id, tmp->informacje.nazwa,
- tmp->informacje.adres, tmp->informacje.kontakt);
- printf("-----------------\n");
- tmp = tmp->nast;
- }
- }
- getchar();
- }
- //funkcja wyswietlajaca liste transakcji
- void wyswietlanie_tr(tr *head1)
- {
- tr *tmp;
- system("cls");
- tmp = head1;
- if (tmp == NULL)
- printf("Brak elementow");
- else
- {
- while (tmp != NULL)
- {
- printf("nazwa: %s \nilosc: %d\n cena: %d\n", tmp->trans.nazwa_tr, tmp->trans.ilosc_zamowionego_towaru,
- tmp->trans.kwota);
- printf("-------------------------\n");
- tmp = tmp->next;
- }
- }
- getchar();
- }
- //wyszukiwanie klienta
- klient * wyszukaj_element(klient* head, char *nazwa)
- {
- klient *pom;
- pom = head;
- while (pom && strcmp(pom->informacje.nazwa, nazwa)!=0 )
- pom = pom->nast;
- return pom;
- }
- //wyszukiwanie transakcji
- tr *wyszukaj(tr* head1, char *nazwa_tr)
- {
- tr *pom;
- pom=head1;
- while(pom && strcmp(pom->trans.nazwa_tr,nazwa_tr)!=0)
- pom=pom->next;
- return pom;
- }
- //Usuwanie wybranego elementu
- void usuwanieklienta(klient **head)
- {
- klient *tmp, *pom;
- char *nazwa;
- char bufor[30 + 1];
- system("cls");
- printf("Podaj nazwe klienta: \n");
- gets(bufor);
- nazwa = (char*)malloc(sizeof(char)* (strlen(bufor) + 1));
- strcpy(nazwa, bufor);
- tmp = wyszukaj_element(*head, nazwa);
- if (tmp == NULL)
- printf("Nie ma takiej osoby\n");
- else
- {
- if (tmp == *head)
- {
- *head = (*head)->nast;
- if(tmp->informacje.id)
- free(tmp->informacje.id);
- if (tmp->informacje.nazwa)
- free(tmp->informacje.nazwa);
- if (tmp->informacje.adres)
- free(tmp->informacje.adres);
- if(tmp->informacje.kontakt)
- free(tmp->informacje.kontakt);
- free(tmp);
- }
- else
- {
- pom = *head;
- while ((strcmp(pom->nast->informacje.nazwa, tmp->informacje.nazwa) != 0))
- pom = pom->nast;
- pom->nast = tmp->nast;
- if(tmp->informacje.id)
- free(tmp->informacje.id);
- if (tmp->informacje.nazwa)
- free(tmp->informacje.nazwa);
- if (tmp->informacje.adres)
- free(tmp->informacje.adres);
- if(tmp->informacje.kontakt)
- free(tmp->informacje.kontakt);
- free(tmp);
- }
- }
- }
- void usuwanietr(tr **head1)
- {
- tr *tmp, *pom;
- char *nazwa;
- char bufor[30 + 1];
- system("cls");
- printf("Podaj nazwe towaru: \n");
- gets(bufor);
- nazwa = (char*)malloc(sizeof(char)* (strlen(bufor) + 1));
- strcpy(nazwa, bufor);
- tmp = wyszukaj_element(*head1, nazwa);
- if (tmp == NULL)
- printf("Nie ma takiej transakcji\n");
- else
- {
- if (tmp == *head1)
- {
- *head1 = (*head1)->next;
- if(tmp->trans.nazwa_tr)
- free(tmp->trans.nazwa_tr);
- if (tmp->trans.ilosc_zamowionego_towaru)
- free(tmp->trans.ilosc_zamowionego_towaru);
- if (tmp->trans.kwota)
- free(tmp->trans.kwota);
- if(tmp->trans.id_klienta)
- free(tmp->trans.id_klienta);
- free(tmp);
- }
- else
- {
- pom = *head1;
- while ((strcmp(pom->next->trans.nazwa_tr, tmp->trans.nazwa_tr) != 0))
- pom = pom->next;
- pom->next = tmp->next;
- if(tmp->trans.nazwa_tr)
- free(tmp->trans.nazwa_tr);
- if (tmp->trans.ilosc_zamowionego_towaru)
- free(tmp->trans.ilosc_zamowionego_towaru);
- if (tmp->trans.kwota)
- free(tmp->trans.kwota);
- if(tmp->trans.id_klienta)
- free(tmp->trans.id_klienta);
- free(tmp);
- }
- }
- }
- //Usuwanie wybranego elementu
- //funkcja glowna zawierajaca menu
- int main()
- { klient *head=NULL;
- tr *head1=NULL;
- char wybor;
- do{
- system("cls");
- printf("1. dodaj osobe do listy \n");
- printf("2. wyswietl liste osob\n");
- printf("3. usun wybranego przez siebie klienta\n");
- printf("4. dodaj nowa transakcje\n");
- printf("5. wyswietl obie listy\n");
- printf("6.wyswietl liste transakcji\n");
- printf("7. usun wybrana transakcje\n");
- printf("00. koniec programu\n");
- wybor=getchar();
- getchar();
- if(wybor=='1')
- {dodaj_na_koniec(&head);
- zapisz_do_pliku(head);}
- else if(wybor=='2') wyswietlanie_listy(head);
- else if(wybor=='3')
- {usuwanieklienta(&head);
- zapisz_do_pliku(head);}
- else if(wybor=='4')
- {
- dodaj_na_koniec_tr(&head1);
- zapisz_do_pliku_tr(head1);
- }
- else if(wybor=='5') wyswietl(head, head1);
- else if(wybor=='6') wyswietlanie_tr(head1);
- else if(wybor=='7')
- {
- usuwanietr(&head1);
- zapisz_do_pliku_tr(head1);
- }
- } while(wybor!='0');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement