Advertisement
Guest User

ksiazka telefoniczna

a guest
Jan 17th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.45 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <locale.h>
  5. #include <string.h>
  6. #include <conio.h>
  7.  
  8. enum rodzajSortowania{imie = 1, nazwisko = 2, numer = 3, grupa = 4};
  9.  
  10. typedef struct
  11. {
  12.     char imie[20];
  13.     char nazwisko[40];
  14.     char numer[20];
  15.     char grupa[20];
  16. }kontakt;
  17. struct element
  18. {
  19.     struct element* nastepny;
  20.     kontakt konto;
  21.     struct element* poprzedni;
  22. };
  23. void dodajElement(struct element** lista, kontakt a, int* licznikElementow);
  24. void pokazElement(struct element* lista);
  25. void wyswietlListe(struct element* lista);
  26. void usunElement(struct element** lista, int* licznikElementow);
  27. void pokazElement(struct element* lista);
  28. void wyczyscListe(struct element** lista, int* licznikElementow);
  29. void odczytZpliku(struct element** lista, char* nazwa, int* licznikElementow);
  30. int sortowanie(struct element** lista, int typ, int licznikElementow);
  31. void intscanf(int* liczba);
  32. void wyszukiwanie(struct element** lista, char* fragment);
  33. void wyszukiwanieGrupy(struct element **lista, char* fragment);
  34. void wpiszWyszukaj(struct element **lista, char** fragment, char co);
  35.  
  36. int main()
  37. {
  38.     int licznikElementow = 0;
  39.     int aktywneSortowanie = 0;
  40.     int menu2, dop, numer, i;
  41.     int menu1 = 0;
  42.     struct element* aktywny;
  43.     kontakt bufor;
  44.     char* nazwaPliku = (char*)malloc(40*sizeof(char));
  45.     char* dane = (char*)malloc(100*sizeof(char));
  46.     char* fragment;
  47.  
  48.     setlocale(LC_ALL,"polish_poland");
  49.     aktywny = (struct element*)malloc(sizeof(struct element));
  50.     while(!menu1)
  51.     {
  52.         puts("\n    1. Wczytaj listę kontaktów z pliku *.csv\n\
  53.     2. Wyświetl listę kontaktów.\n\
  54.     3. Sortuj listę kontaktów.\n\
  55.     4. Dodaj kontakt do listy.\n\
  56.     5. Wyszukaj kontakt z listy.\n\
  57.     6. Wyszukaj kontakty z danej grupy.\n\
  58.     7. Usuń wybrany kontakt z listy.\n\
  59.     8. Wyczyść listę kontaktów.\n\
  60.     0. Wyjdź.");
  61.         intscanf(&menu2);
  62.         switch(menu2)
  63.         {
  64.         case 1:
  65.             puts("Wpisz nazwę pliku, wraz z rozszerzeniem.");
  66.             scanf("%s",nazwaPliku);
  67.             odczytZpliku(&aktywny,nazwaPliku,&licznikElementow);
  68.             break;
  69.         case 2:
  70.             wyswietlListe(aktywny);
  71.             break;
  72.         case 3:
  73.             puts("Podaj element kontaktu, według którego ma zostać wykonane sortowanie.\n\
  74.                  1 - Imię\n\
  75.                  2 - Nazwisko\n\
  76.                  4 - Grupa");
  77.             intscanf(&aktywneSortowanie);
  78.             if(aktywneSortowanie > 0 && aktywneSortowanie < 5 && aktywneSortowanie != 3)
  79.                 aktywneSortowanie = sortowanie(&aktywny,aktywneSortowanie,licznikElementow);
  80.             else
  81.                 do
  82.                 {
  83.                     puts("Spróbuj ponownie!");
  84.                     intscanf(&aktywneSortowanie);
  85.                 }while(aktywneSortowanie <= 0 || aktywneSortowanie >= 5 || aktywneSortowanie == 3);
  86.             break;
  87.         case 4:
  88.             puts("Wpisz dane kontaktu oddzielone spacjami.(Imię Nazwisko NumerTelefonu Grupa)");
  89.             do
  90.             {
  91.                 gets(dane);
  92.                 dop = sscanf(dane,"%s %s %s %s", bufor.imie, bufor.nazwisko, bufor.numer, bufor.grupa);
  93.                 if(dop == 4)
  94.                 {
  95.                     dodajElement(&aktywny,bufor,&licznikElementow);
  96.                     aktywneSortowanie = sortowanie(&aktywny,aktywneSortowanie,licznikElementow);
  97.                 }
  98.             }while(dop != 4);
  99.             break;
  100.         case 5:
  101.             puts("Wpisz fragment nazwy, którego szukać:");
  102.             fragment = (char*)calloc(40,sizeof(char));
  103.             wpiszWyszukaj(&aktywny, &fragment, 'N');                    // 'N' = po nazwach
  104.             free(fragment);
  105.             break;
  106.         case 6:
  107.             puts("Wpisz nazwę grupy, której szukać:");
  108.             fragment = (char*)calloc(20,sizeof(char));
  109.             wpiszWyszukaj(&aktywny, &fragment, 'G');                    // 'G' = po grupach
  110.             free(fragment);
  111.             break;
  112.         case 7:
  113.             wyswietlListe(aktywny);
  114.             puts("Podaj numer kontaktu, który usunąć: ");
  115.             intscanf(&numer);
  116.             while(numer > licznikElementow)
  117.             {
  118.                 puts("Nie ma takiego numeru. Podaj numer jeszcze raz: ");
  119.                 intscanf(&numer);
  120.             }
  121.             for(i = 1; i<numer; i++)
  122.                 if(aktywny->poprzedni != NULL)
  123.                     aktywny = aktywny->poprzedni;
  124.                 else
  125.                     break;
  126.             usunElement(&aktywny, &licznikElementow);
  127.             for(; aktywny->nastepny != NULL; aktywny = aktywny->nastepny);
  128.             break;
  129.         case 8:
  130.             wyczyscListe(&aktywny,&licznikElementow);
  131.             break;
  132.         case 0:
  133.             wyczyscListe(&aktywny,&licznikElementow);
  134.             free(nazwaPliku);
  135.             free(dane);
  136.             free(aktywny);
  137.             menu1 = 1;
  138.             break;
  139.         default:
  140.             puts("Spróbuj ponownie!");
  141.             break;
  142.         }
  143.     }
  144.     return 0;
  145. }
  146. void dodajElement(struct element** lista, kontakt a, int* licznikElementow)
  147. {
  148.     struct element* nowy;
  149.     if(*licznikElementow != 0)
  150.     {
  151.         nowy = (struct element*)malloc(sizeof(struct element));
  152.         nowy->konto = a;
  153.         nowy->poprzedni = *lista;
  154.         nowy->nastepny = (*lista)->nastepny;
  155.         if(*lista != NULL)
  156.             (*lista)->nastepny = nowy;
  157.         if(nowy->nastepny != NULL)
  158.             nowy->nastepny->poprzedni = nowy;
  159.         *lista = (*lista)->nastepny;
  160.         (*licznikElementow)++;
  161.     }else
  162.     {
  163.         (*lista)->nastepny = NULL;
  164.         (*lista)->poprzedni = NULL;
  165.         (*lista)->konto = a;
  166.         (*licznikElementow)++;
  167.     }
  168. }
  169. void pokazElement(struct element* lista)
  170. {
  171.     printf("%s %s %s %s\n", lista->konto.imie, lista->konto.nazwisko, lista->konto.numer, lista->konto.grupa);
  172. }
  173. void usunElement(struct element** lista, int* licznikElementow)
  174. {
  175.     struct element* bufor = *lista;
  176.     if((*lista)->nastepny != NULL)
  177.         (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
  178.     if((*lista)->poprzedni != NULL)
  179.         (*lista)->poprzedni->nastepny = (*lista)->nastepny;
  180.     if((*lista) != NULL)
  181.     {
  182.         *lista = (*lista)->nastepny;
  183.         free(bufor);
  184.         (*licznikElementow)--;
  185.     }
  186.     else
  187.         *lista = NULL;
  188. }
  189. void wyczyscListe(struct element** lista, int* licznikElementow)
  190. {
  191.     if((*lista) != NULL)
  192.     {  
  193.         for(;(*lista)->nastepny != NULL; (*lista) = (*lista)->nastepny);
  194.         while((*lista) != NULL && *licznikElementow != 0)
  195.             usunElement(lista, licznikElementow);
  196.         if(*licznikElementow == 0)
  197.             puts("Wyczyszczono listę");
  198.     }
  199. }
  200. void wyswietlListe(struct element* lista)
  201. {
  202.     struct element* aktualny = lista;
  203.     int i = 1;
  204.     while(aktualny != NULL)
  205.     {
  206.         printf("%d: %s %s %s %s\n", i, aktualny->konto.imie, aktualny->konto.nazwisko, aktualny->konto.numer, aktualny->konto.grupa);
  207.         aktualny = aktualny->poprzedni;
  208.         i++;
  209.     }
  210.     if(aktualny == NULL)
  211.         printf("\n");
  212. }
  213.  
  214. void odczytZpliku(struct element** lista, char* nazwa, int* licznikElementow)
  215. {
  216.     FILE* plik;
  217.     char* nazwaPliku = nazwa;
  218.     kontakt* bufor = NULL;
  219.     plik = fopen(nazwaPliku, "r");
  220.     while(plik == NULL)
  221.     {
  222.         puts("Błąd otwarcia pliku. Podaj nazwę jeszcze raz.");
  223.         scanf("%s", nazwaPliku);
  224.         plik = fopen(nazwaPliku, "r");
  225.     }
  226.     while(plik)
  227.     {
  228.         bufor = (kontakt*)malloc(sizeof(kontakt));
  229.         fscanf(plik,"%[^;];",bufor->imie);
  230.         fscanf(plik,"%[^;];",bufor->nazwisko);
  231.         fscanf(plik,"%[^;];",bufor->numer);
  232.         fscanf(plik,"%s\n",bufor->grupa);
  233.         if(feof(plik) != 0)
  234.         {
  235.             dodajElement(lista, *bufor, licznikElementow);
  236.             free(bufor);
  237.             break;
  238.         }else
  239.         {
  240.             dodajElement(lista, *bufor, licznikElementow);
  241.             free(bufor);
  242.         }
  243.     }
  244.     fclose(plik);
  245. }
  246. int sortowanie(struct element** lista, int typ, int licznikElementow)   //1-sortowanie imionami, 2-nazwiskami, 3-numerami, 4-grupami
  247. {
  248.     int licznikZamian, i;
  249.     switch(typ)
  250.     {
  251.     case imie:
  252.         do
  253.         {
  254.             licznikZamian = 0;
  255.             for(i = 1; i<licznikElementow; i++)
  256.             {
  257.                 if(strcoll((*lista)->konto.imie,(*lista)->poprzedni->konto.imie) ==  1)
  258.                 {
  259.                     licznikZamian++;
  260.                     if((*lista)->nastepny != NULL)
  261.                         (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
  262.                     (*lista)->poprzedni->nastepny = (*lista)->nastepny;
  263.                     (*lista)->nastepny = (*lista)->poprzedni;
  264.                     (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
  265.                     (*lista)->nastepny->poprzedni = (*lista);
  266.                     if((*lista)->poprzedni != NULL)
  267.                         (*lista)->poprzedni->nastepny = (*lista);
  268.  
  269.                 }else if(strcoll((*lista)->konto.imie,(*lista)->poprzedni->konto.imie) ==  0)
  270.                 {
  271.                     if(strcoll((*lista)->konto.nazwisko,(*lista)->poprzedni->konto.nazwisko) ==  1)
  272.                     {
  273.                         licznikZamian++;
  274.                         if((*lista)->nastepny != NULL)
  275.                             (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
  276.                         (*lista)->poprzedni->nastepny = (*lista)->nastepny;
  277.                         (*lista)->nastepny = (*lista)->poprzedni;
  278.                         (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
  279.                         (*lista)->nastepny->poprzedni = (*lista);
  280.                         if((*lista)->poprzedni != NULL)
  281.                             (*lista)->poprzedni->nastepny = (*lista);
  282.                     }else (*lista) = (*lista)->poprzedni;
  283.                 }else (*lista) = (*lista)->poprzedni;
  284.             }
  285.             for(i = 1; i < licznikElementow; i++)
  286.                 (*lista) = (*lista)->nastepny;
  287.         }while(licznikZamian != 0);
  288.         return 1;
  289.         break;
  290.     case nazwisko:
  291.         do
  292.         {
  293.             licznikZamian = 0;
  294.             for(i = 1; i<licznikElementow; i++)
  295.             {
  296.                 if(strcoll((*lista)->konto.nazwisko,(*lista)->poprzedni->konto.nazwisko) ==  1)
  297.                 {
  298.                     licznikZamian++;
  299.                     if((*lista)->nastepny != NULL)
  300.                         (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
  301.                     (*lista)->poprzedni->nastepny = (*lista)->nastepny;
  302.                     (*lista)->nastepny = (*lista)->poprzedni;
  303.                     (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
  304.                     (*lista)->nastepny->poprzedni = (*lista);
  305.                     if((*lista)->poprzedni != NULL)
  306.                         (*lista)->poprzedni->nastepny = (*lista);
  307.  
  308.                 }else if(strcoll((*lista)->konto.nazwisko,(*lista)->poprzedni->konto.nazwisko) ==  0)
  309.                 {
  310.                     if(strcoll((*lista)->konto.imie,(*lista)->poprzedni->konto.imie) ==  1)
  311.                     {
  312.                         licznikZamian++;
  313.                         if((*lista)->nastepny != NULL)
  314.                             (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
  315.                         (*lista)->poprzedni->nastepny = (*lista)->nastepny;
  316.                         (*lista)->nastepny = (*lista)->poprzedni;
  317.                         (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
  318.                         (*lista)->nastepny->poprzedni = (*lista);
  319.                         if((*lista)->poprzedni != NULL)
  320.                             (*lista)->poprzedni->nastepny = (*lista);
  321.                     }else (*lista) = (*lista)->poprzedni;
  322.                 }else (*lista) = (*lista)->poprzedni;
  323.             }
  324.             for(i = 1; i < licznikElementow; i++)
  325.                 (*lista) = (*lista)->nastepny;
  326.         }while(licznikZamian != 0);
  327.         return 2;
  328.         break;
  329.     case numer:
  330.         do
  331.         {
  332.             licznikZamian = 0;
  333.             for(i = 1; i<licznikElementow; i++)
  334.             {
  335.                 if(strcoll((*lista)->konto.numer,(*lista)->poprzedni->konto.numer) ==  1)
  336.                 {
  337.                     licznikZamian++;
  338.                     if((*lista)->nastepny != NULL)
  339.                         (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
  340.                     (*lista)->poprzedni->nastepny = (*lista)->nastepny;
  341.                     (*lista)->nastepny = (*lista)->poprzedni;
  342.                     (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
  343.                     (*lista)->nastepny->poprzedni = (*lista);
  344.                     if((*lista)->poprzedni != NULL)
  345.                         (*lista)->poprzedni->nastepny = (*lista);
  346.  
  347.                 }else (*lista) = (*lista)->poprzedni;
  348.             }
  349.             for(i = 1; i < licznikElementow; i++)
  350.                 (*lista) = (*lista)->nastepny;
  351.         }while(licznikZamian != 0);
  352.         return 3;
  353.         break;
  354.     case grupa:
  355.         do
  356.         {
  357.             licznikZamian = 0;
  358.             for(i = 1; i<licznikElementow; i++)
  359.             {
  360.                 if(strcoll((*lista)->konto.grupa,(*lista)->poprzedni->konto.grupa) ==  1)
  361.                 {
  362.                     licznikZamian++;
  363.                     if((*lista)->nastepny != NULL)
  364.                         (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
  365.                     (*lista)->poprzedni->nastepny = (*lista)->nastepny;
  366.                     (*lista)->nastepny = (*lista)->poprzedni;
  367.                     (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
  368.                     (*lista)->nastepny->poprzedni = (*lista);
  369.                     if((*lista)->poprzedni != NULL)
  370.                         (*lista)->poprzedni->nastepny = (*lista);
  371.  
  372.                 }else if(strcoll((*lista)->konto.grupa,(*lista)->poprzedni->konto.grupa) ==  0)
  373.                 {
  374.                     if(strcoll((*lista)->konto.nazwisko,(*lista)->poprzedni->konto.nazwisko) ==  1)
  375.                     {
  376.                         licznikZamian++;
  377.                         if((*lista)->nastepny != NULL)
  378.                             (*lista)->nastepny->poprzedni = (*lista)->poprzedni;
  379.                         (*lista)->poprzedni->nastepny = (*lista)->nastepny;
  380.                         (*lista)->nastepny = (*lista)->poprzedni;
  381.                         (*lista)->poprzedni = (*lista)->nastepny->poprzedni;
  382.                         (*lista)->nastepny->poprzedni = (*lista);
  383.                         if((*lista)->poprzedni != NULL)
  384.                             (*lista)->poprzedni->nastepny = (*lista);
  385.                     }else (*lista) = (*lista)->poprzedni;
  386.                 }else (*lista) = (*lista)->poprzedni;
  387.             }
  388.             for(i = 1; i < licznikElementow; i++)
  389.                 (*lista) = (*lista)->nastepny;
  390.         }while(licznikZamian != 0);
  391.         return 4;
  392.         break;
  393.     default:
  394.         return 0;
  395.         break;
  396.     }
  397. }
  398. void intscanf(int* liczba)
  399. {
  400.     while(1)
  401.     {
  402.         if(!scanf("%d",liczba))
  403.         {
  404.             while('\n' != getchar())
  405.                 puts("Wpisano wartość spoza wyznaczonego zakresu. Spróbuj jeszcze raz.");
  406.         }
  407.         else break;
  408.     }
  409. }
  410. void wyszukiwanie(struct element **lista, char* fragment)
  411. {
  412.     int j;
  413.     int i = 0;
  414.     char* tab = (char*)malloc(40*sizeof(char));
  415.     char buforImie[20];
  416.     char buforNazwisko[40];
  417.     struct element *bufor = (struct element*)malloc(sizeof(struct element));
  418.     for(; (*lista)->nastepny != NULL; *lista = (*lista)->nastepny);
  419.     for(bufor = *lista; bufor != NULL; bufor = bufor->poprzedni)
  420.     {
  421.         for(j=0;j<20;j++)
  422.         {
  423.             if(bufor->konto.imie[j] > 96 && bufor->konto.imie[j] < 123)
  424.                 buforImie[j] = bufor->konto.imie[j];
  425.             else if(bufor->konto.imie[j] > 64 && bufor->konto.imie[j] < 96)
  426.                 buforImie[j] = bufor->konto.imie[j] + 32;
  427.             else if(bufor->konto.imie[j] == '\0')
  428.                 break;
  429.         }
  430.         for(j=0;j<40;j++)
  431.         {
  432.             if(bufor->konto.nazwisko[j] > 96 && bufor->konto.nazwisko[j] < 123)
  433.                 buforNazwisko[j] = bufor->konto.nazwisko[j];
  434.             else if(bufor->konto.nazwisko[j] > 64 && bufor->konto.nazwisko[j] < 96)
  435.                 buforNazwisko[j] = bufor->konto.nazwisko[j] + 32;
  436.             else if(bufor->konto.nazwisko[j] == '\0')
  437.                 break;
  438.         }
  439.         tab = strstr(buforImie, fragment);
  440.         if(tab != NULL)
  441.         {
  442.             pokazElement(bufor);
  443.             i++;
  444.         }
  445.         else
  446.         {
  447.             tab = strstr(buforNazwisko, fragment);
  448.             if(tab != NULL)
  449.             {
  450.                 pokazElement(bufor);
  451.                 i++;
  452.             }
  453.         }
  454.     }
  455.     free(bufor);
  456.     //free(tab);                    ###ODKOMENTOWANIE POWODUJE BŁĄD, WTF?
  457. }
  458. void wyszukiwanieGrupy(struct element **lista, char* fragment)
  459. {
  460.     int j;
  461.     char* tab;
  462.     char buforGrupa[20];
  463.     struct element *bufor;
  464.     for(; (*lista)->nastepny != NULL; *lista = (*lista)->nastepny);
  465.     for(bufor = *lista; bufor != NULL; bufor = bufor->poprzedni)
  466.     {
  467.         for(j=0;j<20;j++)
  468.         {
  469.             if(bufor->konto.grupa[j] > 96 && bufor->konto.grupa[j] < 123)
  470.                 buforGrupa[j] = bufor->konto.grupa[j];
  471.             else if(bufor->konto.grupa[j] > 64 && bufor->konto.grupa[j] < 96)
  472.                 buforGrupa[j] = bufor->konto.grupa[j] + 32;
  473.             else if(bufor->konto.grupa[j] == '\n')
  474.                 break;
  475.         }
  476.         tab = strstr(buforGrupa, fragment);
  477.         if(tab != NULL)
  478.             pokazElement(bufor);
  479.     }
  480. }
  481. void wpiszWyszukaj(struct element **lista, char** fragment, char co)
  482. {
  483.     int j = 0;
  484.     int i = 0;
  485.     char buforCiagu;
  486.     while(1)
  487.     {
  488.         buforCiagu = _getch();
  489.         if(buforCiagu != 13 && buforCiagu > 96 && buforCiagu < 123 || buforCiagu == 32)     //od 97 do 122 małe litery
  490.         {
  491.             (*fragment)[j] = buforCiagu;
  492.             for(i = 0; i <= j; i++)
  493.                 printf("%c",(*fragment)[i]);
  494.             puts("");
  495.             if(co == 'N')
  496.                 wyszukiwanie(lista, *fragment);
  497.             else if(co == 'G')
  498.                 wyszukiwanieGrupy(lista, *fragment);
  499.             puts("");
  500.             j++;
  501.         }
  502.         else if(buforCiagu != 13 && buforCiagu > 64 && buforCiagu < 96 || buforCiagu == 32)     //od 65 do 95 duże litery
  503.         {
  504.             buforCiagu += 32;
  505.             (*fragment)[j] = buforCiagu;
  506.             for(i = 0; i <= j; i++)
  507.                 printf("%c",(*fragment)[i] - 32);
  508.             puts("");
  509.             if(co == 'N')
  510.                 wyszukiwanie(lista, *fragment);
  511.             else if(co == 'G')
  512.                 wyszukiwanieGrupy(lista, *fragment);
  513.             puts("");
  514.             j++;
  515.         }
  516.         else if(buforCiagu == 13)                           //ENTER
  517.         {
  518.             puts("");
  519.             break;
  520.         }
  521.         else if(buforCiagu == 8 && j > 0)                           //BACKSPACE
  522.         {
  523.             (*fragment)[j] = '\0';
  524.             j--;
  525.             (*fragment)[j] = '\0';
  526.             for(i = 0; i <= j; i++)
  527.                 printf("%c",(*fragment)[i]);
  528.             puts("");
  529.             if(co == 'N')
  530.                 wyszukiwanie(lista, *fragment);
  531.             else if(co == 'G')
  532.                 wyszukiwanieGrupy(lista, *fragment);
  533.             puts("");
  534.         }
  535.     }
  536. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement