n的值自定义输入
简单练手,成绩那一部分改成数组会更好~ ~
#include<stdio.h>
#include<stdlib.h>
struct node{
char number;
char name;
int grade1;
int grade2;
int grade3;
struct node *next;
};
struct node *first=NULL;
void insert(){
struct node *new_node;
new_node=(struct node *)malloc(sizeof(struct node));
printf("请输入学生信息及成绩(格式:x x x x x):");
scanf("%s %s %d %d %d",&new_node->number,&new_node->name,&new_node->grade1,&new_node->grade2,&new_node->grade3);
new_node->next=first;
first=new_node;
}
void print(int n){
struct node *p;
int max;
int k;
char number;
int grade=0;
int grade1=0;
int grade2=0;
int grade3=0;
int i;
for(p=first,i=1;p!=NULL;p=p->next,i++){
grade=p->grade1 + p->grade2 + p->grade3;
printf("学号:%c 姓名:%c 平均成绩:%d \n",p->number,p->name,(grade/3));
grade1=grade1+p->grade1;
grade2=grade2+p->grade2;
grade3=grade3+p->grade3;
k=grade/3;
if(max<k){
max=k;
number=p->number;
}
}
printf("平均分最高的学生的学号为%c 平均成绩为%d \n",number,max);
printf("课程1的平均成绩:%d \n",(grade1/n));
printf("课程2的平均成绩:%d \n",(grade2/n));
printf("课程3的平均成绩:%d \n",(grade3/n));
}
int main(){
int i,n;
printf("请输入要插入学生的人数");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("请输入学生%d的信息\n",i+1);
insert();
}
print(n);
}
运行结果: