1. 程式人生 > 程式設計 >C語言實現簡易通訊錄功能

C語言實現簡易通訊錄功能

本文例項為大家分享了C語言實現簡易通訊錄的具體程式碼,供大家參考,具體內容如下

這兩天用C語言編寫了一個簡易版通訊錄(學生資訊管理) ,大致功能有新增資訊檢視資訊(自動按姓名排序,printf輸出帶顏色字型),查詢資訊(按姓名查詢),刪除資訊(輸入姓名刪除相關資訊),修改資訊(輸入修改人的名字,可選擇修改其任意資訊)和退出

#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請選擇:");
  printf("\n\t\t\t\t\t1.新增資訊");
  printf("\n\t\t\t\t\t2.檢視資訊");
  printf("\n\t\t\t\t\t3.查詢資訊");
  printf("\n\t\t\t\t\t4.刪除資訊");
  printf("\n\t\t\t\t\t5.修改資訊");
  printf("\n\t\t\t\t\t6.退出");
  printf("\n\t************************************************************************");
 
}
 
/*新增資訊*/
void AddInfo(STU *s[])
{
  system("clear");
  printf("姓名 學號 年齡\n");
  printf("------------------------\n");
  printf("請輸入資訊:(bye結束新增)\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++;
  }
}
 
/*檢視資訊(按姓名排序)*/
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();
}
 
/*查詢資訊*/
 
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,age:%d\n",s[i]->age);
    }
  }
  
  if(n == 0)
  {
    printf("不存在!\n");
  }
}
 
void SearchInfo(STU *s[])
{
  system("clear");
  char *name = (char *)malloc(sizeof(char));
 
  printf("請輸入要查詢的人的名字:\n");
  scanf("%s",name);
 
  Search_name(name,s);
}
 
/*刪除資訊*/
 
void DeleteInfo(STU *s[])
{
  system("clear");
  int i,n = 0,j;
  char del_name[10];
 
  printf("請輸入要刪除的人的名字:\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("要刪除的人不存在!\n");
  }
  else
  {
    printf("刪除成功!\n");
  }
}
 
/*修改資訊*/
void Change_name(char *name,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("該學生的資訊如下:");
      printf("name:%s,s[i]->age);
      printf("----------------------------\n");
      printf("請選擇要修改的內容(1.name 2.num 3.age):\n");
      scanf("%d",&choice);
      switch(choice)
      {
        case 1:
          printf("請輸入新的名字:\n");
          scanf("%s",new_name);
          strcpy(s[i]->name,new_name);
          break;
        case 2:
          printf("請輸入新的學號:\n");
          scanf("%d",&new_num);
          s[i]->num = new_num;
          break;
        case 3:
          printf("請輸入新的年齡:\n");
          scanf("%d",&new_age);
          s[i]->age = new_age;
          break;
      }
    }
  }
  
  if(n == 0)
  {
    printf("不存在!\n");
  }
}
 
void ChangeInfo(STU *s[])
{
  system("clear");
  char *name = (char *)malloc(sizeof(char));
 
  printf("請輸入要修改的人的名字:\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;
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。