C language design simple phone book
- 2020-06-23 01:14:16
- OfStack
This paper shares the specific code of C language for your reference. The specific content is as follows
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <getch.h>
typedef struct Contact{ // Define the contact structure
char name[20]; // The name
char sex; // gender
char tel[12]; // The phone
}Contact;
Contact contacts[100];
void show_contact(Contact* conp){ // Display contact information
printf(" Name: %s\t Gender: %s\t Telephone: %s\n",conp->name,'w'==conp->sex?" female ":" male ",conp->tel);
}
void scan_contact(Contact* conp){ // Enter contact information
printf(" Please enter your name, gender ( w Female: m : Male), Telephone: ");
scanf("%s%s%s",conp->name,&conp->sex,conp->tel);
}
void add_contacts(void){ // Add contacts
for(int i=0;i<100;i++){
if(0 == contacts[i].sex){
scan_contact(contacts+i);
printf(" Added successfully! \n");
return;
}
}
printf(" The addition is full \n");
}
void del_contacts(void){ // Delete contacts
char str[20] = {};
printf(" Please enter the deletion name: \n");
scanf("%s",str);
for(int i=0;i<100;i++){
if(0 == strcmp(str,contacts[i].name)){
contacts[i].sex = 0;
printf(" Contact deleted successfully \n");
return;
}
}
printf(" The contact does not exist \n");
}
void find_contacts(void){ // Find contacts
char str[20] = {};
printf(" Please enter the phone number you want to check \n");
scanf("%s",str);
getchar();
for(int i=0;i<100;i++){
if(strstr(contacts[i].tel,str)){
show_contact(contacts+i);
}
}
printf(" Please enter any key to continue ...\n");
getch();
}
void list_contacts(void){ // Display contact information
for(int i=0;i<100;i++){
if(contacts[i].sex){
show_contact(contacts+i);
}
}
printf(" Please enter any key to continue ...\n");
getch();
}
void change_contacts(void){ // Modify contact information
char str[20] = {};
printf(" Please enter the name of the contact to be modified: \n");
scanf("%s",str);
for(int i=0;i<100;i++){
if(0 == strcmp(str,contacts[i].name)){
show_contact(contacts+i);
scan_contact(contacts+i);
return;
}
}
printf(" No contact to modify was found ");
}
char menu(void){
system("clear");
printf(" Welcome to the telephone box \n");
printf("--------------\n");
printf("1 , adding contacts \n");
printf("2 , delete contacts \n");
printf("3 , modify contact information \n");
printf("4 , find contacts \n");
printf("5 , display all contacts \n");
printf("--------------\n");
printf(" Please enter the instruction: ");
char cmd = getch();
printf("%c\n",cmd);
return cmd;
}
int main(){
while(true){
switch(menu()){
case '1':add_contacts(); break;
case '2':del_contacts(); break;
case '3':change_contacts(); break;
case '4':find_contacts(); break;
case '5':list_contacts(); break;
//case '6':exit(); break;
default: printf("cmd error!\n");
}
}
}
//------------------------------------ conclusion ------------------------------------------
// Add and delete contacts breach: can be selective return value to achieve the addition and deletion.
// Find contacts strstr() Function:
//strstr(str1,str2) The function evaluates a string str2 Whether it is str1 The substring. If so, the function returns str2 in str1 The address that first appears in; Otherwise, return NULL .
// So look for contacts can only make phone calls 1 You can find the contacts in the section.
// The downside: You can't have a previously saved contact every time you open it.
// Optimization: You can save contacts to a file and open the file when the program opens.