Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <locale.h>
- #include <string.h>
- #include <conio.h>
- enum rodzajSortowania{imie = 1, nazwisko = 2, numer = 3, grupa = 4};
- typedef struct
- {
- char imie[20];
- char nazwisko[40];
- char numer[20];
- char grupa[20];
- }kontakt;
- struct element
- {
- struct element* nastepny;
- kontakt konto;
- struct element* poprzedni;
- };
- void dodajElement(struct element** lista, kontakt a, int* licznikElementow);
- void pokazElement(struct element* lista);
- void wyswietlListe(struct element* lista);
- void usunElement(struct element** lista, int* licznikElementow);
- void pokazElement(struct element* lista);
- void wyczyscListe(struct element** lista, int* licznikElementow);
- void odczytZpliku(struct element** lista, char* nazwa, int* licznikElementow);
- int sortowanie(struct element** lista, int typ, int licznikElementow);
- void intscanf(int* liczba);
- void wyszukiwanie(struct element** lista, char* fragment);
- void wyszukiwanieGrupy(struct element **lista, char* fragment);
- void wpiszWyszukaj(struct element **lista, char** fragment, char co);
- int main()
- {
- int licznikElementow = 0;
- int aktywneSortowanie = 0;
- int menu2, dop, numer, i;
- int menu1 = 0;
- struct element* aktywny;
- kontakt bufor;
- char* nazwaPliku = (char*)malloc(40*sizeof(char));
- char* dane = (char*)malloc(100*sizeof(char));
- char* fragment;
- setlocale(LC_ALL,"polish_poland");
- aktywny = (struct element*)malloc(sizeof(struct element));
- while(!menu1)
- {
- puts("\n 1. Wczytaj listę kontaktów z pliku *.csv\n\
- 2. Wyświetl listę kontaktów.\n\
- 3. Sortuj listę kontaktów.\n\
- 4. Dodaj kontakt do listy.\n\
- 5. Wyszukaj kontakt z listy.\n\
- 6. Wyszukaj kontakty z danej grupy.\n\
- 7. Usuń wybrany kontakt z listy.\n\
- 8. Wyczyść listę kontaktów.\n\
- 0. Wyjdź.");
- intscanf(&menu2);
- switch(menu2)
- {
- case 1:
- puts("Wpisz nazwę pliku, wraz z rozszerzeniem.");
- scanf("%s",nazwaPliku);
- odczytZpliku(&aktywny,nazwaPliku,&licznikElementow);
- break;
- case 2:
- wyswietlListe(aktywny);
- break;
- case 3:
- puts("Podaj element kontaktu, według którego ma zostać wykonane sortowanie.\n\
- 1 - Imię\n\
- 2 - Nazwisko\n\
- 4 - Grupa");
- intscanf(&aktywneSortowanie);
- if(aktywneSortowanie > 0 && aktywneSortowanie < 5 && aktywneSortowanie != 3)
- aktywneSortowanie = sortowanie(&aktywny,aktywneSortowanie,licznikElementow);
- else
- do
- {
- puts("Spróbuj ponownie!");
- intscanf(&aktywneSortowanie);
- }while(aktywneSortowanie <= 0 || aktywneSortowanie >= 5 || aktywneSortowanie == 3);
- break;
- case 4:
- puts("Wpisz dane kontaktu oddzielone spacjami.(Imię Nazwisko NumerTelefonu Grupa)");
- do
- {
- gets(dane);
- dop = sscanf(dane,"%s %s %s %s", bufor.imie, bufor.nazwisko, bufor.numer, bufor.grupa);
- if(dop == 4)
- {
- dodajElement(&aktywny,bufor,&licznikElementow);
- aktywneSortowanie = sortowanie(&aktywny,aktywneSortowanie,licznikElementow);
- }
- }while(dop != 4);
- break;
- case 5:
- puts("Wpisz fragment nazwy, którego szukać:");
- fragment = (char*)calloc(40,sizeof(char));
- wpiszWyszukaj(&aktywny, &fragment, 'N'); // 'N' = po nazwach
- free(fragment);
- break;
- case 6:
- puts("Wpisz nazwę grupy, której szukać:");
- fragment = (char*)calloc(20,sizeof(char));
- wpiszWyszukaj(&aktywny, &fragment, 'G'); // 'G' = po grupach
- free(fragment);
- break;
- case 7:
- wyswietlListe(aktywny);
- puts("Podaj numer kontaktu, który usunąć: ");
- intscanf(&numer);
- while(numer > licznikElementow)
- {
- puts("Nie ma takiego numeru. Podaj numer jeszcze raz: ");
- intscanf(&numer);
- }
- for(i = 1; i<numer; i++)
- if(aktywny->poprzedni != NULL)
- aktywny = aktywny->poprzedni;
- else
- break;
- usunElement(&aktywny, &licznikElementow);
- for(; aktywny->nastepny != NULL; aktywny = aktywny->nastepny);
- break;
- case 8:
- wyczyscListe(&aktywny,&licznikElementow);
- break;
- case 0:
- wyczyscListe(&aktywny,&licznikElementow);
- free(nazwaPliku);
- free(dane);
- free(aktywny);
- menu1 = 1;
- break;
- default:
- puts("Spróbuj ponownie!");
- break;
- }
- }
- return 0;
- }
- void dodajElement(struct element** lista, kontakt a, int* licznikElementow)
- {
- struct element* nowy;
- if(*licznikElementow != 0)
- {
- nowy = (struct element*)malloc(sizeof(struct element));
- nowy->konto = a;
- nowy->poprzedni = *lista;
- nowy->nastepny = (*lista)->nastepny;
- if(*lista != NULL)
- (*lista)->nastepny = nowy;
- if(nowy->nastepny != NULL)
- nowy->nastepny->poprzedni = nowy;
- *lista = (*lista)->nastepny;
- (*licznikElementow)++;
- }else
- {
- (*lista)->nastepny = NULL;
- (*lista)->poprzedni = NULL;
- (*lista)->konto = a;
- (*licznikElementow)++;
- }
- }
- void pokazElement(struct element* lista)
- {
- printf("%s %s %s %s\n", lista->konto.imie, lista->konto.nazwisko, lista->konto.numer, lista->konto.grupa);
- }
- void usunElement(struct element** lista, int* licznikElementow)
- {
- struct element* bufor = *lista;
- if((*lista)->nastepny != NULL)
- (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
- if((*lista)->poprzedni != NULL)
- (*lista)->poprzedni->nastepny = (*lista)->nastepny;
- if((*lista) != NULL)
- {
- *lista = (*lista)->nastepny;
- free(bufor);
- (*licznikElementow)--;
- }
- else
- *lista = NULL;
- }
- void wyczyscListe(struct element** lista, int* licznikElementow)
- {
- if((*lista) != NULL)
- {
- for(;(*lista)->nastepny != NULL; (*lista) = (*lista)->nastepny);
- while((*lista) != NULL && *licznikElementow != 0)
- usunElement(lista, licznikElementow);
- if(*licznikElementow == 0)
- puts("Wyczyszczono listę");
- }
- }
- void wyswietlListe(struct element* lista)
- {
- struct element* aktualny = lista;
- int i = 1;
- while(aktualny != NULL)
- {
- printf("%d: %s %s %s %s\n", i, aktualny->konto.imie, aktualny->konto.nazwisko, aktualny->konto.numer, aktualny->konto.grupa);
- aktualny = aktualny->poprzedni;
- i++;
- }
- if(aktualny == NULL)
- printf("\n");
- }
- void odczytZpliku(struct element** lista, char* nazwa, int* licznikElementow)
- {
- FILE* plik;
- char* nazwaPliku = nazwa;
- kontakt* bufor = NULL;
- plik = fopen(nazwaPliku, "r");
- while(plik == NULL)
- {
- puts("Błąd otwarcia pliku. Podaj nazwę jeszcze raz.");
- scanf("%s", nazwaPliku);
- plik = fopen(nazwaPliku, "r");
- }
- while(plik)
- {
- bufor = (kontakt*)malloc(sizeof(kontakt));
- fscanf(plik,"%[^;];",bufor->imie);
- fscanf(plik,"%[^;];",bufor->nazwisko);
- fscanf(plik,"%[^;];",bufor->numer);
- fscanf(plik,"%s\n",bufor->grupa);
- if(feof(plik) != 0)
- {
- dodajElement(lista, *bufor, licznikElementow);
- free(bufor);
- break;
- }else
- {
- dodajElement(lista, *bufor, licznikElementow);
- free(bufor);
- }
- }
- fclose(plik);
- }
- int sortowanie(struct element** lista, int typ, int licznikElementow) //1-sortowanie imionami, 2-nazwiskami, 3-numerami, 4-grupami
- {
- int licznikZamian, i;
- switch(typ)
- {
- case imie:
- do
- {
- licznikZamian = 0;
- for(i = 1; i<licznikElementow; i++)
- {
- if(strcoll((*lista)->konto.imie,(*lista)->poprzedni->konto.imie) == 1)
- {
- licznikZamian++;
- if((*lista)->nastepny != NULL)
- (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
- (*lista)->poprzedni->nastepny = (*lista)->nastepny;
- (*lista)->nastepny = (*lista)->poprzedni;
- (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
- (*lista)->nastepny->poprzedni = (*lista);
- if((*lista)->poprzedni != NULL)
- (*lista)->poprzedni->nastepny = (*lista);
- }else if(strcoll((*lista)->konto.imie,(*lista)->poprzedni->konto.imie) == 0)
- {
- if(strcoll((*lista)->konto.nazwisko,(*lista)->poprzedni->konto.nazwisko) == 1)
- {
- licznikZamian++;
- if((*lista)->nastepny != NULL)
- (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
- (*lista)->poprzedni->nastepny = (*lista)->nastepny;
- (*lista)->nastepny = (*lista)->poprzedni;
- (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
- (*lista)->nastepny->poprzedni = (*lista);
- if((*lista)->poprzedni != NULL)
- (*lista)->poprzedni->nastepny = (*lista);
- }else (*lista) = (*lista)->poprzedni;
- }else (*lista) = (*lista)->poprzedni;
- }
- for(i = 1; i < licznikElementow; i++)
- (*lista) = (*lista)->nastepny;
- }while(licznikZamian != 0);
- return 1;
- break;
- case nazwisko:
- do
- {
- licznikZamian = 0;
- for(i = 1; i<licznikElementow; i++)
- {
- if(strcoll((*lista)->konto.nazwisko,(*lista)->poprzedni->konto.nazwisko) == 1)
- {
- licznikZamian++;
- if((*lista)->nastepny != NULL)
- (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
- (*lista)->poprzedni->nastepny = (*lista)->nastepny;
- (*lista)->nastepny = (*lista)->poprzedni;
- (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
- (*lista)->nastepny->poprzedni = (*lista);
- if((*lista)->poprzedni != NULL)
- (*lista)->poprzedni->nastepny = (*lista);
- }else if(strcoll((*lista)->konto.nazwisko,(*lista)->poprzedni->konto.nazwisko) == 0)
- {
- if(strcoll((*lista)->konto.imie,(*lista)->poprzedni->konto.imie) == 1)
- {
- licznikZamian++;
- if((*lista)->nastepny != NULL)
- (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
- (*lista)->poprzedni->nastepny = (*lista)->nastepny;
- (*lista)->nastepny = (*lista)->poprzedni;
- (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
- (*lista)->nastepny->poprzedni = (*lista);
- if((*lista)->poprzedni != NULL)
- (*lista)->poprzedni->nastepny = (*lista);
- }else (*lista) = (*lista)->poprzedni;
- }else (*lista) = (*lista)->poprzedni;
- }
- for(i = 1; i < licznikElementow; i++)
- (*lista) = (*lista)->nastepny;
- }while(licznikZamian != 0);
- return 2;
- break;
- case numer:
- do
- {
- licznikZamian = 0;
- for(i = 1; i<licznikElementow; i++)
- {
- if(strcoll((*lista)->konto.numer,(*lista)->poprzedni->konto.numer) == 1)
- {
- licznikZamian++;
- if((*lista)->nastepny != NULL)
- (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
- (*lista)->poprzedni->nastepny = (*lista)->nastepny;
- (*lista)->nastepny = (*lista)->poprzedni;
- (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
- (*lista)->nastepny->poprzedni = (*lista);
- if((*lista)->poprzedni != NULL)
- (*lista)->poprzedni->nastepny = (*lista);
- }else (*lista) = (*lista)->poprzedni;
- }
- for(i = 1; i < licznikElementow; i++)
- (*lista) = (*lista)->nastepny;
- }while(licznikZamian != 0);
- return 3;
- break;
- case grupa:
- do
- {
- licznikZamian = 0;
- for(i = 1; i<licznikElementow; i++)
- {
- if(strcoll((*lista)->konto.grupa,(*lista)->poprzedni->konto.grupa) == 1)
- {
- licznikZamian++;
- if((*lista)->nastepny != NULL)
- (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
- (*lista)->poprzedni->nastepny = (*lista)->nastepny;
- (*lista)->nastepny = (*lista)->poprzedni;
- (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
- (*lista)->nastepny->poprzedni = (*lista);
- if((*lista)->poprzedni != NULL)
- (*lista)->poprzedni->nastepny = (*lista);
- }else if(strcoll((*lista)->konto.grupa,(*lista)->poprzedni->konto.grupa) == 0)
- {
- if(strcoll((*lista)->konto.nazwisko,(*lista)->poprzedni->konto.nazwisko) == 1)
- {
- licznikZamian++;
- if((*lista)->nastepny != NULL)
- (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
- (*lista)->poprzedni->nastepny = (*lista)->nastepny;
- (*lista)->nastepny = (*lista)->poprzedni;
- (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
- (*lista)->nastepny->poprzedni = (*lista);
- if((*lista)->poprzedni != NULL)
- (*lista)->poprzedni->nastepny = (*lista);
- }else (*lista) = (*lista)->poprzedni;
- }else (*lista) = (*lista)->poprzedni;
- }
- for(i = 1; i < licznikElementow; i++)
- (*lista) = (*lista)->nastepny;
- }while(licznikZamian != 0);
- return 4;
- break;
- default:
- return 0;
- break;
- }
- }
- void intscanf(int* liczba)
- {
- while(1)
- {
- if(!scanf("%d",liczba))
- {
- while('\n' != getchar())
- puts("Wpisano wartość spoza wyznaczonego zakresu. Spróbuj jeszcze raz.");
- }
- else break;
- }
- }
- void wyszukiwanie(struct element **lista, char* fragment)
- {
- int j;
- int i = 0;
- char* tab = (char*)malloc(40*sizeof(char));
- char buforImie[20];
- char buforNazwisko[40];
- struct element *bufor = (struct element*)malloc(sizeof(struct element));
- for(; (*lista)->nastepny != NULL; *lista = (*lista)->nastepny);
- for(bufor = *lista; bufor != NULL; bufor = bufor->poprzedni)
- {
- for(j=0;j<20;j++)
- {
- if(bufor->konto.imie[j] > 96 && bufor->konto.imie[j] < 123)
- buforImie[j] = bufor->konto.imie[j];
- else if(bufor->konto.imie[j] > 64 && bufor->konto.imie[j] < 96)
- buforImie[j] = bufor->konto.imie[j] + 32;
- else if(bufor->konto.imie[j] == '\0')
- break;
- }
- for(j=0;j<40;j++)
- {
- if(bufor->konto.nazwisko[j] > 96 && bufor->konto.nazwisko[j] < 123)
- buforNazwisko[j] = bufor->konto.nazwisko[j];
- else if(bufor->konto.nazwisko[j] > 64 && bufor->konto.nazwisko[j] < 96)
- buforNazwisko[j] = bufor->konto.nazwisko[j] + 32;
- else if(bufor->konto.nazwisko[j] == '\0')
- break;
- }
- tab = strstr(buforImie, fragment);
- if(tab != NULL)
- {
- pokazElement(bufor);
- i++;
- }
- else
- {
- tab = strstr(buforNazwisko, fragment);
- if(tab != NULL)
- {
- pokazElement(bufor);
- i++;
- }
- }
- }
- free(bufor);
- //free(tab); ###ODKOMENTOWANIE POWODUJE BŁĄD, WTF?
- }
- void wyszukiwanieGrupy(struct element **lista, char* fragment)
- {
- int j;
- char* tab;
- char buforGrupa[20];
- struct element *bufor;
- for(; (*lista)->nastepny != NULL; *lista = (*lista)->nastepny);
- for(bufor = *lista; bufor != NULL; bufor = bufor->poprzedni)
- {
- for(j=0;j<20;j++)
- {
- if(bufor->konto.grupa[j] > 96 && bufor->konto.grupa[j] < 123)
- buforGrupa[j] = bufor->konto.grupa[j];
- else if(bufor->konto.grupa[j] > 64 && bufor->konto.grupa[j] < 96)
- buforGrupa[j] = bufor->konto.grupa[j] + 32;
- else if(bufor->konto.grupa[j] == '\n')
- break;
- }
- tab = strstr(buforGrupa, fragment);
- if(tab != NULL)
- pokazElement(bufor);
- }
- }
- void wpiszWyszukaj(struct element **lista, char** fragment, char co)
- {
- int j = 0;
- int i = 0;
- char buforCiagu;
- while(1)
- {
- buforCiagu = _getch();
- if(buforCiagu != 13 && buforCiagu > 96 && buforCiagu < 123 || buforCiagu == 32) //od 97 do 122 małe litery
- {
- (*fragment)[j] = buforCiagu;
- for(i = 0; i <= j; i++)
- printf("%c",(*fragment)[i]);
- puts("");
- if(co == 'N')
- wyszukiwanie(lista, *fragment);
- else if(co == 'G')
- wyszukiwanieGrupy(lista, *fragment);
- puts("");
- j++;
- }
- else if(buforCiagu != 13 && buforCiagu > 64 && buforCiagu < 96 || buforCiagu == 32) //od 65 do 95 duże litery
- {
- buforCiagu += 32;
- (*fragment)[j] = buforCiagu;
- for(i = 0; i <= j; i++)
- printf("%c",(*fragment)[i] - 32);
- puts("");
- if(co == 'N')
- wyszukiwanie(lista, *fragment);
- else if(co == 'G')
- wyszukiwanieGrupy(lista, *fragment);
- puts("");
- j++;
- }
- else if(buforCiagu == 13) //ENTER
- {
- puts("");
- break;
- }
- else if(buforCiagu == 8 && j > 0) //BACKSPACE
- {
- (*fragment)[j] = '\0';
- j--;
- (*fragment)[j] = '\0';
- for(i = 0; i <= j; i++)
- printf("%c",(*fragment)[i]);
- puts("");
- if(co == 'N')
- wyszukiwanie(lista, *fragment);
- else if(co == 'G')
- wyszukiwanieGrupy(lista, *fragment);
- puts("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement