完成对一个顺序表的建立,表中的每个元素是同学们的学号、姓名和三门课程的成绩,输入5个同学的信息,然后显示在屏幕上。(要求利用顺序表的基本操作)
输入格式:
首先收入学生人数5,然后依次输入5个学生的学号、姓名和三门课的成绩
输出格式:
输入5个学生的学号、姓名和三门课的成绩
输入样例:
5
01 张三 89 89 89
02 李四 90 90 90
03 王五 89 89 89
04 钱六 97 97 97
05 赵倩 90 90 90
输出样例:
1 张三 89.0 89.0 89.0
2 李四 90.0 90.0 90.0
3 王五 89.0 89.0 89.0
4 钱六 97.0 97.0 97.0
5 赵倩 90.0 90.0 90.0
代码:
#include<bits/stdc++.h>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int ELemType;
typedef int Status;
typedef struct
{
char name[100];
char number[100];
double m,n,k;
}Student,*Sqlist;//单个元素和指针
Status Init_Student(Student &S)
{
cin>>S.number>>S.name>>S.m>>S.n>>S.k;
return OK;
}
int main()
{
Sqlist L=NULL;
int num;
cin>>num;
L=(Sqlist)malloc(sizeof(Student)*num);//一定要给数组开辟内存
if(!L)exit(OVERFLOW);
for(int i=0;i<num;i++)
{
if(Init_Student(L[i]))
printf("%c %s %.1lf %.1lf %.1lf\n",L[i].number[1],L[i].name,L[i].m,L[i].n,L[i].k);//保留一位小数的做法
}
}