学习内容:简易通讯录
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 1000
int person=0;
struct student
{
char name[100];
int age;
char sex;
};
typedef struct student STU;
void welcome()
{
printf("\t\t\033[46m*************************\n");
printf("\t\t*********Welcome!********\n");
printf("\t\t*************************\n");
}
void menu()
{
system("clear");
printf("\n");
printf("\t\t************************\n");
printf("\t\t1.添加 2.查看 \n");
printf("\t\t3.查找 4.删除 \n");
printf("\t\t5.修改 6.返回 \n");
printf("\t\t************************\n");
}
void Add(STU *s[])
{
printf("Please input:");
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",&s[person]->age);
person++;
}
}
void ShowAll(STU *s[])
{
int i,j;
STU *temp;
temp=(STU *)malloc(sizeof(STU)*64);
/* printf("The former information is:\n");
for(i=0;i<person;i++)
{
printf("name:%s age:%d\n",s[i]->name,s[i]->age);
}*/
for(i=0;i<person-1;i++)
{
for(j=0;j<person-i-1;j++)
{
if(strcmp(s[j]->name,s[j+1]->name)>0)
{
temp=s[j+1];
s[j+1]=s[j];
s[j]=temp;
}
}
}
printf("the later information is:\n");
for(i=0;i<person;i++)
{
printf("name:%s age:%d\n",s[i]->name,s[i]->age);
}
getchar();
getchar();
}
void Find(STU *s[])
{
int i=0;
char *p;
p=(char *)malloc(sizeof(char)*64);
printf("Please input the name:");
scanf("%s",p);
while(1)
{
if(strcmp(s[i]->name,p)==0)
{
printf("name:%s age:%d",s[i]->name,s[i]->age);
break;
}
else
{
i++;
}
}
}
void Delete(STU *s[])
{
int i,j;
char *p;
p=(char *)malloc(sizeof(char)*64);
printf("Please input name you want to delete:\n");
scanf("%s",p);
for(i=0;i<person-1;i++)
{
if(strcmp(s[i]->name,p)==0)
{
free(s[i]);
for(j=i;j<person-i;j++)
{
strcpy(s[j]->name,s[j+1]->name);
s[j]->age=s[j+1]->age;
}
person--;
}
}
}
void Correct(STU *s[])
{
int i=0;
char *q;
q=(char *)malloc(sizeof(char)*64);
printf("Please input the name you want to change:\n");
scanf("%s",q);
for(i=0;i<person;i++)
{
if(strcmp(s[i]->name,q)==0)
{
printf("Please input the name after change:\n");
scanf("%s",s[i]->name);
printf("Please input the age after change:\n");
scanf("%d",&s[i]->age);
printf("name:%s age:%d\n",s[i]->name,s[i]->age);
break;
}
}
printf("failure!");
}
int main()
{
STU *stu[SIZE]={0};
int choice;
welcome();
while(1)
{
menu();
scanf("%d",&choice);
switch(choice)
{
case 1:Add(stu);
break;
case 2:ShowAll(stu);
break;
case 3:Find(stu);
break;
case 4:Delete(stu);
break;
case 5:Correct(stu);
break;
case 6:exit(0);
break;
}
}
return 0;
}
删除那部分还有问题,最后一个名字前移时会变成乱码