Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define MAX 150
- struct Talbum {
- char gatunek[MAX];
- char wytwornia[MAX];
- char wykonawca[MAX];
- float dlugosc; //dlugosc albumu
- char nazwa_albumu[MAX];//nazwa albumu
- struct Talbum *nast;
- struct Talbum *poprz;
- };
- struct Tlista {
- struct Talbum *pocz; //adres pierwszego elementu listy
- struct Talbum *kon; //adres ostatniego elementu listy
- };
- struct Talbum * nowy_el(char *gat, char *wytw, char *wyko, float dlug, char *naza)
- {
- struct Talbum *ptr = NULL;
- // printf("Adding line: %s %s %s %f %s\n", gat, wytw, wyko, dlug, naza);
- if (!(ptr = (struct Talbum*)malloc(sizeof(struct Talbum))))
- {
- printf("\nMemory allocation failure while adding a new element.\n");
- return NULL;
- }
- strcpy(ptr->gatunek, gat);
- strcpy(ptr->wykonawca, wyko);
- strcpy(ptr->wytwornia, wytw);
- strcpy(ptr->nazwa_albumu, naza);
- ptr->dlugosc = dlug;
- return ptr;
- }
- void dodaj_za_ostatnim(struct Tlista *lista, struct Talbum *el)
- {
- struct Talbum *wsk = lista->pocz;
- if (!wsk)
- {
- lista->pocz = lista->kon = el;
- lista->pocz->poprz = NULL;
- lista->kon->nast = NULL;
- }
- else
- {
- lista->kon->nast = el;
- el->poprz = lista->kon;
- lista->kon = el;
- }
- strcpy(lista->kon->gatunek, el->gatunek);
- strcpy(lista->kon->wytwornia, el->wytwornia);
- strcpy(lista->kon->wykonawca, el->wykonawca);
- lista->kon->dlugosc = el->dlugosc;
- strcpy(lista->kon->nazwa_albumu, el->nazwa_albumu);
- lista->kon->nast = NULL;
- }
- void wypisz(struct Tlista lista)
- {
- struct Talbum *temp = lista.pocz;
- printf("\nList:\n\n");
- while (temp)
- {
- printf("%s %s %s %f %s\n", temp->gatunek, temp->wytwornia, temp->wykonawca, temp->dlugosc, temp->nazwa_albumu);
- temp = temp->nast;
- }
- }
- void usun(struct Tlista lista, char *usuwana_nazwa)
- {
- struct Talbum *temp = lista.pocz;
- struct Talbum *poprz;
- if(strcmp(temp->nazwa_albumu,usuwana_nazwa)==0){
- lista.pocz=lista.pocz->nast;
- free(temp);
- }
- else{
- while(temp, strcmp(usuwana_nazwa,temp->nazwa_albumu)!=0)
- {
- poprz=temp;
- temp=temp->nast;
- }
- if(strcmp(usuwana_nazwa,temp->nazwa_albumu)==0)
- {
- poprz->nast=temp->nast;
- free(temp);
- }
- }
- }
- //void wypisz(struct Tlista lista)
- //{
- // struct Talbum *temp = lista.pocz;
- // printf("\nList:\n\n");
- //
- // if (lista.pocz == 0)
- // {
- // printf("LISTA JEST PUSTA.");
- // }
- // else while (temp)
- // {
- // printf("%s %s %s %f %s \n ", &temp->gatunek, temp->wytwornia, temp->wykonawca, temp->dlugosc, temp->nazwa_albumu);
- // temp = temp->nast;
- // }
- //}
- struct Tlista wczytaj_z_pliku(char *nazwa)
- {
- FILE *input;
- if (!(input = fopen(nazwa, "r")))
- {
- printf("\nCouldn't open input file %s. End of program.\n\n", nazwa);
- exit(0);
- }
- else
- {
- fseek(input, 0, SEEK_END);
- long pom1 = ftell(input);
- fseek(input, 0, SEEK_SET);
- long pom2 = ftell(input);
- if (pom1 == pom2)
- {
- printf("\nInput file %s is empty.\n\n", nazwa);
- if (fclose(input))
- {
- printf("Couldn't close input file %s. End of program.\n\n", nazwa);
- exit(0);
- }
- printf("End of program.\n\n");
- exit(0);
- }
- }
- struct Tlista lista;
- lista.pocz = NULL;
- lista.kon = NULL;
- struct Talbum bufor, *pom = NULL;
- while (fscanf(input, "%s %s %s %f %s", bufor.gatunek, bufor.wytwornia, bufor.wykonawca, &bufor.dlugosc, bufor.nazwa_albumu)==5)
- {
- if (!(pom = nowy_el(bufor.gatunek, bufor.wytwornia, bufor.wykonawca, bufor.dlugosc, bufor.nazwa_albumu)))
- {
- printf("Error while adding new element to the list.");
- if (fclose(input))
- {
- printf("Couldn't close input file %s. End of program.\n\n", nazwa);
- exit(0);
- }
- printf("End of program.\n\n");
- exit(0);
- }
- dodaj_za_ostatnim(&lista, pom);
- }
- return lista;
- }
- int main()
- {
- printf("***********WITAM W PROGRAMIE DOMINIKI JAROSZ************\n ******WCZYTYWANIE PLIKU.******* \n");
- printf("JEZELI CHCESZ DODAC WLASNE DANE WCISNIJ 1 \n");
- int czy=0;
- struct Tlista test;
- test.pocz = NULL;
- test.kon = NULL;
- char nazwa[20];
- char wytwornia_nazwa[20];
- char wykonawca[20];
- char gatunek[20];
- float dl;
- char filename[]="input.txt";
- test = wczytaj_z_pliku(&filename);
- scanf("%d",&czy);
- while (getchar()!='\n')
- ;
- while(czy==1)
- {
- printf("Podaj gatunek:\n");
- scanf("%s", gatunek);
- while (getchar()!='\n')
- ;
- printf("\nPodaj nazwe wytworni:\n");
- scanf("%s", wytwornia_nazwa);
- while (getchar()!='\n')
- ;
- printf("\nPodaj wykonawce:\n");
- scanf("%s", wykonawca);
- while (getchar()!='\n')
- ;
- printf("\nPodaj dlugosc:\n");
- scanf("%f", &dl);
- while (getchar()!='\n')
- ;
- printf("\nPodaj nazwe:\n");
- scanf("%s", nazwa);
- while (getchar()!='\n')
- ;
- struct Talbum *temp = nowy_el(&gatunek, &wytwornia_nazwa, &wykonawca, dl, &nazwa);
- dodaj_za_ostatnim(&test, temp);
- printf("JEZELI CHCESZ DODAC KOLEJNY ELEMENT DO LISTY KLIKNIJ 1, JEZELI NIE - KLIKNIJ 0");
- scanf("%d",&czy);
- while (getchar()!='\n')
- ;
- }
- // test = wczytaj_z_pliku(&filename);
- //dodaj_za_ostatnim(&test, temp);
- wypisz(test);
- usun(test, "nazwa2");
- printf("\n\n Po usunieciu: \n\n");
- wypisz(test);
- printf("\n\nEnd of program.\n\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement