现有有N个学生的数据记录,每个记录包括学号、姓名、三科成绩。

/*现有有N个学生的数据记录,每个记录包括学号、姓名、三科成绩。
编写一个函数input,用来输入一个学生的数据记录。
编写一个函数print,打印一个学生的数据记录。
在主函数调用这两个函数,读取N条记录输入,再按要求输出。 N<100
输入
学生数量N占一行 每个学生的 学号、姓名、三科成绩 占一行,空格分开。
输出
每个学生的学号、姓名、三科成绩占一行, 逗号分开。
样例输入
2
a100 zhblue 70 80 90
b200 newsclan 90 85 75
样例输出
a100,zhblue,70,80,90
b200,newsclan,90,85,75
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
struct student
{
char id[20]; /* student ID */
char name[20];
float score1;
float score2;
float score3;
struct student *next;
}; //分号莫忘.
//用 结构体类型的 指针变量 作为参数要比直接用结构体作为参数来的高效
void read_in_link( struct student * link,int n)
{//链表//contemporary(当代.)
struct student *p; /* p->next = &link[i + 1] */
int i = 0;
for (p = link; p < link + n; )
{
scanf("%s %s %f %f %f", p->id, p->name, &p->score1, &p->score2, &p->score3);
i == n - 1 ? (p->next = NULL) : (p->next = &link[i + 1]);
i++;
p++;
}
}
/* 以链表末端的NULL,不需要知道链表的元素个数. */
void print(struct student *p)
{
struct student *i;
for(i = p;i != NULL;i = i->next)
{
printf("%s,%s,%.0f,%.0f,%.0f\n",i->id,i->name,i->score1,i->score2,i->score3);
}
}
//主函数main
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
struct student *link;
/* (一次性,申请内存(这件事还是放在主函数里去做比较好.),并处理失败的情况. */
if ((link = (struct student *)malloc(n * sizeof(struct student))) == NULL)
{
puts("Error");
exit(1);
}
read_in_link(link,n);
print(link);
}
return 0;
}
posted @   xuchaoxin1375  阅读(19)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-03-31 typora markdown 官方快捷键/自定义/标记快捷键(查看与修改)/多种方法将markdown转word(docx)
点击右上角即可分享
微信分享提示