Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Dawid Mocek
- Zadanie 17
- Napisz program, który przechowuje dane użytkowników: imię, nazwisko, telefon, data urodzenia. Program powinien umożliwić:
- dodawanie elementów na kolejne miejsca
- wyświetlanie bieżących elementów
- sortowanie po nazwisku użytkownika
- wyszukiwanie po nazwisku, telefonie, roku urodzenia.
- */
- #define TAB_SIZE 100
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- struct person {
- char name[255];
- char surname[255];
- long int phone;
- int bday;
- bool flag;
- };
- /* Persons array */
- person list[TAB_SIZE];
- /* Show persons */
- void showPersons(person * arr, int size) {
- int i = 0;
- for(i; i < size; i++ ) {
- if(arr[i].flag == true) {
- printf("%i: %s\t%s\t%i\t%i\n", i, arr[i].name, arr[i].surname, arr[i].bday, arr[i].phone);
- }
- }
- }
- /* Add person */
- void addPerson(person p, person * arr, int size) {
- int i = 0;
- for(i; i < size; i++) {
- if(arr[i].flag == false) {
- arr[i] = p;
- break;
- }
- }
- }
- /* Sort */
- int strcmp_by_surname(const void *a, const void *b)
- {
- struct person *ia = (struct person *)a;
- struct person *ib = (struct person *)b;
- return strcmp(ia->surname, ib->surname);
- }
- void sortList(person * arr, int size) {
- qsort(arr, size, sizeof(struct person), strcmp_by_surname);
- }
- /* Find */
- bool findPerson(person p_search, int by_what, person * arr, int size) {
- bool b = false;
- int i = 0;
- char s[255];
- switch(by_what) {
- /* Surname */
- case 1:
- for(i; i < size; i++) {
- (strcmp(arr[i].surname, p_search.surname) == 0) ? b = true : b;
- }
- break;
- /* Phone */
- case 2:
- for(i; i < size; i++) {
- (arr[i].phone == p_search.phone) ? b = true : b;
- }
- break;
- /* Bday */
- case 3:
- for(i; i < size; i++) {
- (arr[i].bday == p_search.bday) ? b = true: b;
- }
- break;
- default: break;
- }
- return b;
- }
- void printMenu() {
- printf("1. Add user\n2. Show users\n3. Sort users by surname\n4. Search user\n5. Exit\n");
- }
- void printSearchBy() {
- printf("6. By surname\n7. By phone\n8. By B-day\n");
- }
- int main()
- {
- person osobnik, person_tmp;
- int choice = 0, choice2 = 0;
- bool is_ok;
- while(choice != 5) {
- printMenu();
- scanf("%i", &choice);
- system("cls");
- switch(choice) {
- /* add person */
- case 1:
- printf("Name: ");
- scanf("%s", &osobnik.name);
- printf("Surname: ");
- scanf("%s", &osobnik.surname);
- printf("Date of(mmDDYYYY): ");
- scanf("%i", &osobnik.bday);
- printf("Phone number: ");
- scanf("%il", &osobnik.phone);
- osobnik.flag = true;
- addPerson(osobnik, list, TAB_SIZE);
- break;
- /* show persons */
- case 2:
- printf("Persons list:\n");
- showPersons(list, TAB_SIZE);
- break;
- /* sort persons by surname */
- case 3:
- sortList(list, TAB_SIZE);
- break;
- /* find person */
- case 4:
- printSearchBy();
- scanf("%i", &choice2);
- system("cls");
- switch(choice2) {
- case 6:
- printf("Give surname: ");
- scanf("%s", &person_tmp.surname);
- is_ok = (findPerson(person_tmp, 1, list , TAB_SIZE)) ? printf("Found !\n") : printf("Not found person with \"%s\" surname.", person_tmp.surname);
- break;
- case 7:
- printf("Give phone: ");
- scanf("%il", &person_tmp.phone);
- is_ok = findPerson(person_tmp, 2, list , TAB_SIZE) ? printf("Found !\n") : printf("Not found person with %i phone #.", person_tmp.phone);
- break;
- case 8:
- printf("Give bday: ");
- scanf("%i", &person_tmp.bday);
- is_ok = findPerson(person_tmp, 3, list , TAB_SIZE) ? printf("Found !\n") : printf("Not found person with %i B-day.", person_tmp.bday);
- break;
- default: printf("Bad option\n"); break;
- }
- break;
- default: break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement