前言
在学习结构化数据中,要实现一个小型的人员信息管理系统,可以添加,删除,查看人员信息。每一个人员信息由姓名、性别、身高、体重这4个部分组成。
1. 初学者可能会用下面这种方式,灵活性差。
下面的代码为每个人都声明了4个变量来存储个人信息。如果需要添加更多的人员,则需要在代码里添加更多的变量。如果要输入N个人的信息,代码会非常冗余。
#include <stdio.h>
int main()
{
//第一个人
char name1[20];
int gender1;
double height1;
double weight1;
//第二个人
char name2[20];
int gender2;
double height2;
double weight2;
//第三个人
char name3[20];
int gender3;
double height3;
double weight3;
//输入人员信息
printf("input name gender height weight:\n");
scanf("%s %d %lf %lf", name1, &gender1, &height1, &weight1);
scanf("%s %d %lf %lf", name2, &gender2, &height2, &weight2);
scanf("%s %d %lf %lf", name3, &gender3, &height3, &weight3);
//显示人员信息
printf("display personal information\n");
printf("name gender height weight:\n");
printf("%s %d %.2f %.2f\n", name1, gender1, height1, weight1);
printf("%s %d %.2f %.2f\n", name2, gender2, height2, weight2);
printf("%s %d %.2f %.2f\n", name3, gender3, height3, weight3);
return 0;
}
2. 使用数组存储数据
局限性:只能一次性输入所有人员信息,无法继续追加人员信息
#include <stdio.h>
int main()
{
//使用数组存储人员信息
//姓名数组,每个名字最多20个字节,最多10个名字
char name[10][20];
int gender[10];
double height[10];
double weight[10];
//人员数量
int numOfPerson = 0;
printf("How many people?\n");
scanf("%d",&numOfPerson);
//输入人员信息
printf("input name gender height weight:\n");
for(int i=0;i<numOfPerson;i++)
{
scanf("%s %d %lf %lf", name[i], &gender[i], &height[i], &weight[i]);
}
//显示人员信息
printf("display personal information\n");
printf("name gender height weight:\n");
for(int i=0;i<numOfPerson;i++)
{
printf("%s %d %.2f %.2f\n", name[i], gender[i], height[i], weight[i]);
}
return 0;
}
3. 交互式界面
启动后显示主界面,有3个选项:
1.input person information
2.display person information
3.exit
输入1,可以输入人员信息,输入完成后,返回主界面
输入2,可以显示所有成员信息,按任意键返回主界面
输入3,就退出程序
不仅可以灵活处理和查看人员信息,而且代码精简不冗余,可以按照自己的需求更改数组。
#include <stdio.h>
#include <stdlib.h>
int main()
{
//使用数组存储人员信息
//姓名数组,每个名字最多20个字节,最多10个名字
char name[10][20];
int gender[10];
double height[10];
double weight[10];
//人员数量
int numOfPerson = 0;
while (1)
{
system("cls");//system是头文件stdlib.h中的函数,它的参数是一串字符串
//录入信息
printf("1.input person information\n");
//查看信息
printf("2.display person information\n");
//退出
printf("3.exit\n");
int input;
scanf("%d", &input);
//cls命令作用是清除控制台中之前显示过的字符
system("cls");
if(input == 1)//执行录入信息操作
{
//录入信息
printf("input name gender height weight:\n");
scanf("%s %d %lf %lf", name[numOfPerson], &gender[numOfPerson], &height[numOfPerson], &weight[numOfPerson]);
numOfPerson++;
}
else if(input == 2)//执行查看信息操作
{
//查看信息
printf("display personal information\n");
printf("name gender height weight:\n");
for(int i=0;i<numOfPerson;i++)
{
printf("%s %d %.2f %.2f\n", name[i], gender[i], height[i], weight[i]);
}
//pause命令暂停程序执行,按任意键后继续执行
system("pause");
}
else
{
break;
}
}
return 0;
}
4. 聚合数据
引入一个关键词struct,将每个人的信息聚合在一个实体中。
//聚合数据struct
#include <stdio.h>
#include <stdlib.h>
int main()
{
//1.数组定义
struct
{
char name[20];
int gender;
double height;
double weight;
}arr[10];
//人员数量
int numOfPerson = 0;
while (1)
{
system("cls");//system是头文件stdlib.h中的函数,它的参数是一串字符串
//录入信息
printf("1.input person information\n");
//查看信息
printf("2.display person information\n");
//退出
printf("3.exit\n");
int input;
scanf("%d", &input);
//cls命令作用是清除控制台中之前显示过的字符
system("cls");
if(input == 1)//执行录入信息操作
{
//录入信息
printf("input name gender height weight:\n");
//2.输入数据
scanf("%s %d %lf %lf", arr[numOfPerson].name, &arr[numOfPerson].gender, &arr[numOfPerson].height, &arr[numOfPerson].weight);
numOfPerson++;
}
else if(input == 2)//执行查看信息操作
{
//查看信息
printf("display personal information\n");
printf("name gender height weight:\n");
for(int i=0;i<numOfPerson;i++)
{
//3.显示数据
printf("%s %d %.2f %.2f\n", arr[i].name, arr[i].gender, arr[i].height, arr[i].weight);
}
//pause命令暂停程序执行,按任意键后继续执行
system("pause");
}
else
{
break;
}
}
return 0;
}