#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include <string.h>
typedef struct stu
{
long stuID; //学号
char stuname[10]; //名字
char stusex; //性别
int score[4]; //分数
int total; //总分
int aver; //平均分
int line; //排名
}STU;
typedef struct node //创建结点类型
{
STU stu; //数据域
struct node* next; //指向下一个节点的指针
}NODE;
NODE* head = NULL; //定义头指针
NODE* creatlist();
void start();
void inputstudent(); //void inputstudent(STU stu[]); 修改
void savestudent();
void readstudent();
void printfstudent();
//#define N 30 修改
int main()
{
int arr[100] = { 1 }; //存每名学生的名次
//NODE* list = creatlist(); //修改
//STU stu[N]; //修改
int m = 4;
while (1)
{
start();
char ch = _getch();
switch (ch)
{
case '1': //录入学生信息
inputstudent(); //inputstudent(stu); 修改
break;
case '2': //保存学生信息
savestudent();
break;
case '3'://读取学生信息
readstudent();
break;
case '4': //打印学生成绩
printfstudent();
break;
case '5': //按总分由高到低排出名次
break;
case '6': //按总分由低到高排出名次
break;
case '7': //按学号由小到大排出成绩表
break;
case '8': //按姓名字典顺序排序排出成绩表
break;
}
}
return 0;
}
void start()
{
printf("*****************************************\n");
printf("欢迎使用学生成绩管理系统 *\n");
printf("*1.录入学生信息 *\n");
printf("*2.保存学生信息 *\n");
printf("*3.读取学生信息 *\n");
printf("*4.打印学生信息 *\n");
printf("*5.按总分由高到低排出名次 *\n");
printf("*6.按总分由低到高排出名次 *\n");
printf("*7.按学号由小到大排出成绩表 *\n");
printf("*8.按姓名字典顺序排序排出成绩表 *\n");
printf("*9.根据学号查询学生成绩及排名 *\n");
printf("*0.根据姓名查询学生成绩及排名 *\n");
printf("*****************************************\n");
}
NODE* creatlist() //创建表头表示整个链表即创建链表(表头可以是头结点,也可以是数据结点,通常是头结点)
{
NODE* headNode = (NODE*)malloc(sizeof(NODE));
headNode->next = NULL;
return headNode;
}
void inputstudent() //void inputstudent(STU stu[]) 修改
{
NODE* newnode = (NODE*)malloc(sizeof(NODE)); //创建一个新结点来作头结点,使newnode这个指针可以通过->来当作结构体变量来用
newnode->next = NULL;
if (head == NULL) //遍历
{
head = newnode;
}
else
{
newnode->next = head;
head = newnode;
}
printf("学号:");
scanf_s("%ld", &newnode->stu.stuID);
printf("姓名:");
scanf_s("%s", newnode->stu.stuname, 10);
printf("性别:");
scanf_s(" %c", &newnode->stu.stusex, 1);
printf("成绩:");
scanf_s("%d %d %d %d", &newnode->stu.score[0], &newnode->stu.score[1], &newnode->stu.score[2], &newnode->stu.score[3]);
newnode->stu.total = newnode->stu.score[0] + newnode->stu.score[1] + newnode->stu.score[2] + newnode->stu.score[3];
printf("总分:%d\n", newnode->stu.total);
newnode->stu.aver = (newnode->stu.score[0] + newnode->stu.score[1] + newnode->stu.score[2] + newnode->stu.score[3]) / 4.0;
printf("平均分:%d\n", newnode->stu.aver);
// if (newnode->next->stu.total > newnode->stu.total)
// newnode->next->stu.line++;
printf("名次:%d", newnode->stu.line);
}
void savestudent() //保存学生信息
{
FILE* pf = fopen("pph.txt", "w"); //创建并打开文件
if (pf == NULL) //判断打开文件是否失败
{
printf("打开文件失败\n");
return;
}
NODE* p = head;
while (p != NULL)
{
fwrite(&p->stu, sizeof(STU), 1, pf); //修改
//fwrite(&p->stu, 1, sizeof(STU), pf); //修改
p = p->next;
}
fclose(pf);
printf("数据保存成功\n");
}
void readstudent() //读取学生信息
{
FILE* pf = fopen("pph.txt", "r"); //打开文件
if (pf == NULL) //判断打开文件是否失败
{
printf("err!\n");
return;
}
while (1) //(!feof(pf)) 修改
{
NODE* newnode = (NODE*)malloc(sizeof(NODE));
newnode->next = NULL;
if (fread(&newnode->stu, sizeof(STU), 1, pf) != 1) { //fread(&newnode->stu, 1, sizeof(STU), pf);
free(newnode); //修改
break;
} //修改
//头插法
if (head == NULL)
{
head = newnode;
}
else
{
newnode->next = head;
head = newnode;
}
}
printf("加载数据成功\n");
fclose(pf);
}
void printfstudent()
{
NODE* P = head;
while (P != NULL)
{
printf("%ld %s %c %d %d %d %d %d %d %d", P->stu.stuID, P->stu.stuname, P->stu.stusex,
P->stu.score[0], P->stu.score[1], P->stu.score[2], P->stu.score[3],P->stu.total, P->stu.aver); //修改
P = P->next;
break;
}
system("pause");
}
想在122-125行之间实现实时给学生的总分数排名词,如何实现?