这个版本我将会持续优化题解,并且加入纯C版本(后天发布)
有10个学生,每个学生的数据包含学号、姓名、年龄、四门课成绩、总成绩和平均成绩。
要求在main()函数中输入这10个学生的数据,用自定义函数Total_ave()求出学生的总分和平均分,用sort()函数按总分从高到低重新排列学生信息,用List()函数求出10个学生的平均分并将
高于平均分的学生显示出来。(要求用指针实现)
这题难度也算普及了吧(bushi 仍然很水
首先我们了解一下结构体
struct student
{
int num;//学号
char name;//名字
int year;//年龄
double score1;
double score2;
double score3;
double score4;//四科成绩
double total=0;//总成绩
double avg=0;//平均分
}
struct 结构体名字
{
类型 变量;
};
如果有多组数即可在末尾加上数组
即
struct 结构体名字
{
类型 变量;
}数组;
struct student
{
int num;//学号
char name;//名字
int year;//年龄
double score1;
double score2;
double score3;
double score4;//四科成绩
double total=0;//总成绩
double avg=0;//平均分
}s[10];//s[10]即开了一个包含上面结构体的数组,其数据储存可以类比二维数组
如果要使用指针可以在末尾加入
}*p;
或者是内部有{struct student *p;}
由于分数可能特别多,比如初中有9科,甚至加上体育美术等;
可以在结构体中开数组
struct student
{
double score[1001];//n科成绩
double total=0;//总成绩
double avg=0;//平均分
}s[10];
注意这时的读入就需要再套一层循环对多个科目的分数使用s[i].score[j]进行读入
并且在后面的各个函数中也是如此
使用时为:结构体名字.变量名字
如是数组即可有:数组.变量名字
先给一个无指针版,更好解释一下题解思路
无指针版:
#include <bits/stdc++.h>
using namespace std;
struct stu
{
int num;//学号
char name;//名字
int year;//年龄
double score1;
double score2;
double score3;
double score4;//四科成绩
double total=0;//总成绩
double avg=0;//平均分
}s[10];//10个人,开数组s[10];
void Total_ave(stu s[10])//用于计算总分和平均分
{
for(int i=0;i<10;i++)
{
s[i].total = s[i].score1+s[i].score2+s[i].score3+s[i].score4;//总分
s[i].avg = s[i].total/4;//平均分
}
}
bool cmp(const stu& a,const stu&b)//结构体sort的条件
{
if(a.avg!=b.avg)
{
if(a.avg>b.avg)//比对平均分大小
return true;
else
return false;
}
}
void List(stu s[10])//选出分数大于总平均数的同学
{
double totalavg;
for(int i=0;i<10;i++)
{
totalavg+=s[i].avg;//总平均分和
}
totalavg /= 10;//总平均分
cout << totalavg << endl;
for(int i=0;i<10;i++)
{
if(s[i].avg > totalavg)//如果大于总平均分就输出
{
cout << s[i].num <<' '<< s[i].name << endl ;
}
}
}
int main(int argc, char const *argv[])
{
for(int i=0;i<10;i++)//读入学号,名字,年龄,成绩
{
cin >> s[i].num >> s[i].name >> s[i].year >> s[i].score1 >> s[i].score2 >> s[i].score3 >> s[i].score4;
}
Total_ave(s);
sort(s,s+10,cmp);
List(s);
return 0;
}
其中要注意的是结构体在sort中需要定义一个cmp函数作为排序条件
void cmp(const stu&a,const stu&b)//const常引用,保证引用不被改变
//直接 stu&a也行,但是cmp为条件判断语句,为了防止在函数内因为人为失误改变数组的值,建议使用const保证值不变;
{
//内部写条件判断
//条件1 例:
if (a>b)
{
return true;
else
return false;
}
}
//true即为符合sort快排条件,反之;
即比对结构体下的一个或多个数值
有指针版:
#include <bits/stdc++.h>
using namespace std;
struct stu
{
int num;//学号
char name;//名字
int year;//年龄
double score1;
double score2;
double score3;
double score4;//四科成绩
double total=0;//总成绩
double avg=0;//平均分
struct stu *next;
}s[10],*p;//10个人,开数组s[10];
void Total_ave(stu s[10])//用于计算总分和平均分
{
struct stu *p;
for(p=s;p<s+10;p++)
{
p->total = p->score1+p->score2+p->score3+p->score4;//总分
p->avg = p->total/4;//平均分
}
}
bool cmp(const stu& a,const stu&b)//结构体sort的条件
{
if(a.avg!=b.avg)
{
if(a.avg>b.avg)
return true;
else
return false;
}
}
void List(stu s[10])//选出分数大于总平均数的同学
{ stu *p;
double totalavg;
for(p=s;p<s+10;p++)
{
totalavg+=p->avg;//总平均分和
}
totalavg /= 10;//总平均分
cout << totalavg << endl;
for(p=s;p<s+10;p++)
{
if(p->avg > totalavg)//如果大于总平均分就输出
{
cout << p->num <<' '<< p->name <<' '<< p->year<< "\n";
}
}
}
int main(int argc, char const *argv[])
{ stu *p;
for(p=s;p<s+10;p++)//读入学号,名字,年龄,成绩
{
cin >> p->num >> p->name >> p->year >> p->score1 >> p->score2 >> p->score3 >> p->score4;
}
Total_ave(s);
sort(s,s+10,cmp);
List(s);
return 0;
}
开结构体指针 stu *p(结构体名字+ *指针变量)
用p->指向结构体内的变量;
p->a;
(*P).a;
//都可以表示指针指向结构体元素
纯C版本发布,链表看情况,用不太到(bushi
#include <stdio.h>
struct stu
{
int num;//学号
char name;//名字
int year;//年龄
double score1;
double score2;
double score3;
double score4;//四科成绩
double total;//总成绩
double avg;//平均分
struct stu *next;
}s[10],*p;//10个人,开数组s[10];
void Total_ave(struct stu s[10])//用于计算总分和平均分
{
struct stu *p;
for(p=s;p<s+10;p++)
{
p->total = p->score1+p->score2+p->score3+p->score4;//总分
p->avg = p->total/4;//平均分
}
}
void Sort(struct stu s[10])
{ int i,j,n1=0;
double v=0;
char zm;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(s[i].avg>s[j].avg)
{
v=s[i].avg;
s[i].avg=s[j].avg;
s[j].avg=v;
n1=s[i].num;
s[i].num=s[j].num;
s[j].num=n1;
n1=s[i].year;
s[i].year=s[j].year;
s[j].year=n1;
zm=s[i].name;
s[i].name=s[j].name;
s[j].name=zm;
}
}
}
}
void List(struct stu s[10])//选出分数大于总平均数的同学
{ struct stu *p;
double totalavg;
for(p=s;p<s+10;p++)
{
totalavg+=p->avg;//总平均分和
}
totalavg /= 10;//总平均分
printf("%lf\n", totalavg);
struct stu;
for(p=s;p<s+10;p++)
{
if(p->avg > totalavg)//如果大于总平均分就输出
{
printf("%s\n",&p->name);
}
}
/* int im;
for(im=0;im<10;im++)
{
if(s[im].avg > totalavg)
{
printf("%d% s %d\n",&s[im].num,&s[im].name,&s[im].year);
}
}*/
}
int main()
{
struct stu *p;
int j,k;
for(j=0;j<10;j++)
{
s[j].total= 0;
s[j].avg=0;
}
for(k=0;k<10;k++)//读入学号,名字,年龄,成绩
{
scanf("%d %s %d %lf %lf %lf %lf",&s[k].num,&s[k].name,&s[k].year,&s[k].score1,&s[k].score2,&s[k].score3,&s[k].score4);
}
Total_ave(s);
Sort(s);
List(s);
return 0;
}
其实自己也有点小问题,就是调用指针输出后必须得加&
printf("%s\n",&p->name); 不加就卡住,感慨C语言确实更难学OMO