
求c语言编程:结构体
定义一个结构体数组,编写程序实现以下功能:1.从键盘输入表4-1中数据给数组。表4-1用以创建结构体数组的原始数据学号姓名性别年龄高数计算机物理英语总分平均分1001Ja...
定义一个结构体数组,编写程序实现以下功能:
1.从键盘输入表4-1中数据给数组。
表4-1 用以创建结构体数组的原始数据
学号 姓名 性别 年龄 高数 计算机 物理 英语 总分 平均分
1001 Jack m 23 34 56 78 12
1002 Sam f 56 67 65 87 54
1003 Marry f 45 54 68 67 56
1004 David m 78 34 56 98 44
2.计算每个同学的总分和平均分,并在对应的结构体中填上数据。
3.根据每位学生的总分降序排列。
4.输出数组中的数据,每位同学一行。
希望帮帮忙,还会追加悬赏的! 展开
1.从键盘输入表4-1中数据给数组。
表4-1 用以创建结构体数组的原始数据
学号 姓名 性别 年龄 高数 计算机 物理 英语 总分 平均分
1001 Jack m 23 34 56 78 12
1002 Sam f 56 67 65 87 54
1003 Marry f 45 54 68 67 56
1004 David m 78 34 56 98 44
2.计算每个同学的总分和平均分,并在对应的结构体中填上数据。
3.根据每位学生的总分降序排列。
4.输出数组中的数据,每位同学一行。
希望帮帮忙,还会追加悬赏的! 展开
1个回答
展开全部
#include <iostream>
using namespace std;
#define MAX 4 //////如果学过链表就可以不用第一结构体数组
typedef struct stude{
int no;
char name[20];
char sex[3];
int age;
float math;
float computer;
float physic;
float english;
float sum;
float average;
} stu;
void main()
{
stu student[MAX];
///////// 输入成绩
for(int i =0; i <MAX; i++)
{
cout<<"请输入第"<<i+1<<"个同学的:"<<endl;
cout<<"学号:";
cin>>student[i].no;
cout<<endl<<"姓名:";
cin>>student[i].name;
cout<<endl <<"性别";
cin>>student[i].sex;
cout<<endl<<"年龄:";
cin>>student[i].age;
cout<<endl<<"高数成绩:";
cin>>student[i].math;
cout<<endl<<"计算机成绩:";
cin>>student[i].computer;
cout<<endl<<“物理成绩:";
cin>>student[i].physic;
cout<<endl<<"英语成绩:";
cin>>student[i].english;
cout<<endl;
//////求总分
student[i].sum = student[i].math + student[i].computer + student[i].physic +
student[i].english;
///////求平均分
student[i].average = student[i].sum / 4;
}
///////按总分排序 ,用冒泡法
for(int j = 0 ; j < MAX - 1; j++)
{
for(int k =0; k < MAX - 1 - j; k++)
{
if(student[k].sum < student[k+1].sum)
{
float tem;
tem = student[k].sum;
student[k].sum = student[k+1].sum;
student[k+1].sum = tem;
}
}
}
cout<<"学号 "<<"姓名 "<<"性别 "<<"年龄 "<<"高数 "<<"计算机 "<<"物理 "<<"英语 "<<"总分 "<<"平均分"<<endl;
for(int col = 0; col < MAX; col++)
{
cout<<student[col].no<<" "<<stuent[col].name<<" "<<student[col].sex<<" "<<student[col].age<< <<studetn[col].math<<" "<<student[col].computer<<" "<<student[col].physic<<" "<<student[col].english<<" "<<student[col].sum<<" "<<student[col].average<<endl;
}
}
using namespace std;
#define MAX 4 //////如果学过链表就可以不用第一结构体数组
typedef struct stude{
int no;
char name[20];
char sex[3];
int age;
float math;
float computer;
float physic;
float english;
float sum;
float average;
} stu;
void main()
{
stu student[MAX];
///////// 输入成绩
for(int i =0; i <MAX; i++)
{
cout<<"请输入第"<<i+1<<"个同学的:"<<endl;
cout<<"学号:";
cin>>student[i].no;
cout<<endl<<"姓名:";
cin>>student[i].name;
cout<<endl <<"性别";
cin>>student[i].sex;
cout<<endl<<"年龄:";
cin>>student[i].age;
cout<<endl<<"高数成绩:";
cin>>student[i].math;
cout<<endl<<"计算机成绩:";
cin>>student[i].computer;
cout<<endl<<“物理成绩:";
cin>>student[i].physic;
cout<<endl<<"英语成绩:";
cin>>student[i].english;
cout<<endl;
//////求总分
student[i].sum = student[i].math + student[i].computer + student[i].physic +
student[i].english;
///////求平均分
student[i].average = student[i].sum / 4;
}
///////按总分排序 ,用冒泡法
for(int j = 0 ; j < MAX - 1; j++)
{
for(int k =0; k < MAX - 1 - j; k++)
{
if(student[k].sum < student[k+1].sum)
{
float tem;
tem = student[k].sum;
student[k].sum = student[k+1].sum;
student[k+1].sum = tem;
}
}
}
cout<<"学号 "<<"姓名 "<<"性别 "<<"年龄 "<<"高数 "<<"计算机 "<<"物理 "<<"英语 "<<"总分 "<<"平均分"<<endl;
for(int col = 0; col < MAX; col++)
{
cout<<student[col].no<<" "<<stuent[col].name<<" "<<student[col].sex<<" "<<student[col].age<< <<studetn[col].math<<" "<<student[col].computer<<" "<<student[col].physic<<" "<<student[col].english<<" "<<student[col].sum<<" "<<student[col].average<<endl;
}
}
追问
我没有学过链表。可不可以只用结构体来编译呢?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询