作者: 杨
版权声明:
本文为博主原创文章,转载请附上源文链接!
本文链接:https://my.oschina.net/yangmufa/blog/3096426
首先创建一个线性表用来存储学生成绩,每个学生的成绩作为一个数据元素对应标注的一行。
定义线性表的结构体类型,成绩表的数据项包括 学号、姓名、数学、英语、政治、专业课1、专业课2、总成绩。
手机终端 显示不全 可在代码区 左右 滑动屏幕
#include <stdio.h>
#include <stdlib.h>
#define MaxLength 255
//数据元素
typedef struct {
int id;//学号
char name[20];//姓名
int score[5];//英语、政治、专业课1、专业课2的成绩
int total;//总成绩
}studentElement;
//顺序表结构
typedef struct {
studentElement studentArray[MaxLength];
int length;
}seqList;
然后在operation.h定义并实现函数用于满足各种操作(
int InitList(seqList*seqList); 初始化(输入学生人数建立线性表长度,录入每个学生的各科成绩信息,计算出每个人总成绩)。
int Sort(seqList *seqList); 排序(首先输入要对所有人成绩排序的科目后按照升序排列,最后打印显示排序后的所有人成绩表。)
int Puery(seqList *seqList); 查询(输入学号,将线性表中的每个学生的学号与给定的字符串相比较若有匹配则打印显示该学生多所有信息,否则出错误提示。)
int Print(seqList *seqList); (输出所有信息。)
)。
手机终端 显示不全 可在代码区 左右 滑动屏幕
#include <stdio.h>
#include <stdlib.h>
#include "DataElement.h"
//初始化(输入学生人数建立线性表长度,录入每个学生的各科成绩信息,计算出每个人总成绩)。
int InitList(seqList*seqList){
int length=0;
int i=0;
int total=0;
int j=0;
printf("输入学生人数:");
scanf_s("%d",&length);
if(length<0||length>MaxLength){
printf("输入的学生人数不符合吹逻辑!Over!!!");
}else{
seqList->length=length;
}
if(seqList->length<0||seqList->length>MaxLength){
printf("初始化学生人数失败!\n");
return 0;
}else{
for(i=0;i<length;i++){//输入每个学生的信息
seqList->studentArray[i].total=0;//每个学生的确立之前总成绩归零
printf("输入第%d个人的信息:\n",i+1);
//输入学号和姓名
printf("学号:\n");
scanf("%d",&seqList->studentArray[i].id);
printf("姓名:\n");
scanf("%s",&seqList->studentArray[i].name);
//输入 数学、英语、政治、专业课1、专业课2的成绩
printf("数学:\n");
scanf_s("%d",&seqList->studentArray[i].score[0]);
printf("英语:\n");
scanf_s("%d",&seqList->studentArray[i].score[1]);
printf("政治:\n");
scanf_s("%d",&seqList->studentArray[i].score[2]);
printf("专业课1:\n");
scanf_s("%d",&seqList->studentArray[i].score[3]);
printf("专业课2:\n");
scanf_s("%d",&seqList->studentArray[i].score[4]);
printf("\n第%d个同学插入操作完毕!\n\n",i+1);
for(j=0;j<5;j++){//计算每个学生的总成绩
seqList->studentArray[i].total+=seqList->studentArray[i].score[j];
}
}
}
return 0;
};
//排序(首先输入要对所有人成绩排序的科目后按照升序排列,最后打印显示排序后的所有人成绩表。)
int Sort(seqList *seqList){
int option=0;
int i=0,j=0;
studentElement temp;
printf("开始排序......\n\n数学输入0,英语输入1,政治则输入2,专业课1输入3,专业课2输入4,总成绩输入5 \n");
printf("输入基于排序分数的科目:");
scanf("%d",&option);
if(option<6||option>=0){
//安装某科成绩升序排列
if(option>=0&&option<5){
for(i=0;i<seqList->length-1;i++){
for(j=i+1;j<seqList->length;j++){
if(seqList->studentArray[i].score[option]>seqList->studentArray[j].score[option]){
temp=seqList->studentArray[i];
seqList->studentArray[i]=seqList->studentArray[j];
seqList->studentArray[j]=temp;
}
}
}
printf("\n\n按某科成绩排序操作完毕!!!\n");
}
//安装某科成绩升序排列
if(option==5){
for(i=0;i<seqList->length-1;i++){
for(j=i+1;j<seqList->length;j++){
if(seqList->studentArray[i].total>seqList->studentArray[j].total){
temp=seqList->studentArray[i];
seqList->studentArray[i]=seqList->studentArray[j];
seqList->studentArray[j]=temp;
}
}
}
printf("\n按总成绩排序操作完毕!!!");
}
}else{
printf("输入不正确!!!");
}
return 0;
};
//查询(输入学号,将线性表中的每个学生的学号与给定的字符串相比较
//若有匹配则打印显示该学生多所有信息,否则出错误提示)。
int Puery(seqList *seqList){
int id=0;
int i=0;
printf("输入学号即可查询该生的所有信息:");
scanf_s("%d",&id);
if(id<0){
printf("\n学号不存在查询失败!\n\n");
}else{
for(i=0;i<sizeof(seqList->length);i++){//打印输出各学生的全部信息
if(id==seqList->studentArray[i].id){
printf("学号:%d\n姓名:%s\n数学:%d\n英语:%d\n政治:%d\n\
专业课1:%d\n专业课2:%d\n总分:%d\n\n恭喜!查询成功!\n\n",\
seqList->studentArray[i].id,\
seqList->studentArray[i].name,\
seqList->studentArray[i].score[0],\
seqList->studentArray[i].score[1],\
seqList->studentArray[i].score[2],\
seqList->studentArray[i].score[3],\
seqList->studentArray[i].score[4],\
seqList->studentArray[i].total);
}
}
}
return 0;
};
//输出所有信息
int Print(seqList *seqList){
int i=0;
for(i=0;i<seqList->length;i++){
printf("|学号:%d|姓名:%s|数学:%d|英语:%d|政治:%d|专业课1:%d|专业课2:%d|总分:%d\n",\
seqList->studentArray[i].id,\
seqList->studentArray[i].name,\
seqList->studentArray[i].score[0],\
seqList->studentArray[i].score[1],\
seqList->studentArray[i].score[2],\
seqList->studentArray[i].score[3],\
seqList->studentArray[i].score[4],\
seqList->studentArray[i].total);
}
printf("\n打印完毕!\n");
return 0;
}
测试:
手机终端 显示不全 可在代码区 左右 滑动屏幕
#include <stdio.h>
#include <stdlib.h>
#include "operation.h"
int main(){
seqList SL;
//seqList *SL=&sl;
InitList(&SL);
printf("--------------------------------------\n");
printf("排序前:\n");
Print(&SL);
printf("\n--------------------------------------\n");
Sort(&SL);
printf("\n--------------------------------------\n");
printf("排序后:\n");
Print(&SL);
printf("\n--------------------------------------\n");
Puery(&SL);
printf("\n--------------------------------------\n");
}
测试结果:
/* ------------------- 反爬声明o(*▽*)咻咻咻 --------------------
作者: 杨木发
版权声明:
本文为博主倾情原创文章,整篇转载请附上源文链接!
如果觉得本文对你有所收获,你的请评论点赞 与
合理优质的转发也将是鼓励支持我继续创作的动力,
更多精彩可百度搜索 杨木发 或:
个人网站: www.yangmufa.com ,
开源中国: https://my.oschina.net/yangmufa ,
Gitee: https://gitee.com/yangmufa ,
GitHub: https://github.com/yangmufa 。
坚持创作 善于总结 开源共享 高质进步。
------------------- 反爬声明o(*▽*)咻咻咻 -------------------- */