# Structure design (calculation and search)

CSDN Q & A 2022-06-24 10:06:16 阅读数:526

structuredesigncalculationsearch

requirement ：
Enter the student's grades in each subject , Design a function to calculate the average score of each subject 、 The average score of all students in each subject 、 Find student grades , Then output the results .
I hope some friends can help me check if there are any errors in the code , And asking questions “ The average score of all students in each subject ” How to write the function of this block ？
#include<stdio.h>
#include<string.h>
struct students_list{
char name;
int Math;
Int English;
Int Science;
};
void student_average(struct students_list students[ ],intneme);
void subject_average(struct students_list students[ ],int
subject);
void search_student(struct students_list students[ ],char*name);
int Count=0;

int main(void)
{
int choice,i;
char name;
char subject;
struct students_list students;
for(i=1;;i++){
printf(" Please enter the students' names in turn 、 Math scores 、 English scores 、 Scientific achievements :");
scanf("%s %d3（ I don't know why I can't input three in a row d）",&students[i].name,&students[i].Math, &students[i].English, &students[i].Science);
Count++;
}
do{
printf(" Student achievement function options ：1: Calculate the student average 2: Calculate the course average 3: Check student grades 0: Exit and output results \n");
printf(" Please select the function ："); scanf("%d", &choice);
switch(choice){
case 1:
printf(" Please enter the name of the student you want to find :");
scanf("%s", name);
student_average(students,name);
break;
case 2:
printf(" Please enter the course you want to find :");
scanf("%s", subject);
subject_average(students,subject);
break;
case 3:
printf(" Please enter the name of the student you want to find :");
scanf("%s", name);
search_student(students, name);
break;
case 0: break;
}
}while(choice != 0);
for(i=1;i<Count;i++){
printf("%s: %d
3（ I don't know why I can't input three in a row d）",students[i].name,students[i].Math, students[i].English, students[i].Science);
}
return 0;
}
/ Student average /
void student_average(struct students_list students[ ],intneme)
{
int i,flag = 0;
double average;
if(Count == 0){
printf(" The report card is empty !\n");
return;
}
for(i = 0; i <=Count; i++)
if(strcmp(name, friends[i].name) == 0){ /
Find the students */
flag=1; break;
}
if(flag){
average=(students[i].Math+students[i].English+students[i].Science)/3.0
printf(“student_average:%lf\n”,average);
}
else
printf(" No such student !");
}
void search_student(struct students_list students[ ],char*name)
{ int i, flag = 0;
if(Count == 0){
printf(" The report card is empty !\n");
return;
}
for(i = 0; i < =Count; i++)
if(strcmp(name, friends[i].name) == 0){
flag=1; break;
}
if(flag){
printf("Math: %d\n", students[i].Math);
printf("English: %d\n", students[i].English);
printf("Science: %d\n", students[i].Science);
}
else
printf(" No such student !");
}