Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define true 1
- #define false 0
- typedef int bool;
- typedef struct s_listaO{
- char imie[16];
- char nazwisko[16];
- int kwotaNaPrezent;
- bool czyKupiono;
- char opisPrezentu[32];
- int cenaPrezentu;
- struct s_listaO *next;
- }ListaO;
- typedef struct s_listaP{
- int indeks;
- int cena;
- char opis[50];
- struct s_listaP *next;
- }ListaP;
- ListaP *DodajPrezent (ListaP *start)
- {
- ListaP *pozycjaPrzed = start;
- ListaP *nowyPrezent = malloc(sizeof(ListaP));
- printf("Podaj opis prezentu: \n");
- scanf("%s", nowyPrezent->opis);
- printf("Podaj cene prezentu: \n");
- scanf("%d", &nowyPrezent->cena);
- if (start == 0) {
- printf("Dodano do listy prezent: %s o wartosci %d\n", nowyPrezent->opis, nowyPrezent->cena);
- nowyPrezent->next = NULL;
- nowyPrezent->indeks = 1;
- start = nowyPrezent;
- return start;
- }
- else
- {
- int mark = 0;
- ListaP *pozycjaPo = start->next;
- while (mark == 0){
- if ((strcmp(pozycjaPrzed->opis, nowyPrezent->opis) < 0 && pozycjaPo == NULL) || (strcmp(pozycjaPrzed->opis, nowyPrezent->opis) < 0 && strcmp(pozycjaPo->opis, nowyPrezent->opis) > 0)) {
- pozycjaPrzed->next = nowyPrezent;
- nowyPrezent->next = pozycjaPo;
- mark = 1;
- nowyPrezent->indeks = pozycjaPrzed->indeks++;
- printf("Dodano do listy prezent: %s o wartosci %d\n", nowyPrezent->opis, nowyPrezent->cena);
- return start;
- }
- else if (strcmp(pozycjaPrzed->opis, nowyPrezent->opis) > 0) {
- nowyPrezent->next = pozycjaPrzed;
- start = nowyPrezent;
- mark = 1;
- nowyPrezent->indeks = pozycjaPrzed->indeks--;
- printf("Dodano do listy prezent: %s o wartosci %d\n", nowyPrezent->opis, nowyPrezent->cena);
- return start;
- }
- else if (strcmp(pozycjaPrzed->opis, nowyPrezent->opis) == 0) {
- pozycjaPrzed->next = nowyPrezent;
- nowyPrezent->next = pozycjaPo;
- nowyPrezent->indeks = pozycjaPrzed->indeks++;
- printf("Dodano do listy prezent: %s o wartosci %d\n", nowyPrezent->opis, nowyPrezent->cena);
- mark = 1;
- return start;
- }
- else {
- pozycjaPrzed = pozycjaPrzed->next;
- pozycjaPo = pozycjaPo->next;
- }
- }
- }
- }
- void WypiszPrezenty(ListaP *start)
- {
- ListaP *tmp = start;
- while (tmp!=0)
- {
- printf("Prezent nr %d to: %s o wartosci %d zl", tmp->indeks, tmp->opis, tmp->cena);
- tmp = tmp->next;
- }
- }
- ListaO *Dodaj(ListaO *start, ListaP **start2)
- {
- ListaO *pozycjaPrzed = start;
- ListaO *nowaOsoba = malloc(sizeof(ListaO));
- char input[33];
- printf("Podaj imie i nazwisko osoby: \n");
- fgets(input, 32, stdin);
- sscanf(input,"%s %s", nowaOsoba->imie, nowaOsoba->nazwisko);
- printf("Podaj maksymalna kwote przeznaczona na prezent:\n");
- scanf("%d", &nowaOsoba->kwotaNaPrezent);
- printf("Aby wybrac prezent z listy, napisz lista\nAby podac nowy pomysl, napisz nowy\n");
- char input2[6];
- scanf("%s", input2);
- if (strncmp(input2, "lista", 5) == 0)
- {
- WypiszPrezenty(start2);
- }
- else if (strncmp(input2, "nowy", 4) == 0)
- {
- DodajPrezent(&start2);
- }
- else
- {
- printf("NIE DZIALA\n");
- }
- if (start == 0) {
- printf("Dodano osobe: %s %s\n", nowaOsoba->imie, nowaOsoba->nazwisko);
- nowaOsoba->next = NULL;
- }
- else
- {
- int mark = 0;
- ListaO *pozycjaPo = start->next;
- while (mark == 0){
- if ((strcmp(pozycjaPrzed->nazwisko, nowaOsoba->nazwisko) < 0 && pozycjaPo == NULL) || (strcmp(pozycjaPrzed->nazwisko, nowaOsoba->nazwisko) < 0 && strcmp(pozycjaPo->nazwisko, nowaOsoba->nazwisko) > 0)) {
- pozycjaPrzed->next = nowaOsoba;
- nowaOsoba->next = pozycjaPo;
- mark = 1;
- printf("Dodano osobe: %s %s\n", nowaOsoba->imie, nowaOsoba->nazwisko);
- return start;
- }
- else if (strcmp(pozycjaPrzed->nazwisko, nowaOsoba->nazwisko) > 0) {
- nowaOsoba->next = pozycjaPrzed;
- start = nowaOsoba;
- mark = 1;
- printf("Dodano osobe: %s %s\n", nowaOsoba->imie, nowaOsoba->nazwisko);
- return start;
- }
- else if (strcmp(pozycjaPrzed->nazwisko, nowaOsoba->nazwisko) == 0) {
- pozycjaPrzed->next = nowaOsoba;
- nowaOsoba->next = pozycjaPo;
- printf("Dodano osobe: %s %s\n", nowaOsoba->imie, nowaOsoba->nazwisko);
- return start;
- }
- else {
- pozycjaPrzed = pozycjaPrzed->next;
- pozycjaPo = pozycjaPo->next;
- }
- }
- }
- return start;
- }
- void Wypisz(ListaO *start)
- {
- ListaO *tmp = start;
- int licznik = 0;
- while (tmp!=0) {
- licznik++;
- printf("Osoba nr %d to: %s %s\n", licznik, tmp->imie, tmp->nazwisko);
- tmp=tmp->next;
- }
- }
- void Zapisz(){
- printf("Zapisano..\n");
- }
- void Odczytaj(){
- printf("Odczytano..\n");
- }
- void WyczyscO(ListaO *start)
- {
- ListaO *czysc = start;
- ListaO *trzymaj = NULL;
- while (trzymaj!=NULL) {
- trzymaj = czysc->next;
- free(czysc);
- czysc = trzymaj;
- }
- }
- void WyczyscP(ListaP *start)
- {
- ListaP *czysc = start;
- ListaP *trzymaj = NULL;
- while (trzymaj!=NULL) {
- trzymaj = czysc->next;
- free(czysc);
- czysc = trzymaj;
- }
- }
- int main(){
- char input[16];
- char komenda[16];
- ListaO *poczatekListyO = NULL;
- ListaP *poczatekListyP = NULL;
- while(fgets(input, 15, stdin))
- {
- sscanf(input, "%s", komenda);
- if (strncmp(komenda, "dodajo", 6) == 0)
- {
- poczatekListyO = Dodaj(poczatekListyO, poczatekListyP);
- }
- else if (strncmp(komenda, "dodajp", 6) == 0)
- {
- poczatekListyP = DodajPrezent(poczatekListyP);
- }
- else if (strncmp(komenda, "wypiszo", 12) == 0)
- {
- Wypisz(poczatekListyO);
- }
- else if (strncmp(komenda, "wypiszp", 7) == 0)
- {
- WypiszPrezenty(poczatekListyP);
- }
- else if (strncmp(komenda, "zapisz", 6) == 0)
- {
- Zapisz();
- }
- else if (strncmp(komenda, "odczytaj", 8) == 0)
- {
- Odczytaj();
- }
- else if (strncmp(komenda, "wyjdz", 5) == 0)
- break;
- }
- WyczyscO(poczatekListyO);
- WyczyscP(poczatekListyP);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement