C language realizes the simple address book function

  • 2020-09-28 09:05:45
  • OfStack

This article shares the specific code of C language for your reference. The specific content is as follows

These two days with C language to write a simple address book (student information management), broadly function has added information, check the information (with automatic sort by name, printf output color font), to find information (by name lookup), delete information (input name delete information), modify the information (type the name of the modifier, can choose to modify its arbitrary information) and exit.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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 Please select a :");
 printf("\n\t\t\t\t\t1. Add information ");
 printf("\n\t\t\t\t\t2. Check the information ");
 printf("\n\t\t\t\t\t3. To find information ");
 printf("\n\t\t\t\t\t4. Delete the information ");
 printf("\n\t\t\t\t\t5. Modify the information ");
 printf("\n\t\t\t\t\t6. exit ");
 printf("\n\t************************************************************************");
 
}
 
/* Add information */
void AddInfo(STU *s[])
{
 system("clear");
 printf(" The name   Student id   age \n");
 printf("------------------------\n");
 printf(" Please enter information: (bye Add the end )\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();
}
 
/* To 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(" There is no !\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 the 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 successful! \n");
 }
}
 
/* Modify the 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 you want to modify (1.name 2.num 3.age):\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 your new student number :\n");
     scanf("%d", &new_num);
     s[i]->num = new_num;
     break;
    case 3:
     printf(" Please enter your new age :\n");
     scanf("%d", &new_age);
     s[i]->age = new_age;
     break;
   }
  }
 }
 
 if(n == 0)
 {
  printf(" There is no !\n");
 }
}
 
void ChangeInfo(STU *s[])
{
 system("clear");
 char *name = (char *)malloc(sizeof(char));
 
 printf(" Please enter the name of the person to be modified :\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;
}

Related articles: