#define NUM 4
#define SUB_NUM 5
/*定义全局变量结构体*/
static struct student
{ char name[20];
char stu_id[5]; /* format 000 */
float subject_point[SUB_NUM]; /* five subject point */
}stu[NUM] ={{"lili","001",34,18,73,12,10},
{"tata","002",24,36,72,72,90},
{"haha","003",90,90,90,90,90},
{"qiqi","004",65,56,87,52,90}};
#include
#include
void main()
{
float Average_sub(int );
struct student *find(char *);
int Unpass(void);
float Average_per(char *);
// struct student *s; /*stu结构体指针*/
// int i ;
int course_code;
float average;
char student_id[10];
float average_person;
// printf("input the student infomation\n");
// s = stu;
/**************************************************************************
for(i = 0;i < NUM; i++){
scanf("%d%s",s->name,s->stu_id);
for(j = 0;j < SUB_NUM; j++) scanf("%f",&s->subject_point[j]);
s++;
}
**************************************************************************/
printf("\n");
printf("Calculated the average performance of a class\n");
printf("Please,input the course code(eg: 1,2,3,4,5)\n");
scanf("%d",&course_code);
average = Average_sub(course_code);
printf("\n");
printf("the course %d performance is%f\n",course_code,average);
printf("\n");
printf("to find the student failed to pass two or more courses\n");
Unpass();
printf("\n");
printf("Calculate the average performance of a person\n");
printf("Please,input the student id(format 000)\n");
scanf("%s",student_id);
printf("\n");
average_person = Average_per(student_id);
printf("the student %s's average is %f\n",student_id,average_person);
getchar();
}
/***************************************************************************
*function name:求某一门课的平均成绩
*input argument:课程代码(1,2,3,4,5)
*output argument: average grade
****************************************************************************/
float Average_sub(int sub_number)
{
int i;
float sum = 0;
int sub_num;
struct student *p;
p = stu;
sub_num = sub_number;
if(sub_num<1 || sub_num>5){
printf("input is error");
return 0;
}
sub_num--; /* 课程代码减一(从零开始)*/
for(i=0;i < NUM; i++)
{
sum = p->subject_point[sub_num] + sum;
p++;
} /*sum*/
sum = sum / NUM; /*ave*/
return sum;
}
/*****************************************************
*function:查找匹配
*note:简单查询
*intput:student ID
*output:所在数组的位置(结构体指针)
****************************************************/
struct student *find(char *id )
{
// int i = 0;
struct student *p;
p = stu ; /*数组入口赋值给p*/
while (strcmp(id,p->stu_id)) /*strcmp比较两个字符串相等,相等返回0*/
{
if(p < stu + NUM)
p++;
else
{
printf("the input is error\n"); /*查找完毕未发现匹配项*/
break;
}
}
return p; /*返回指针p*/
}
/*问题:id只是字符串的首地址*/
/***********************************************
*function:求个人平均成绩
*input argument:student ID?
*output argumnet:个人平均成绩
***********************************************/
float Average_per(char *st_id)
{
int i;
struct student *p;
float av = 0;
char *ch;
ch = st_id;
p = find(ch);
if(p >= (stu + NUM)) return 0;
for(i = 0; i < SUB_NUM; i++)
{
av = av + p->subject_point[i];
} /*sum*/
av = av/SUB_NUM; /*averaging*/
return av ;
}
/***********************************************
*function name:找出两门及以上不及格的学生
*input argumnet: void
*output argument: student number
*notice:输出学生学号
************************************************/
int Unpass(void)
{
int count;
int i,j;
struct student *p;
p = stu;
for (i = 0; i < NUM; i++)
{
count = 0;
for (j = 0; j < SUB_NUM; j++)
{
if (count >= 2){
printf("the student NO.%s\n",p->stu_id);
break; //充分体现了for和continue的区别(continue 跳出if循环,break跳出最近的第一个for循环)
}
if (p->subject_point[j] < 60) count++;
}
p++;
}
return 0;
}
/************************************************************
//经验:
//字符串,最好用数组表式(char st[10]),不建议使用指针char *st。
//scanf("%s",student_id);
//printf("\n");
//scanf("%s\n",student_id) 此句和上面两句比较在调试上出问题。
*************************************************************/
阅读(608) | 评论(0) | 转发(0) |