作业中参数传递与结构体的问题,不懂,求解
程序代码:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STUDENT_COUNT 2//为方便测试写了两个人
#define NAME_LENGTH 10
typedef struct Student
{
char name[NAME_LENGTH];
int studentId;
double mathScore;
double computerScore;
double sumScorce;
}Student;
void show(Student (*students)[STUDENT_COUNT])//Student (*students)与(Student *)students的区别是什么?
{
int i;
Student *p = (Student *)students;//为什么强制转换?是为了方便控制操作数吗?
printf(" 姓名:\t学号:\t数学成绩:\t计算机成绩:\t\n");
for (i = 0;i<STUDENT_COUNT;i++)
{
printf("%10s",(p+i)->name);//这里为什么是(p+i)->name儿不是students[i]->?
printf("\t%d",(p+i)->studentId);
printf(" \t%g",(p+i)->mathScore);
printf(" \t\t%g\n",(p+i)->computerScore);
}
}
void seek(Student (*students)[STUDENT_COUNT],char name[NAME_LENGTH])
{
int i;
Student *p = (Student *)students;
for (i = 0;i<STUDENT_COUNT;i++)
{
if (0==strcmp(name,(p+i)->name))
{
printf(" 姓名:\t学号:\t数学成绩:\t计算机成绩:\t\n");
printf("%10s",(p+i)->name);
printf("\t%d",(p+i)->studentId);
printf(" \t%g",(p+i)->mathScore);
printf(" \t\t%g\n",(p+i)->computerScore);
return 0;
}
}
printf("查无此人!\n");
}
void sort(Student (*students)[STUDENT_COUNT])
{
int i,j;
Student tmpStu;
Student*p = (Student * )students;
for (i = 0;i<STUDENT_COUNT;i++)
{
(p+i)->sumScorce = (p+i)->mathScore + (p+i)->computerScore;
}
for (i = 0;i<STUDENT_COUNT-1;i++)
{
for (j = 0;j<STUDENT_COUNT-1-i;j++)
{
if ((p+j)->sumScorce<(p+j+1)->sumScorce)
{
strcpy(tmpStu.name,(p+j)->name);
tmpStu.studentId = (p+j)->studentId;
tmpStu.mathScore = (p+j)->mathScore;
= (p+j)->computerScore;
tmpStu.sumScorce = (p+j)->sumScorce;
strcpy((p+j)->name,(p+j+1)->name);
(p+j)->studentId = (p+j+1)->studentId;
(p+j)->mathScore = (p+j+1)->mathScore;
(p+j)->computerScore = (p+j+1)->computerScore;
(p+j)->sumScorce = (p+j+1)->sumScorce;
strcpy((p+j+1)->name,tmpStu.name);
(p+j+1)->studentId = tmpStu.studentId;
(p+j+1)->mathScore = tmpStu.mathScore;
(p+j+1)->computerScore = (p+j+1)->sumScorce = tmpStu.sumScorce;
}
}
}
}
int main(void)
{
int choice;
int i;
char name[NAME_LENGTH]="";
Student students[STUDENT_COUNT];
for (i = 0;i<STUDENT_COUNT;i++)
{
printf("请输入第%d个学生的姓名:",i+1);
scanf("%s",&students[i].name);
printf("请输入第%d个学生的学号:",i+1);
scanf("%d",&students[i].studentId);
printf("请输入第%d个学生的数学成绩:",i+1);
scanf("%lf",&students[i].mathScore);
printf("请输入第%d个学生的计算机成绩:",i+1);
scanf("%lf",&students[i].computerScore);
}
printf("\n");
printf("----------------------------------\n");
printf("* 1.输出学生信息 *\n");
printf("* 2.查找学生信息(按姓名查找) *\n");
printf("* 3.对学生按总成绩排序输出 *\n");
printf("---------------------------------\n");
printf("\n");
printf("请输入你的选择(1-3):");
scanf("%d",&choice);
switch (choice)
{
case 1:
show(&students);
break;
case 2:
printf("请输入要查询人的姓名:");
scanf("%s",name);
seek(&students,name);
break;
case 3:
sort(&students);
printf(" 姓名:\t学号:\t数学成绩:\t计算机成绩:\t总成绩:\n");
for (i = 0;i<STUDENT_COUNT;i++)
{
printf("%10s",students[i].name);
printf("\t%d",students[i].studentId);
printf(" \t%g",students[i].mathScore);
printf(" \t\t%g",students[i].computerScore);
printf("\t\t%g\n",students[i].sumScorce);
}
break;
default:
printf("你选错了=_=!\n");
}
system("pause");
return 0;
}
//虽然程序是我写的,是看书上临摹的,很多不懂,写的也不好,见谅










