{
private:
string name
double score
static double TotalScore
static double AverageScore
static int num //计算平均分需要
public:
Score(string n="", double s=0):name(n), score(s){}
void Set(string n, double s){name=nscore=s} //定义数组对象时,需要对每个对象重新赋值
static void Output(){cout <<"TotalScore = " <<TotalScore <<", AverageScore = " <<AverageScore <<endl}
friend void ComputeTotalScore(const Score&a)
}
double Score::TotalScore = 0
double Score::AverageScore = 0
int Score::num = 0
void ComputeTotalScore(const Score&a)
{
Score::TotalScore += a.score
Score::num++
Score::AverageScore = Score::TotalScore/Score::num
}
void main()
{
Score arr[10]
string name = ""
double score = 0
for(int i=0i<10i++)
{
cout <<"Input " <<i+1 <<" Student Name and Score: "
cin >>name >>score
arr[i].Set(name, score)
ComputeTotalScore(arr[i])
}
arr[9].Output()
}
//按你要求来的 不过只输入了3组数,你可以自己改#includestdio.h
#includestring.h
#define max 100
typedef struct
{
char name[max]// 姓名
int num//学号
int pro//程序设计成绩
int maths//数学成绩
}student
void input(student s[],int n)//输入以及求总分
{
int sum=0
for(int i=0ini++)
{
printf("输入第%d个学生的姓名:\n",i+1)gets(s[i].name)
printf("输入第%d个学生的学号:\n",i+1)scanf("%d",s[i].num)
printf("输入第%d个学生的英语成绩:\n",i+1)scanf("%d",s[i].eng)
printf("输入第%d个学生的程序设计成绩:\n",i+1)scanf("%d",s[i].pro)
printf("输入第%d个学生的数学成绩:\n",i+1)scanf("%d",s[i].maths)
sum=s[i].eng+s[i].pro+s[i].maths
printf("总分为:%d\n",sum)
fflush(stdin)
}
}
void sumscore(student s[],int n)//单科成绩总和
{
int sum_eng=0,sum_pro=0,sum_maths=0
for(int i=0ini++)
{
sum_eng+=s[i].eng
sum_pro+=s[i].pro
sum_maths+=s[i].maths
}
printf("英语总成绩为:%d\n",sum_eng)
printf("程序设计总成绩为:%d\n",sum_pro)
printf("数学总成绩为:%d\n",sum_maths)
}
void searchst(student s[],int n)//查找学生
{
char e[max]int number
printf("输入要查询的姓名:\n")gets(e)
printf("输入要查询的学号:\n")scanf("%d",number)
for(int i=0ini++)
if(!strcmp(s[i].name,e)number==s[i].num)
{
printf("英语成绩:%d\n程序设计成绩:%d\n数学成绩:%d\n",s[i].eng,s[i].pro,s[i].maths)
printf("总成绩为:%d\n",s[i].eng+s[i].pro+s[i].maths)
return
}
printf("查无此人!")
}
void main()
{
student s[3]int n=3//这里只用了3个测试,30个太夸张了,可以改成30组数....
input(s,n)
printf("\n")
sumscore(s,n)
printf("\n")
searchst(s,n)
printf("\n")
}
参考资料:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)