#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10000
struct student
{
char name[20];
int age;
char sex;
int id;
};
int person = 0;
void welcome(); //欢迎页面
void menu(); //菜单
void addinfo(struct student *stu[]); //录入信息
void showall(struct student *stu[]); //显示信息
void seek(struct student *stu[]); //查找信息
void modify(struct student *stu[]); //修改信息
void delete(struct student *stu[]);
int main()
{
int choice;
struct student *stu[SIZE] = {0};
welcome();
while(1)
{
menu();
printf("请输入功能选项:\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
addinfo(stu);
break;
case 2: //显示
showall(stu);
break;
case 3:seek(stu); //查找
break;
case 4:modify(stu); //修改
break;
case 5:delete(stu); //删除
break;
case 6:
printf("羊肉就是好吃!\n");
exit(0);
break; //退出
}
}
//printf("please input :\n");
// scanf("%s%d %c%d", stu[i].name, &stu[i].age, &stu[i].sex, &stu[i].id);
// printf("%s %d %c %d\n", stu[i].name, stu[i].age, stu[i].sex, stu[i].id);
return 0;
}
void welcome()
{
system("clear");
printf("\n\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\n欢迎来到青青草原羊羊大世界\n");
printf("\n\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
sleep(1);
}
void menu()
{
system("clear");
printf("\n\n\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("1、添加联系人 2、查看联系人信息\n");
printf("3、查找联系人 4、修改联系人信息\n");
printf("5、Shut Down! 6、退出羊羊养殖场\n");
printf("******************************************************************\n");
}
void addinfo(struct student *stu[])
{
printf("请输入添加联系人的信息(name、age、sex、id):\n");
while(1)
{
stu[person] = (struct student *)malloc(sizeof(struct student));
scanf("%s", stu[person]->name);
if(strcmp(stu[person]->name, "ok") == 0)
{
break;
}
scanf("%d", &stu[person]->age);
getchar();
scanf("%c", &stu[person]->sex);
getchar();
scanf("%d", &stu[person]->id);
person ++;
}
}
void showall(struct student *stu[])
{
int i, j;
struct student *tmp[1];
tmp[0] = (struct student *)malloc(sizeof(struct student));
for (i = 0; i < person - 1; i++)
{
for (j = 0; j < person - i - 1; j++)
{
if(strcmp(stu[j+1]->name, stu[j]->name) > 0)
{
tmp[0] = stu[j+1];
stu[j+1] = stu[j];
stu[j] = tmp[0];
}
}
}
printf("联系人信息如下:\n");
for(i = 0; i < person; i++ )
{
printf("%s %d %c %d\n",stu[i]->name, stu[i]->age, stu[i]->sex, stu[i]->id);
}
sleep(3);
}
void seek(struct student *stu[]) // search
{
char str[20] = {0};
int i = 0;
printf("请输入联系人姓名:\n");
scanf("%s", str);
for(i = 0; i < person; i++)
{
if( strcmp (stu[i] -> name, str) == 0)
{
printf("查找成功!\n");
printf("%s %d %c %d\n", stu[i] -> name, stu[i] -> age, stu[i] -> sex, stu[i] -> id);
sleep(3);
break;
}
if( (i + 1) == person)
{
printf("查找失败!\n");
}
}
}
void modify(struct student *stu[])
{
char str[20] = {0};
int i;
printf("请输入您想修改的联系人姓名: \n");
scanf("%s", str);
for(i = 0; i < person; i++)
{
if ( strcmp(stu[i] -> name, str) == 0)
{
printf("这位仁兄的原始信息:%s %d %c %d \n", stu[i] -> name, stu[i] -> age, stu[i] -> sex, stu[i] -> id);
scanf("%s", stu[i] -> name);
getchar();
scanf("%d", &stu[i] -> age);
getchar();
scanf("%c", &stu[i] -> sex);
getchar();
scanf("%d", &stu[i] -> id);
break;
}
if((i + 1) == person)
{
printf("没有这号人!\n");
}
}
printf("已成功修改! \n");
}
void delete(struct student *stu[])
{
char str[20] = {0};
int i, j;
printf("请输入你想干掉的人:\n");
scanf("%s", str);
for(i = 0; i < person; i++)
{
if ( strcmp(str, stu[i] -> name) == 0)
{
break;
}
free(stu[i]);
}
for(j = i; j < person - 1;j++)
{
stu[j] = stu[j + 1];
}
person--;
printf("已成功击毙!\n");
}