void select_studnet_all(Students std[], int count)
{
int i;
puts(" 学号 姓名 性别 年龄 入团时间 联系电话");
for (i = 0; i < count; i++)
{
printf("%ld %-5s %s %d %6.2f %s\n", std[i].studne_id, std[i].name, std[i].sex, std[i].age, std[i].time, std[i].telephone);
}
puts("\n");
}
说明:显示函数很简单只要用一个循环就可以把所以已经储存得信息显示出来,注意要用count而不是数字,因为我们有添加功能所以数据不只有我们先前存储得,还有后添加得。
int add_studnet_info(Students std[], int count)
{
long studne_id; /* 学号 */
char name[NAME_LEN]; /* 姓名 */
char sex[3]; /* 性别 */
int age; /* 年龄 */
double time; /* 入团时间 */
char telephone[13]; /* 联系电话 */
int index;
printf("请输入学号:");
scanf("%ld", &studne_id);
index = find_Students_id(std, studne_id, count);
printf("请输入姓名:");
scanf("%s", name);
printf("请输入性别:");
scanf("%s", sex);
printf("请输入年龄:");
scanf("%d", &age);
printf("请输入入团时间:");
scanf("%lf", &time);
printf("请输入联系电话:");
scanf("%s", &telephone);
if (index == -1)
{
std[count].studne_id = studne_id;
strcpy(std[count].name, name);
strcpy(std[count].sex, sex);
std[count].age = age;
std[count].time = time;
strcpy(std[count].telephone, telephone);
puts("已成功添加新的团员信息:");
printf("%ld %-5s %s %d %6.2f %s\n", studne_id, name, sex, age, time, telephone);
count++;
}else
{
puts("学号重复添加失败!");
}
return count;
}
说明:添加函数要调用我们得结构体里面得信息然后对新加得数据进行添加这样数据库得数据才是统一的。
int find_Students_id(Students std[], long studne_id, int count)
{
int i;
int index;
for (i = 0; i < count; i++)
{
if (std[i].studne_id == studne_id)
{
index = i;
break;
}
else
{
index = -1;
}
}
return index;
}
说明:这个代码没有实际的用途不是功能代码,他只是为了定义 i 这个变量让我们后面得代码更简洁。
void update_studnet_info(Students std[], int count)
{
long studne_id; /* 学号 */
char name[NAME_LEN]; /* 姓名 */
char sex[3]; /* 性别 */
int age; /* 年龄 */
double time; /* 身高 */
char telephone[50]; /* 联系电话 */
printf("请输入要修改的团员的学号:");
scanf("%ld", &studne_id);
index = find_Students_id(std, studne_id, count);
if (index != -1)
{
puts("请进行修改操作!");
printf("请输入学号:");
scanf("%ld", &studne_id);
printf("请输入姓名:");
scanf("%s", name);
printf("请输入性别:");
scanf("%s", sex);
printf("请输入年龄:");
scanf("%d", &age);
printf("请输入入团时间:");
scanf("%lf", &time);
printf("请输入联系电话:");
scanf("%s", &telephone);
puts("已更新的团员信息:");
printf("%ld %-5s %s %d %6.2f %s\n", studne_id, name, sex, age, time, telephone);
std[index].studne_id = studne_id;
strcpy(std[index].name, name);
strcpy(std[index].sex, sex);
std[index].age = age;
std[index].time = time;
strcpy(std[index].telephone ,telephone);
puts("修改成功!");
}
else
{
puts("没有查找到该团员!");
}
}
说明:更新系统只是为了改变某个团员信息。
int delete_studnet_info(Students std[], int count)
{
long studne_id; /* 学号 */
int i;
printf("请输入要删除的团员的学号:");
scanf("%ld", &studne_id);
for (i = 0; i < count; i++)
{
if (std[i].studne_id == studne_id)
{
while (1)
{
if (i < count)
{
std[i] = std[i + 1];
i++;
}
else
{
break;
}
}
puts("删除成功!");
count--;
break;
}
}
if (count == i)
{
puts("没有查找到该团员!");
}
return count;
}
说明:删除功能和添加功能很类似。