1.程序分析:
本程序旨在利用结构体和联合建立学生信息。
2.源代码:
/*
用结构嵌套联合体的方法建立高中学生成绩档案 。
档案信息包括姓名,性别,数学成绩,英语成绩,语文成绩。
另外,文科生有政治成绩,历史成绩,地理成绩。
理科生有物理成绩,化学成绩,生物成绩
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
struct person{
char name[11];//姓名
bool sex;//性别
bool type;//文理科类别
unsigned math;//数学成绩
unsigned english;//英语成绩
unsigned chinese;//语文成绩
union
{
struct{
unsigned physics;//物理成绩
unsigned chemistry;//化学成绩
unsigned biology;//生物成绩
}science;
struct{
unsigned polity;//政治成绩
unsigned history;//历史成绩
unsigned geography;//地理成绩
}arts;
}other;
};
//输入数据
void input(person *p,int &num)
{
char s[101];
int x;
int n;
cout<<"输入n组数据"<<endl;
cin>>n;
while(num<n)
{
system("CLS");
system("color 0a");
cout<<"输入姓名:"<<endl;
cin>>s;
num++;
strcpy(p[num].name,s);//拷贝姓名
cout<<"(1 男神,0 女神)"<<endl;
cin>>x;
if(x)
p[num].sex=true;
else
p[num].sex=false;
cout<<" 1 理科,0 文科"<<endl;
cin>>x;
if(x)
p[num].type=true;
else
p[num].type=false;
cout<<"数学成绩"<<endl;
cin>>p[num].math;
cout<<"语文成绩"<<endl;
cin>>p[num].chinese;
cout<<"英语成绩"<<endl;
cin>>p[num].english;
if(p[num].type)
{
cout<<"物理成绩"<<endl;
cin>>p[num].other.science.physics;
cout<<"化学成绩"<<endl;
cin>>p[num].other.science.chemistry;
cout<<"生物成绩"<<endl;
cin>>p[num].other.science.biology;
}
else
{
cout<<"政治成绩"<<endl;
cin>>p[num].other.arts.polity;
cout<<"历史成绩"<<endl;
cin>>p[num].other.arts.history;
cout<<"地理成绩"<<endl;
cin>>p[num].other.arts.geography;
}
}
}
//输出统计结果
void output(person *p)
{
system("CLS");
char sex[][5]={"女神","男神"};
char type[][5]={"文科","理科"};
cout<<"姓名:"<<p->name<<endl;
cout<<"性别:"<<sex[int(p->sex)]<<endl;
cout<<"类别:"<<type[int(p->type)]<<endl;
cout<<"数学成绩:"<<p->math<<endl;
cout<<"语文成绩:"<<p->chinese<<endl;
cout<<"英语成绩:"<<p->english<<endl;
if(p->type)
{
cout<<"物理成绩"<<p->other.science.physics<<endl;
cout<<"化学成绩:"<<p->other.science.chemistry<<endl;
cout<<"生物成绩:"<<p->other.science.biology<<endl;
}
else
{
cout<<"政治成绩:"<<p->other.arts.polity<<endl;
cout<<"历史成绩:"<<p->other.arts.history<<endl;
cout<<"地理成绩:"<<p->other.arts.geography<<endl;
}
cout<<endl;
}
//按姓名查找
void search_name(person *p,int num)
{
char name[11];
cout<<"请输入姓名:"<<endl;
cin>>name;
for(int i=0;i<=num;i++)
{
if(strcmp(p[i].name,name)==0)
break;
if(i>num)
cout<<"查无此人"<<endl;
else
cout<<p[i].name<<p[i].sex<<p[i].type<<endl;
}
}
//统计男神和女神的人数
void sex_statistics(person *p,int num)
{
int man,women,i;
man=0;
women=0;
for(i=0;i<=num;i++)
{
if(p[i].sex)
man++;
else
women++;
}
cout<<"男神:"<<man<<endl;
cout<<"女神:"<<women<<endl;
cout<<endl;
}
//按总分统计
void sum_statistics(person *p,int num)
{
unsigned score,sum;
int i;
cout<<"输入要查询的分数线:"<<endl;
cin>>score;
for(i=0;i<=num;i++)
{
sum=p[i].math+p[i].chinese+p[i].english;
if(p[i].type)
sum=sum+p[i].other.science.physics+p[i].other.science.chemistry+p[i].other.science.biology;
else
sum=sum+p[i].other.arts.polity+p[i].other.arts.history+p[i].other.arts.geography;
if(sum>score)
cout<<p[i].name<<" "<<sum<<endl;
}
}
int main()
{
person p[101];
int code,num = 0;
while(true)
{
cout<<"-------0.退出程序----------"<<endl;
cout<<"-------1.输入数据----------"<<endl;
cout<<"-------2.按姓名查询----------"<<endl;
cout<<"-------3.按性别统计人数----------"<<endl;
cout<<"-------4.按总分统计人数----------"<<endl;
cin>>code;
switch(code)
{
case 0:
exit(0);
case 1:
input(p,num);
break;
case 2:
search_name(p,num);
break;
case 3:
sex_statistics(p,num);
break;
case 4:
sum_statistics(p,num);
default :
break;
}
}
return 0;
}
3.测试结果:
1号操作
2号操作
以下测试不再赘述......