#include <stdio.h> #include <string.h> #include <stdlib.h> //include the prototype of exit() #include <stdbool.h> #define NUMLEN 11 #define NAMLEN 20 // name length #define PHOLEN 12 // max length of phone number #define MAILEN 20 // mail length // The following constants used in struct ADDRESS #define PROLEN 10 // province length #define CITLEN 25 // city length #define COULEN 25 // county length #define VILLEN 20 // village length int count = 0 ; // actual number of people int choice = 0 ; // user's choice used in the menu struct BIRTHDAY // struct birthday { int year ; int month ; int day ; } ; struct ADDRESS // struct address { char province[PROLEN] ; // store province address char city[CITLEN] ; // store city address char county[COULEN] ; // store county address char village[VILLEN] ; // store village address } ; struct STUDENT // struct student { char name[NAMLEN] ; // store name char num[NUMLEN] ; // store school number int age ; char sex ; struct BIRTHDAY bir ; struct ADDRESS addr ; char phonum[PHOLEN] ; //store phone number char mail[MAILEN] ; // store e-mail address } ; struct STUDENT stu[100] ; // the number of student void Add(void) ; void Get_bir(void) ; // used in Add() to get birthday bool check(struct BIRTHDAY bir) ; // used in Cet_bir() to check bir's validity bool leap_year(int year) ; // used in check() to confirm whether it is a leap year void Get_addr(void) ; // used in Add() to get address void Get_phonum(void) ; // used in Add() to get phone number void Get_mail(void) ; // used in Add() to get e-mail address ///////////////////////////////////////////////////////////////// void search_name(void) ; // search by name void search_num(void) ; // search by number (school number) void Display(int i) ; // display the information of someone void Search(void) ; ///////////////////////////////////////////////////////////////// void Modify(void) ; // used to modify student's information void change(int i) ; // used in Modify() bool yes_or_no(void) ; // used in change() void change_name(void) ; // change information used in search by name void change_num(void) ; // change information used in search by number bool confirm(void) ; // to acquire the user's confirm void Name(int i) ; // modify name, the following all used in change() void Num(int i) ; // ... void Age(int i) ; // ... void Sex(int i) ; // ... void Bir(int i) ; // ... void Addr(int i) ; // ... void Phonum(int i) ; // ... void Mail(int i) ; // ... ///////////////////////////////////////////////////////////////// void Delete(void) ; // delete information void delete_name(void) ; // delete by name void delete_num(void) ; // delete by number void del(int i) ; //////////////////////////////////////////////////////////////// void Sort(void) ; //////////////////////////////////////////////////////////////// void Menu(void) ; int get_ch() ; // get user's directive void Show(void) ; int main(void) { Show() ; Menu() ; while (1) { choice = get_ch() ; switch (choice) { case 1: Add() ; break ; case 2: Delete() ; break ; case 3: Modify() ; break ; case 4: Search() ; break ; case 5: for (int n = 0; n < count; ++ n) Display(n) ; break ; case 6: Sort() ; break ; case 7: Menu() ; break ; case 0: exit(0) ; default: printf("Incorrect indirective!\n") ; } } return 0 ; } void Add(void) { printf("Enter your name: ") ; scanf("%s",stu[count].name) ; getchar() ; printf("Enter your school number: ") ; while(!(scanf("%s",stu[count].num) && strlen(stu[count].num) == NUMLEN - 1)) printf("Enter your school number(ten digits),try again: ") ; getchar() ; printf("Enter your age: ") ; while(!(scanf("%d",&stu[count].age) && stu[count].age >= 15 && stu[count].age <= 30)) printf("Age ranges from 15 to 30, try again: ") ; getchar() ; printf("Enter your sex: ") ; while(!(scanf("%c",&stu[count].sex) && (stu[count].sex == 'm' || stu[count].sex == 'f'))) printf("The sex is %c?\n",stu[count].sex) ; getchar() ; Get_bir() ; // get birthday getchar() ; Get_addr() ; // get address Get_phonum() ; // get phone number Get_mail() ; count ++ ; printf("Done!\n") ; } void Get_bir(void) { printf("Enter your birthday like this: 2015 6 24\n") ; printf("Enter your birthday: ") ; while(!(scanf("%d%d%d", &stu[count].bir.year, &stu[count].bir.month, &stu[count].bir.day) && check(stu[count].bir))) printf("Wrong !! Try again: ") ; } bool check (struct BIRTHDAY bir) { if (2 == bir.month) if(leap_year(bir.year)) if(bir.day <= 29) return true ; else { printf("%d year %d month has %d days?\n",bir.year,bir.month,bir.day) ; printf(" 2 month has no more than 29 days even the year is a leap year!!\n") ; return false ; } else if (bir.day <= 28) return true ; else { printf("%d year is not a leap year, so 2 month shouldn't have more than 28 days\n",bir.year) ; return false ; } else if(bir.day > 31) { printf("Have you ever seen a month that has %d days?\n", bir.day) ; return false ; } if(31 == bir.day) if((bir.month < 8 && bir.month%2 == 1) || (bir.month >= 8 && bir.month % 2 == 0)) return true ; else { printf("%d month is not a solar month,so it shouldn't have more than 30 days.\n",bir.month) ; return false ; } else return true ; } bool leap_year(int year) { return ((0 == year%4 && year% 100 != 0) || (0 == year%400)) ? true : false ; } void Get_addr(void) { printf("Enter your province: ") ; while(!scanf("%s",stu[count].addr.province)) printf("Something went wrong!! Try again: ") ; getchar() ; printf("Enter your city: ") ; while(!scanf("%s",stu[count].addr.city)) printf("Something went wrong!! Try again: ") ; getchar() ; printf("Enter your county: ") ; while(!scanf("%s",stu[count].addr.county)) printf("Something went wrong!! Try again: ") ; getchar() ; printf("Enter your village: ") ; while(!scanf("%s",stu[count].addr.village)) printf("Something went wrong!! Try again: ") ; getchar() ; } void Get_phonum(void) { printf("Enter phone number: ") ; while(!(scanf("%s",stu[count].phonum) && (strlen(stu[count].phonum) == PHOLEN - 1 || strlen(stu[count].phonum) == PHOLEN - 5))) printf("Wrong!! Try again: ") ; getchar() ; } void Get_mail(void) { printf("Enter e-mail address: ") ; while(!scanf("%s",stu[count].mail)) printf("Wrong!! Try again: ") ; getchar() ; } void Search(void) { if(!count) printf("No person!! Please add first.\n") ; else { int choice_12 = 0 ; printf("Search by name, input 1(one); by number, input 2\n") ; printf("Enter your choice: ") ; while(!(scanf("%d",&choice_12) && (1 == choice_12 || 2 == choice_12))) printf("Enter 1 or 2, Try again: ") ; switch(choice_12) { case 1: search_name() ; break ; case 2: search_num() ; break ; } } } void search_name(void) { char temp[NAMLEN] ; printf("Enter the name: ") ; while(scanf("%s",temp)) { int flag = 0 ; for (int i = 0; i < count; ++ i) if (0 == strcmp(temp,(stu+i)->name)) { Display(i) ; flag = 1 ; break ; } if (!flag) { printf("No such student!!\n") ; printf("Enter the name carefully: ") ; continue ; } if (flag) break ; } } void search_num(void) { char temp[NUMLEN] ; printf("Enter the number: ") ; while(scanf("%s",temp)) { int flag = 0 ; for (int i = 0; i < count; ++ i) if (0 == strcmp(temp,(stu+i)->num)) { Display(i) ; flag = 1 ; break ; } if (!flag) { printf("No such person!!\n") ; printf("Enter the number carefully: ") ; continue ; } if (flag) break ; } } void Display(int i) { printf("name \tnumber\tage sex\n") ; printf("%s \t%s\t%d %c\n",stu[i].name,stu[i].num,stu[i].age,stu[i].sex) ; printf("birthday: %d %d %d\n",stu[i].bir.year,stu[i].bir.month,stu[i].bir.day) ; printf("address: %s %s %s %s\n",stu[i].addr.province,stu[i].addr.city,stu[i].addr.county,stu[i].addr.village) ; printf("phone number: %s\te-mail address: %s\n",stu[i].phonum,stu[i].mail) ; } /////////////////////////////////////////////////////////// void Modify(void) { if (!count) printf("No person!! please add first.\n") ; else { int choice_12 = 0 ; printf("Search by name, input 1; by number, input 2\n") ; printf("Enter your choice now: ") ; while (!(scanf("%d",&choice_12) && (1 == choice_12 || 2 == choice_12))) printf("Enter 1 or 2. Try again: ") ; switch (choice_12) { case 1: change_name() ; break ; case 2: change_num() ; break ; } } } void change(int i) { printf("1) name \t2) number \t3) age \t4) sex\n") ; printf("5) birthfay \t6) address\t7) phonum\t8) e-mail\n") ; int choice_18 = 0 ; bool FLAG = true ; while(FLAG) { printf("Enter the corresponding option: ") ; while(!(scanf("%d",&choice_18) && choice_18 < 9 && choice_18 > 0)) printf("Enter 1 or 2 or ... or 8. Try again: ") ; switch (choice_18) { case 1: Name(i) ; FLAG = yes_or_no() ; break ; case 2: Num(i) ; FLAG = yes_or_no() ; break ; case 3: Age(i) ; FLAG = yes_or_no() ; break ; case 4: Sex(i) ; FLAG = yes_or_no() ; break ; case 5: Bir(i) ; FLAG = yes_or_no() ; break ; case 6: Addr(i) ; FLAG = yes_or_no() ; break ; case 7: Phonum(i) ; FLAG = yes_or_no() ; break ; case 8: Mail(i) ; FLAG = yes_or_no() ; break ; } } } bool yes_or_no(void) { getchar() ; char flag = '0' ; printf("Continue ?(y\\n): ") ; while(!(scanf("%c",&flag) && ('y' == flag || 'Y' == flag || 'n' == flag || 'N' == flag))) printf("Enter 'y'('Y') or 'n'('N'). Try again: ") ; if ('y' == flag || 'Y' == flag) return true ; else return false ; } void change_name(void) { char temp[NAMLEN] ; printf("Enter the name: ") ; while(scanf("%s",temp)) { int flag = 0 ; for (int i = 0; i < count; ++ i) if (0 == strcmp(temp,(stu+i)->name)) { change(i) ; flag = 1 ; break ; } if (!flag) { printf("No such student!!\n") ; printf("Enter the name carefully: ") ; continue ; } else break ; } } void change_num(void) { char temp[NUMLEN] ; printf("Enter the number: ") ; while (scanf("%s",temp)) { int flag = 0 ; for (int i = 0; i < count; ++ i) if (0 == strcmp(temp,(stu+i)->num)) { change(i) ; flag = 1 ; break ; } if (!flag) { printf("No such person!!\n") ; printf("Enter the number carefully: ") ; continue ; } else break ; } } void Name(int i) { char temp_name[NAMLEN] ; while(!scanf("%s",temp_name)) printf("Enter carefully! Try again: ") ; getchar() ; if (confirm()) { strcpy(stu[i].name,temp_name) ; printf("Update successfully!\n") ; } else printf("Information remains unchanged!\n") ; } void Num(int i) { char temp_num[NUMLEN] ; while(!(scanf("%s",temp_num) && 10 == strlen(temp_num))) printf("Enter carefully! Try again: ") ; getchar() ; if (confirm()) { strcpy(stu[i].num,temp_num) ; printf("Update successfully!\n") ; } else printf("Information remains unchanged!\n") ; } void Age(int i) { int temp_age = 0 ; while(!(scanf("%d",&temp_age) && temp_age < 31 && temp_age > 14)) printf("Age ranges from 15 to 30. Try again: ") ; getchar() ; if(confirm()) { stu[i].age = temp_age ; printf("Update successfully!\n") ; } else printf("Information remains unchanged!\n") ; } void Sex(int i) { char temp_sex = '0' ; while(!(scanf("%c",&temp_sex) && ('m' == temp_sex || 'f' == temp_sex))) printf("Enter 'm' or 'f'. Try again: ") ; getchar() ; if(confirm()) { stu[i].sex = temp_sex ; printf("Update successfully!\n") ; } else printf("Information remains unchanged!\n") ; } void Bir(int i) { struct BIRTHDAY temp_bir ; printf("Enter the birthday like this: 2048 8 15\n") ; printf("Enter the birthday now: ") ; while(!(scanf("%d %d %d", &temp_bir.year, &temp_bir.month, &temp_bir.day) && check(temp_bir))) printf("Enter carefully. Try again: ") ; getchar() ; if(confirm()) { // the below three statements can be substituted with stu[i].bir = temp_bir stu[i].bir.year = temp_bir.year ; stu[i].bir.month = temp_bir.month ; stu[i].bir.day = temp_bir.day ; printf("Update successfully!\n") ; } else printf("Information remains unchanged!\n") ; } void Addr(int i) { struct ADDRESS temp_addr ; printf("Enter the address like this: henan zhengzhou hongxing zhaozhuang\n") ; printf("Enter the new address now: ") ; while(!scanf("%s %s %s %s",temp_addr.province, temp_addr.city, temp_addr.county, temp_addr.village)) printf("Enter carefully. Try again: ") ; getchar() ; if(confirm()) { // the below four statements can be substituted with stu[i].addr = temp_addr strcpy(stu[i].addr.province,temp_addr.province) ; strcpy(stu[i].addr.city,temp_addr.city) ; strcpy(stu[i].addr.county,temp_addr.county) ; strcpy(stu[i].addr.village,temp_addr.village) ; printf("Update successfully!\n") ; } else printf("Information remains unchanged!\n") ; } void Phonum(int i) { char temp_phonum[PHOLEN] ; printf("Enter the new phone number now: ") ; while(!(scanf("%s",temp_phonum) && (7 == strlen(temp_phonum) || 11 == strlen(temp_phonum)))) printf("Enter carefully. Try again: ") ; getchar() ; if(confirm()) { strcpy(stu[i].phonum,temp_phonum) ; printf("Update successfully!\n") ; } else printf("Information remains unchanged!\n") ; } void Mail(int i) { char temp_mail[MAILEN] ; printf("Enter the new e-mail address: ") ; while(!scanf("%s",temp_mail)) printf("Enter carefully. Try again: ") ; getchar() ; if(confirm()) { strcpy(stu[i].mail,temp_mail) ; printf("Update successfully!\n") ; } else printf("Information remains unchanged.\n") ; } bool confirm(void) { char flags = '0' ; printf("Do you really want to change that ?(y\\n): ") ; while (!(scanf("%c",&flags) && ('y' == flags || 'Y' == flags || 'n' == flags || 'N' == flags))) printf("Enter 'y'('Y') or 'n'('N'). Try again: ") ; if ('y' == flags || 'Y' == flags) return true ; else return false ; } ////////////////////////////////////////////// void Delete(void) { if (!count) printf("No person!! Please add first.\n") ; else { int choice_12 = 0 ; printf("Search by name, input 1; by name, input 2\n") ; printf("Enter your choice now: ") ; while(!(scanf("%d",&choice_12) && (1 == choice_12 || 2 == choice_12))) printf("Enter 1 or 2. Try again: ") ; switch (choice_12) { case 1: delete_name() ; break ; case 2: delete_num() ; break ; } } } void del(int i) { if (confirm()) { for (int j = i; j < count; ++ j) stu[j] = stu[j+1] ; -- count ; // important printf("Delete successfully!\n") ; } else printf("Information remains unchanged!\n") ; } void delete_name(void) { char temp[NAMLEN] ; printf("Enter the name: ") ; while(scanf("%s",temp)) { int flag = 0 ; for (int i = 0; i < count; ++ i) if (0 == strcmp(temp,(stu+i)->name)) { del(i) ; flag = 1 ; break ; } if (!flag) { printf("No such student!!\n") ; printf("Enter the name carefully: ") ; continue ; } else break ; } } void delete_num(void) { char temp[NUMLEN] ; printf("Enter the number: ") ; while (scanf("%s",temp)) { int flag = 0 ; for (int i = 0; i < count; ++ i) if (0 == strcmp(temp,(stu+i)->num)) { del(i) ; flag = 1 ; break ; } if (!flag) { printf("No such person!!\n") ; printf("Enter the number carefully: ") ; continue ; } else break ; } } ////////////////////////////////////////////// void Sort(void) { if(!count) printf("No person!! please add first.\n") ; else { struct STUDENT temp_stu ; for (int i = 0; i < count; ++ i) for (int j = i; j < count; ++ j) if (strcmp(stu[i].num,stu[j].num) >= 0) { temp_stu = stu[i] ; stu[i] = stu[j] ; stu[j] = temp_stu ; } printf("Sort successfully.\n") ; } } void Menu(void) { printf("\t\t 1) Add 2) Delete 3) Modify 4) Search\n") ; printf("\t\t 5) Output 6) Sort 7) Menu 0) Exit\n") ; } int get_ch() { int choi=0 ; printf("Enter your choice: ") ; while (!(scanf("%d",&choi) != EOF && choi >= 0 && choi < 8)) printf("Enter integers from 0 to 7. Try again: ") ; return choi ; } void Show(void) { printf("\t\t\t|||||||||||||||||||||||||||||||||\n") ; printf("\t\t\t|| ||\n") ; printf("\t\t\t|| WELCOME TO COME HERE ||\n") ; printf("\t\t\t|| ||\n") ; printf("\t\t\t|||||||||||||||||||||||||||||||||\n\n") ; } gcc -o stucode stucode.c -Wall -std=c99