#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include <string.h>
typedef struct stu
{
long stuID; //学号
char stuname[10]; //名字
char stusex; //性别
int score; //分数
}STU;
typedef struct node //创建结点类型
{
STU stu; //数据域
struct node* next; //指向下一个节点的指针
}NODE;
NODE* head = NULL; //定义头指针
NODE* creatlist();
void start();
void inputstudent(STU stu[]);
void savestudent();
void readstudent();
void printfstudent();
#define N 30
int main()
{
NODE* list = creatlist();
STU stu[N];
int m = 4;
while (1)
{
start();
char ch = _getch();
switch (ch)
{
case '1': //录入学生信息
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(STU stu[])
{
NODE* newnode = (NODE*)malloc(sizeof(NODE)); //创建一个新结点来作头结点,使newnode这个指针可以通过->来当作结构体变量来用
newnode->next = NULL;
if (head == NULL) //遍历
{
head = newnode;
}
else
{
newnode->next = head;
head = newnode; //newnode = head; 修改
}
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", &newnode->stu.score);
}
void savestudent() //保存学生信息
{
FILE* pf = fopen("pph.txt", "w"); //创建并打开文件
if (pf == NULL) //判断打开文件是否失败
{
printf("打开文件失败\n");
return;
}
NODE* p = head;
while (p != NULL)
{
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 (!feof(pf))
{
NODE* newnode = (NODE*)malloc(sizeof(NODE));
fread(&newnode->stu, 1, sizeof(STU), pf);
newnode->next = NULL;
//头插法
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", P->stu.stuID, P->stu.stuname, P->stu.stusex, P->stu.score);
P = P->next;
}
system("pause");
}
After reading the data, there is a garbled code in front of the print, and the 123 pieces of SAN m 99 behind is the content I input. How to solve it?
0 Answer
No answer yet
这家伙很懒,什么都没留下...