SoFunction
Updated on 2025-03-03

C language implements simple address book function

This article shares the specific code for implementing simple address book in C language for your reference. The specific content is as follows

I wrote one in C language these two daysSimple version of address book (student information management), roughly functions includeAdd informationView information(Automatically sort by name, printf output with color font),Find information(Find by name),Delete information(Enter a name to delete relevant information),Modify information(Enter the name of the modifyer and choose to modify any information) andquit

#include <>
#include <>
#include <>
#define SIZE 100
typedef struct student STU;
int person = 0;
 
struct student
{
 char name[10];
 int num;
 int age;
};
 
void welcome()
{
 system("clear");
 printf("\n\n\n\n\t\t\t========================================");
 printf("\n\n\t\t\t\t\tWelcome!\n");
 sleep(3);
}
 
void menu()
{
 system("clear");
 printf("\n\n\t************************************************************************");
 printf("\n\t\t\t\t\t\tPlease select:");
 printf("\n\t\t\t\t\t\t1.Add information");
 printf("\n\t\t\t\t\t\t2.View information");
 printf("\n\t\t\t\t\t\t3.Find information");
 printf("\n\t\t\t\t\t\t4.Delete information");
 printf("\n\t\t\t\t\t\t5.Modify information");
 printf("\n\t\t\t\t\t\t6.Exit");
 printf("\n\t************************************************************************");
 
}
 
/*Add information*/
void AddInfo(STU *s[])
{
 system("clear");
 printf("Name Student ID Age\n");
 printf("------------------------\n");
 printf("Please enter information:(byeEnd Add)\n");
 while(1)
 {
  s[person] = (STU*)malloc(sizeof(STU));
  if(NULL == s[person])
  {
   printf("malloc failure!\n");
  }
  scanf("%s", s[person]->name);
  if(!strcmp(s[person]->name, "bye"))
  {
   break;
  }
  scanf("%d%d", &s[person]->num, &s[person]->age);
  getchar();
  person++;
 }
}
 
/*View information (sorted by name)*/
void ShowAll(STU *s[])
{
 system("clear");
 int i, j;
 STU *q[1] = {0};
 q[0] = (STU *)malloc(sizeof(STU));
 
 
 printf("information:\n");
 
 for(i = 0; i < person; i++)
 {
  for(j = 0; j < person - 1 - i; j++)
  {
   if(strcmp(s[j]->name, s[j + 1]->name) > 0)
   {
    q[0] = s[j];
    s[j] = s[j + 1];
    s[j + 1] = q[0];
   }
  }
 }
 
 for(i = 0; i < person; i++)
 {
  printf("\e[1;35mname:%s, num:%d, age:%d\e[0m\n", s[i]->name, s[i]->num, s[i]->age);
 }
 sleep(3);
 getchar();
}
 
/*Find information*/
 
void Search_name(char *name, STU *s[])
{
 int i, n = 0;
 for(i = 0; i < person; i++)
 {
  if(strcmp(name, s[i]->name) == 0)
  {
   n++;
   printf("name:%s, num:%d, age:%d\n", s[i]->name, s[i]->num, s[i]->age);
  }
 }
 
 if(n == 0)
 {
  printf("Not exists!\n");
 }
}
 
void SearchInfo(STU *s[])
{
 system("clear");
 char *name = (char *)malloc(sizeof(char));
 
 printf("Please enter the name of the person you are looking for:\n");
 scanf("%s", name);
 
 Search_name(name, s);
}
 
/*Delete information*/
 
void DeleteInfo(STU *s[])
{
 system("clear");
 int i, n = 0, j;
 char del_name[10];
 
 printf("Please enter the name of the person you want to delete:\n");
 scanf("%s", del_name);
 getchar();
 getchar();
 
 for(i = 0; i < person; i++)
 {
  if(strcmp(del_name, s[i]->name) == 0)
  {
   n++;
   free(s[i]);
   for(j = i; j < person - 1; j++)
   {
    strcpy(s[j]->name, s[j + 1]->name);
    s[j]->num = s[j + 1]->num;
    s[j]->age = s[j + 1]->age;
   }
   person--;
  }
 }
 
 if(n == 0)
 {
  printf("The person to be deleted does not exist!\n");
 }
 else
 {
  printf("Delete successfully!\n");
 }
}
 
/*Modify information*/
void Change_name(char *name, STU *s[])
{
 int i, n = 0, choice;
 char *new_name = (char *)malloc(sizeof(char));
 int new_num, new_age;
 
 for(i = 0; i < person; i++)
 {
  if(strcmp(name, s[i]->name) == 0)
  {
   n++;
   printf("The student's information is as follows:");
   printf("name:%s, num:%d, age:%d\n", s[i]->name, s[i]->num, s[i]->age);
   printf("----------------------------\n");
   printf("Please select what to modify(  ):\n");
   scanf("%d", &choice);
   switch(choice)
   {
    case 1:
     printf("Please enter a new name:\n");
     scanf("%s", new_name);
     strcpy(s[i]->name, new_name);
     break;
    case 2:
     printf("Please enter a new student number:\n");
     scanf("%d", &new_num);
     s[i]->num = new_num;
     break;
    case 3:
     printf("Please enter a new age:\n");
     scanf("%d", &new_age);
     s[i]->age = new_age;
     break;
   }
  }
 }
 
 if(n == 0)
 {
  printf("Not exists!\n");
 }
}
 
void ChangeInfo(STU *s[])
{
 system("clear");
 char *name = (char *)malloc(sizeof(char));
 
 printf("Please enter the name of the person you want to modify:\n");
 scanf("%s", name);
 
 Change_name(name, s);
 
}
 
int main()
{
 struct student *s[SIZE] = {0};
 int choice;
 
 welcome();
 
 while(1)
 {
  menu();
  printf("\nPlease input choice:");
  scanf("%d", &choice);
 
  switch(choice)
  {
   case 1:
    AddInfo(s);
    break;
   case 2:
    ShowAll(s);
    break;
   case 3:
    SearchInfo(s);
    break;
   case 4:
    DeleteInfo(s);
    break;
   case 5:
    ChangeInfo(s);
    break;
   case 6:
    exit(0);
    break;
  }
 }
 
 return 0;
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.