Advertisement
m4ly

Lab2 | Struktura | Tablica

Mar 9th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.95 KB | None | 0 0
  1. /*
  2. Author: Dawid Mocek
  3. Zadanie 17
  4.  
  5. Napisz program, który przechowuje dane użytkowników: imię, nazwisko, telefon, data urodzenia. Program powinien umożliwić:
  6.  
  7.     dodawanie elementów na kolejne miejsca
  8.     wyświetlanie bieżących elementów
  9.     sortowanie po nazwisku użytkownika
  10.     wyszukiwanie po nazwisku, telefonie, roku urodzenia.
  11. */
  12.  
  13.  
  14. #define TAB_SIZE 100
  15.  
  16. #include "stdio.h"
  17. #include "stdlib.h"
  18. #include "string.h"
  19.  
  20.  
  21. struct person {
  22.  char name[255];
  23.  char surname[255];
  24.  long int phone;
  25.  int bday;
  26.  bool flag;
  27. };
  28.  
  29. /* Persons array */
  30. person list[TAB_SIZE];
  31.  
  32. /* Show persons */
  33. void showPersons(person * arr, int size) {
  34.     int i = 0;
  35.        
  36.     for(i; i < size; i++ ) {
  37.         if(arr[i].flag == true) {
  38.                 printf("%i: %s\t%s\t%i\t%i\n", i, arr[i].name, arr[i].surname, arr[i].bday, arr[i].phone);
  39.             }
  40.         }
  41. }
  42.  
  43. /* Add person */
  44. void addPerson(person p, person * arr, int size) {
  45.     int i = 0;
  46.  
  47.     for(i; i < size; i++) {
  48.         if(arr[i].flag == false) {
  49.             arr[i] = p;
  50.             break;
  51.         }
  52.     }
  53. }
  54.  
  55. /* Sort */
  56. int strcmp_by_surname(const void *a, const void *b)
  57. {
  58.     struct person *ia = (struct person *)a;
  59.     struct person *ib = (struct person *)b;
  60.     return strcmp(ia->surname, ib->surname);
  61. }
  62.  
  63. void sortList(person * arr, int size) {
  64.     qsort(arr, size, sizeof(struct person), strcmp_by_surname);  
  65. }
  66.  
  67.  
  68. /* Find */
  69. bool findPerson(person p_search, int by_what, person * arr, int size) {
  70.     bool b = false;
  71.     int i = 0;
  72.     char s[255];
  73.     switch(by_what) {
  74.         /* Surname */
  75.         case 1:
  76.                 for(i; i < size; i++) {
  77.                     (strcmp(arr[i].surname, p_search.surname) == 0) ?  b = true : b;
  78.                 }
  79.                 break;
  80.         /* Phone */
  81.         case 2:
  82.             for(i; i < size; i++) {
  83.                   (arr[i].phone == p_search.phone) ? b = true : b;             
  84.                 }
  85.                 break;
  86.         /* Bday */
  87.         case 3:
  88.             for(i; i < size; i++) {
  89.                     (arr[i].bday == p_search.bday) ? b = true: b;
  90.                 }
  91.                 break;
  92.  
  93.         default:  break;
  94.  
  95.     }
  96.  
  97. return b;
  98. }
  99.  
  100. void printMenu() {
  101.     printf("1. Add user\n2. Show users\n3. Sort users by surname\n4. Search user\n5. Exit\n");
  102. }
  103.  
  104. void printSearchBy() {
  105.     printf("6. By surname\n7. By phone\n8. By B-day\n");
  106. }
  107.  
  108.  
  109. int main()
  110. {
  111.     person osobnik, person_tmp;
  112.     int choice = 0, choice2 = 0;
  113.     bool is_ok;
  114.  
  115.    
  116.  
  117.     while(choice != 5) {
  118.  
  119.     printMenu();
  120.  
  121.     scanf("%i", &choice);
  122.     system("cls");
  123.         switch(choice) {
  124.             /* add person */
  125.             case 1:
  126.                     printf("Name: ");
  127.                     scanf("%s", &osobnik.name);
  128.                     printf("Surname: ");
  129.                     scanf("%s", &osobnik.surname);
  130.                     printf("Date of(mmDDYYYY): ");
  131.                     scanf("%i", &osobnik.bday);
  132.                     printf("Phone number: ");
  133.                     scanf("%il", &osobnik.phone);
  134.                     osobnik.flag = true;
  135.  
  136.                     addPerson(osobnik, list, TAB_SIZE);
  137.                    
  138.                     break;
  139.             /* show persons */
  140.             case 2:
  141.                    
  142.                     printf("Persons list:\n");
  143.  
  144.                     showPersons(list, TAB_SIZE);
  145.  
  146.                     break;
  147.             /* sort persons by surname */
  148.             case 3:
  149.                     sortList(list, TAB_SIZE);
  150.                     break;
  151.             /* find person */      
  152.             case 4:    
  153.                 printSearchBy();
  154.  
  155.                 scanf("%i", &choice2);
  156.                 system("cls");
  157.                         switch(choice2) {
  158.  
  159.                             case 6:
  160.                                 printf("Give surname: ");
  161.                                 scanf("%s", &person_tmp.surname);
  162.  
  163.                                 is_ok = (findPerson(person_tmp, 1, list , TAB_SIZE)) ? printf("Found !\n") : printf("Not found person with \"%s\" surname.", person_tmp.surname);
  164.  
  165.                                 break;
  166.  
  167.                             case 7:
  168.                                 printf("Give phone: ");
  169.                                 scanf("%il", &person_tmp.phone);
  170.  
  171.                                 is_ok = findPerson(person_tmp, 2, list , TAB_SIZE) ? printf("Found !\n") : printf("Not found person with %i phone #.", person_tmp.phone);
  172.  
  173.                                 break;
  174.  
  175.                             case 8:
  176.                                 printf("Give bday: ");
  177.                                 scanf("%i", &person_tmp.bday);
  178.                                
  179.                                 is_ok = findPerson(person_tmp, 3, list , TAB_SIZE) ? printf("Found !\n") : printf("Not found person with %i B-day.", person_tmp.bday);
  180.  
  181.                                 break;
  182.  
  183.                             default: printf("Bad option\n"); break;
  184.                             }
  185.                    
  186.  
  187.                      break;
  188.             default: break;
  189.         }
  190.     }
  191.  
  192.     return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement