LinkedList.h
#pragma once
typedef STUDENT ElemType;
typedef struct SingleNode
{
ElemType data;
struct SingleNode *next;
}SingleLinkedList, *LinkList;
void ListInitialize(SingleLinkedList**head);
/*单链表初始化*/
int ListLength(SingleLinkedList *head);/*获得线性表的长度*/
int ListGet(SingleLinkedList *head, int i, ElemType *x);/*取序号为i的元素的值*/
int ListInsert(SingleLinkedList *head, int i, ElemType x);/*插入数据元素*/
int ListDelete(SingleLinkedList *head, int i, ElemType *x);/*删除元素*/
void ListDestroy(SingleLinkedList **head);
void reverse(SingleLinkedList *head);/*逆置,头插原理*/
LinkedList.cpp
#include"pch.h"
#include"LinkedList.h"
int locate_i = 0;
void ListInitialize(SingleLinkedList**head)
/*单链表初始化*/
{
if ((*head = (SingleLinkedList *)malloc(sizeof(SingleLinkedList))) == NULL)
exit(1);
(*head)->next = NULL;
}
int ListLength(SingleLinkedList *head)/*获得线性表的长度*/
{
SingleLinkedList *p = head;
int size = 0;
while (p->next != NULL)
{
p = p->next;
size++;
}
return size;
}
int ListGet(SingleLinkedList *head, int i, ElemType *x)/*取序号为i的元素的值*/
{
SingleLinkedList *p;
int j;
p = head;
j = -1;
while (p->next != NULL && j < i)
{
p = p->next;
j++;
}
if (j != i)
{
printf("取元素位置参数错!");
return 0;
}
*x = p->data;
return 1;
}
int ListInsert(SingleLinkedList *head, int i, ElemType x)/*插入数据元素*/
{
SingleLinkedList *p, *q;
int j;
p = head;
j = -1;
while (p->next != NULL && j < i - 1)
{
p = p->next;
j++;
}
if (j != i - 1)
{
printf("插入位置参数错");
return 0;
}
if ((q = (SingleLinkedList *)malloc(sizeof(SingleLinkedList))) == NULL)
exit(1);
q->data = x;
q->next = p->next;
p->next = q;
return 1;
}
int ListDelete(SingleLinkedList *head, int i, ElemType *x)/*删除元素*/
{
SingleLinkedList *p, *s;
int j;
p = head;
j = -1;
while (p->next != NULL && p->next->next != NULL && j < i - 1)
{
p = p->next;
j++;
}
if (j != i - 1)
{
printf("删除位置参数错!");
return 0;
}
s = p->next;
*x = s->data;
p->next = s->next;
free(s);
return 1;
}
void ListDestroy(SingleLinkedList **head)
{
SingleLinkedList *p, *p1;
p = *head;
while (p != NULL)
{
p1 = p;
p = p->next;
free(p1);
}
*head = NULL;
}
void reverse(SingleLinkedList *head)/*逆置,头插原理*/
{
SingleLinkedList *p, *q;
p = head->next;
head->next = NULL;
while (p)
{
q = p; p = p->next;
q->next = head->next;
head->next = q;
}
}
pch.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
#include<conio.h>
#define N 3000
typedef struct
{
double math;
double english;
double cLanguage;
}SCORE;
typedef struct Student
{
char Name[N];
char Sex[N];
char ID[N];
char teacher[N];
char _class[N];
SCORE myScore;
}STUDENT;
#include"LinkedList.h"
typedef struct
{
char Name[N];
LinkList p;
}TEACHER;
#define FIRSTMENU1 "
---------------------------------------------------\n"
#define FIRSTMENU2 "
学生成绩管理系统\n"
#define FIRSTMENU3 "
********************主菜单*************************\n"
#define FIRSTMENU4 "
*
1 输入学生信息
2 显示学生信息
*\n"
#define FIRSTMENU5 "
*
3 查询学生信息
4 修改学生信息
*\n"
#define FIRSTMENU6 "
*
5 显示排名信息
6 删除学生信息
*\n"
#define FIRSTMENU7 "
*
7 回收站
8 保存学生信息
*\n"
#define FIRSTMENU8 "
*
0 退出系统
*\n"
#define FIRSTMENU9 "
***************************************************\n"
#define SECONDMENU1_1 "
*****************输入学生信息**********************\n"
#define SECONDMENU1_2 "
请开始输入信息 注意每个类型的信息之间的空格\n"
#define SECONDMENU1_3 "
*
1 继续输入
0 返回上一级菜单
*\n"
#define SECONDMENU8_1 "
*****************保存学生信息**********************\n"
#define SECONDMENU2_1 "
*****************显示学生信息**********************\n"
#define SECONDMENU3_1 "
*****************查询学生信息**********************\n"
#define SECONDMENU3_2 "
*
1 按学号查询
2 按姓名查询
*\n"
#define SECONDMENU3_3 "
*
3 按班级查询
4 按辅导员查询
*\n"
#define HEADER1 "
--------------------------------学生信息-----------------------------------------------\n"
#define HEADER2 "
|学号
|姓名
|性别
|辅导员
|班级
|高数
|英语
|C语言
| \n"
#define HEADER3 "
|----------|----------|----------|----------|----------|----------|----------|----------| \n"
void Menu();
void ScanfStuIn(STUDENT students[], STUDENT students_display[], int &count_stu,TEACHER teacher[]);
int ScanfStuInError(char ch, int flag, int *p,int *a);
void stringinput(char* t, int lens,const char *p);
void
CountDown(int n);
void SaveData(STUDENT students[],int count_stu,int flag,TEACHER teacher[]);//flag用于这个函数是直接调用还是退出是调用
int ReadfromFile(STUDENT students[]);
void Display(STUDENT students[], int count_stu);
void Search(STUDENT students_display[]);
int* Search_(STUDENT students_display[], int xiabiao[], int &p, char ch);
int InspectID(STUDENT students_display[], const char *p);
void UpdataFile(FILE *fp,STUDENT students[]);
void UpdataFile_teacher(FILE *fp_teacher, TEACHER students[]);
int ReadfromFile_Teacher(TEACHER teacher_void[]);
void Display_Teacher(TEACHER teacher[], int count_teachers);
pch.cpp
#pragma once
#include"pch.h"
int i = 0;
bool save = false;
int count_file = 0;
int count_teachers = 0;
extern int locate_i;
void CountDown(int n)
{
while (n > 0)
{
printf("%d ", n);
Sleep(1000);
n--;
}
printf("\n");
}
void Menu()
{
system("cls");
printf(FIRSTMENU1);
printf(FIRSTMENU2);
printf(FIRSTMENU3);
printf(FIRSTMENU4);
printf(FIRSTMENU5);
printf(FIRSTMENU6);
printf(FIRSTMENU7);
printf(FIRSTMENU8);
printf(FIRSTMENU9);
}
void ScanfStuIn(STUDENT students[],STUDENT students_display[],int &count_stu,TEACHER teacher[])
{
system("cls");
int flag = 1,flag_=1; FILE *fp; char ch = '1';
int k=1,j,n=0,p = 0,a=0;//flag用于退出输入的循环,n是用于显示一次头顶菜单
while (flag)
{
while (a==0&&p==0)
{
printf(FIRSTMENU1);
printf(FIRSTMENU2);
printf(SECONDMENU1_1);
a = 1;
}
if (ch == '1')
{
while (1)
{
printf("
Id:"); stringinput(students[i].ID, 15, "Id:");
if (InspectID(students_display, students[i].ID) != 0)
{
printf("
Name:"); stringinput(students[i].Name, 15, "Name:");
printf("
Sex:"); stringinput(students[i].Sex, 15, "Sex:");
printf("
teacher:"); stringinput(students[i].teacher, 15, "teacher:");
if (count_teachers != 0)
{
for (j = 0; j < count_teachers; j++)
{
if (strcmp(students[i].teacher, teacher[j].Name) == 0)
{
flag_ = 0;
break;
}
else
{
flag_ = 1;
strcpy(teacher[j].Name, students[i].teacher);
ListInsert(teacher->p, locate_i, students[i]);
locate_i++;
count_teachers++;
}
}
}
else
{
strcpy(teacher[0].Name, students[i].teacher);
ListInsert(teacher->p, locate_i, students[i]);
locate_i++;
count_teachers++;
}
printf("
class:"); stringinput(students[i]._class, 15, "class:");
i++;
printf("
输入成功\n");
save = false;
count_stu++;
printf(SECONDMENU1_3);
printf("
请输入你的选择:");
ch = '0';
getchar();
break;//用于缓冲上一个字符
}
}
}
ch = getchar();
flag=ScanfStuInError(ch,flag,&p,&a);
}
//加一个以学号为基础的统计学生个数函数
}
int ScanfStuInError(char ch,int flag,int *p,int *a)
{
system("cls");
if (ch == '0')
{
p = 0;
return 0;
}
else if (ch == '1')
{
*p = 0;
*a = 0;
return 1;
}
else
while (flag)
{
printf(FIRSTMENU1);
printf(FIRSTMENU2);
printf(SECONDMENU1_1);
printf("
你的输入不符合要求,请按要求输入\n");
printf(SECONDMENU1_3);
*p = 1;
return 1;
}
}
void stringinput(char* t, int lens,const char *p)
{
char str[255];
do
{
scanf("%s", str);
//进行长度校验,超过lens值重新输入
if (strlen(str) > lens)
{
printf("
超过规定长度!\n");
printf("
%s", p);
}
} while (strlen(str)>lens);
//将输入的字条串保存到字符串t中
strcpy(t, str);
}
void SaveData(STUDENT students[],int count_stu,int flag,TEACHER teacher[])
{
if (count_stu == 0)
{
if (flag == 0)
{
system("cls");
printf(FIRSTMENU1);
printf(FIRSTMENU2);
printf("
******************确认退出中***********************\n");
printf("
无可保存的数据,自动退出:");
CountDown(3);
exit(0);
}
else
{
system("cls");
printf(FIRSTMENU1);
printf(FIRSTMENU2);
printf("
******************确认退出中***********************\n");
printf("
无可保存的数据,自动返回上一级菜单");
CountDown(3);
return;
}
}
system("cls");
FILE *fp;
if ((fp = fopen("D:\\demo.txt", "a")) == NULL)
{
printf("
文件保存失败\n");
exit(0);
}
fwrite(students, sizeof(STUDENT), count_stu, fp);
fclose(fp);
printf(FIRSTMENU1);
printf(FIRSTMENU2);
printf(SECONDMENU8_1);
printf("
保存成功
");
save = true;
fp = fopen("D:\\demo.txt", "r");
UpdataFile(fp, students);
fclose(fp);
FILE *fp_teacher;
if ((fp_teacher = fopen("D:\\teacher.txt", "a")) == NULL)
{
exit(0);//崩程序
}
fwrite(teacher, sizeof(TEACHER), count_teachers, fp_teacher);
fclose(fp_teacher);
fp_teacher = fopen("D:\\teacher.txt", "r");
UpdataFile_teacher(fp_teacher, teacher);
fclose(fp_teacher);
system("pause");
}
int ReadfromFile(STUDENT students[])
{
system("cls");
FILE *fp; int i;
if ((fp = fopen("D:\\demo.txt", "r")) == NULL)
{
printf("
文件保存失败\n");
exit(0);
}
for (i = 0; !feof(fp); i++)
{
fread(&students[i], sizeof(STUDENT), 1, fp);
}
fclose(fp);
return i - 1;
}
void Display(STUDENT students[], int count_stu)
{
int i = 0;
system("cls");
printf(FIRSTMENU1);
printf(FIRSTMENU2);
printf(SECONDMENU2_1);
printf(HEADER1);
printf(HEADER2);
printf(HEADER3);
for (i; i < count_stu; i++)
{
printf("
%s", students[i].ID);
printf("
%s", students[i].Name);
printf("
%s", students[i].Sex);
printf("
%s", students[i].teacher);
printf("
%s", students[i]._class);
printf("\n");
}
system("pause");
}
void Search(STUDENT students_display[])
{
int i = 0; char c[N]; char select; int flag = 0, count = 0;
int xiabiao[N];
system("cls");
printf(FIRSTMENU1);
printf(FIRSTMENU2);
printf(SECONDMENU3_1);
printf(SECONDMENU3_2);
printf(SECONDMENU3_3);
printf("
*
0 返回上一级菜单
*\n");
printf("
请输入你的选择:");
while (1)
{
scanf("%s", c);
//stringinput(c, 15, "");
if (c[1] == '\0')
{
select = c[0];
if (select >= '0' && select <= '4')
{
switch (select)
{
case '1':
Search_(students_display, xiabiao,count,'1');
if (count==0)
{
printf("
查询不到该信息;");
}
else
{
for (i = 0; i < count; i++)
{
//Display(&students_display[xiabiao[i]], 1);//查不了重复的写法
Display(&students_display[*xiabiao], count);//可以查重复的写法
}
}
//按学号查询
break;
case '2':
//按姓名查询
Search_(students_display, xiabiao, count, '2');
if (count == 0)
{
printf("查询不到该信息;");
}
else
{
for (i = 0; i < count; i++)
{
Display(&students_display[*xiabiao], count);
}
}
break;
case '3':
Search_(students_display, xiabiao, count, '3');
if (count == 0)
{
printf("查询不到该信息;");
}
else
{
for (i = 0; i < count; i++)
{
Display(&students_display[*xiabiao], count);
}
}
//按班级查询
break;
case '4':
//按辅导员查询
break;
case '0':
return;
default:
break;
}
}
else
{
printf("
你的输入不符合要求,请按要求输入\n");
printf("
");
}
}
else
{
printf("
你的输入不符合要求,请按要求输入\n");
printf("
");
}
}
}
//p是用来记录找到了多少个人
int* Search_(STUDENT students_display[], int xiabiao[], int &p,char ch)
{
FILE *fp; char find[N];
if (ch == '1')
printf("
请输入你要查询的学号:");
if (ch == '2')
printf("
请输入你要查询的姓名:");
if (ch == '3')
printf("
请输入你要查询的班级:");
if (ch == '4')
printf("
请输入你要查询的辅导员:");
scanf("%s", find);
int i; p = 0;
if ((fp = fopen("D:\\demo.txt", "r")) == NULL)
{
printf("查询失败\n");
return NULL;
}
for (i = 0; !feof(fp); i++)
{
fread(&students_display[i], sizeof(STUDENT), 1, fp);
}
fclose(fp);
if (ch == '1')
{
for (i = 0; i < count_file; i++)
{
if (strcmp(students_display[i].ID, find) == 0)
{
xiabiao[p++] = i;
}
}
}
else if (ch == '2')
{
for (i = 0; i < count_file; i++)
{
if (strcmp(students_display[i].Name, find) == 0)
{
xiabiao[p++] = i;
}
}
}
else if (ch == '3')
{
for (i = 0; i < count_file; i++)
{
if (strcmp(students_display[i]._class, find) == 0)
{
xiabiao[p++] = i;
}
}
}
return xiabiao;
}
int InspectID(STUDENT students_display[], const char *p)
{
FILE *fp; int j;
if ((fp = fopen("D:\\demo.txt", "r")) == NULL)
{
printf("
文件保存失败\n");
exit(0);
}
for (j = 0; !feof(fp); j++)
{
fread(&students_display[j], sizeof(STUDENT), 1, fp);
if (strcmp(students_display[j].ID,p)==0)
{
printf("
你输入的ID已存在,请重新输入:");
CountDown(3);
return 0;
}
}
fclose(fp);
return 1;
}
void UpdataFile(FILE *fp,STUDENT students[])
{
while (!feof(fp))//如果是在读取函数之前进行判断,则如果文件最后一行是空白行,可能会造成内存错误。baidu
if (fread(&students[0], sizeof(STUDENT), 1, fp) == 1)
count_file++;
}
void UpdataFile_teacher(FILE *fp_teacher, TEACHER teachers[])
{
while (!feof(fp_teacher))//如果是在读取函数之前进行判断,则如果文件最后一行是空白行,可能会造成内存错误。baidu
if (fread(&teachers[0], sizeof(TEACHER), 1, fp_teacher) == 1)
count_teachers++;
}
int ReadfromFile_Teacher(TEACHER teacher_void[])
{
FILE *fp_teacher; int i;
if ((fp_teacher = fopen("D:\\teacher.txt", "r")) == NULL)
{
printf("
文件保存失败\n");
exit(0);
}
for (i = 0; !feof(fp_teacher); i++)
{
fread(&teacher_void[i], sizeof(TEACHER), 1, fp_teacher);
}
fclose(fp_teacher);
return i - 1;
}
void Display_Teacher(TEACHER teacher[], int count_teachers)
{
int i = 0;
system("cls");
printf(FIRSTMENU1);
printf(FIRSTMENU2);
printf(SECONDMENU2_1);
printf(HEADER1);
printf(HEADER2);
printf(HEADER3);
for (i; i < count_teachers; i++)
{
printf("
%s", teacher[i].p->data.Name);
printf("\n");
}
system("pause");
}
main.cpp
#include"pch.h"
extern bool save;
extern int count_file;
extern int count_teachers;
void main(int a)
{
char select; int n = 0, count_stu = 0; int flag = 0;
char c[N];
FILE *fp,*fp_teacher;
STUDENT students[N], students_dispaly[N];
TEACHER teachers[N],teacher_void[N];
ListInitialize(&teachers->p);
fp = fopen("D:\\demo.txt", "a+");//打开保存学生信息的文件
if (fp == NULL)
{
printf("
文件打开失败\n");
exit(0);
}
UpdataFile(fp, students);
fclose(fp);
fp_teacher = fopen("D:\\teacher.txt", "a+");
if (fp_teacher == NULL)
{
printf("
老师信息数据库文件打开失败\n");
exit(0);
}
UpdataFile_teacher(fp_teacher, teachers);
fclose(fp_teacher);
Menu();
while (1)
{
system("cls");
Menu();
printf("
请输入你的选择:");
scanf(" %s",c);
if (c[1] == '\0'&&isdigit(c[0]) != 0)
{
select = c[0];
if (isdigit(select) != 0)//判断输入是不是数字
{
//fflush(stdin);
if (select > '0' && select <= '8')
{
switch (select)
{
case '1':
ScanfStuIn(students,students_dispaly,count_stu,teachers);
break;
case '2':
Display(students_dispaly, ReadfromFile(students_dispaly));
//显示学生信息
break;
case '3':
Search(students_dispaly);
//查询学生信息
break;
case '4':
//修改学生信息
break;
case '5':
//显示排名信息
break;
case '6':
//删除学生信息
break;
case '7':
Display_Teacher(teacher_void, ReadfromFile_Teacher(teacher_void));
//回收站
break;
case '8':
SaveData(students, count_stu,1,teachers);
//保存信息
break;
default:
//输入错误
break;
}
}
else if (select == '0')
{
//还要判断是否保存了数据
if (save==true)
{
system("cls");
printf(FIRSTMENU1);
printf(FIRSTMENU2);
printf("
退出成功 感谢你的使用");
break;
}
else
{
system("cls");
printf(FIRSTMENU1);
printf(FIRSTMENU2);
printf("
******************确认退出中***********************\n");
printf("
你的数据还没有保存\n");
printf("
1 保存后退出
其他 直接退出\n");
printf("
选择:");
scanf(" %s", c);
select = c[0];
if (c[1] == '\0'&&select == '1')
{
SaveData(students, count_stu,0,teachers);
printf("
保存成功,等待退出:");
CountDown(3);
exit(0);
}
else
{
printf("
等待退出:");
CountDown(3);
exit(0);
}
}
}
else
{
printf("
你的输入有错误,请重新输入:");
}
}
}
else
{
printf("
你的输入有错误,请重新输入:");
CountDown(3);//倒计时
}
}
system("pause");
}