#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"source_add.h"
Student stinputdata(Student StudentNode)
{
printf("请依次输入以下信息:姓名,学号,专业,班级,性别(男,女)\n");
scanf("%s %d %s %d %s",StudentNode.name,&StudentNode.xuehao,&StudentNode.zhuanye,&StudentNode.Class,StudentNode.sex);
return StudentNode;
}
//输入课程编号,课程名称,课程学分,课程成绩
Course coinputdata(Course CourseNode)
{
while (1) {
printf("请依次输入以下信息:课程编号,课程名称,课程学分,课程成绩(如果输入信息有误,将课程成绩输入小于0或者大于100即可重新输入)\n");
scanf("%d %s %f %f",&CourseNode.daihao,CourseNode.name,&CourseNode.xuefen,&CourseNode.chengji);
if (CourseNode.chengji >= 0 && CourseNode.chengji <= 100) {
break; // 成绩满足条件,跳出循环
} else {
printf("输入的成绩无效,请确保成绩在0-100范围内!\n");
}
}
float a=0;
a = CourseNode.chengji;
if (a < 60) {CourseNode.GPA = 0; }
else if (a >= 90) { CourseNode.GPA = 4.0; }
else if (a<64&&a >= 60) { CourseNode.GPA = 1.0; }
else if (a < 67&&a>=64) { CourseNode.GPA = 1.3; }
else if (a < 70 && a >= 67) { CourseNode.GPA = 1.7; }
else if (a < 74 && a >= 70) { CourseNode.GPA = 2.0; }
else if (a < 77 && a >= 74) { CourseNode.GPA = 2.3; }
else if (a < 80 && a >= 77) { CourseNode.GPA = 2.7; }
else if (a < 84 && a >= 80) { CourseNode.GPA = 3.0; }
else if (a < 87 && a >= 84) { CourseNode.GPA = 3.3; }
else if (a < 90 && a >= 87) { CourseNode.GPA = 3.7; }
return CourseNode;
}
Keyan putdata(Keyan Keyannode){
printf("请输入科研竞赛获奖信息\n");
scanf("%s",&Keyannode.name);
return Keyannode;
}
struct Lstudent* initStudentList() {
return NULL;
}
struct Lstudent* addStudentToList(struct Lstudent* head) {
Student newStudent;
struct Lstudent* newNode = (struct Lstudent*)malloc(sizeof(struct Lstudent));
if (!newNode) {
printf("内存分配失败!\n");
return head;
}
newStudent = stinputdata(newStudent);
// 检查学号是否已存在
struct Lstudent* currStudent = head;
while (currStudent != NULL) {
if (currStudent->data.xuehao == newStudent.xuehao) {
printf("学号 %d 已存在,不能重复添加,请重新输入!\n", newStudent.xuehao);
free(newNode); // 释放新分配的内存
return head;
}
currStudent = currStudent->next;
}
newNode->chead=NULL;
newNode->data = newStudent;
newNode->next = head;
return newNode;
}
void printStudentList(struct Lstudent* head) {
while (head != NULL) {
printf("姓名:%s,学号:%d,班级:%d,性别:%s,课程平均绩点:%.2f, 总平均成绩:%.2f\n",
head->data.name,
head->data.xuehao,
head->data.Class,
head->data.sex,
head->data.KGPA,
head->data.ZGPA);
head = head->next;
}
}
CourseListNode* addCourseToListForStudent(CourseListNode** chead) {
// 调用输入函数获取课程信息
Course newCourse = coinputdata((Course){});
// 创建新节点并分配内存
CourseListNode* newNode = (CourseListNode*)malloc(sizeof(CourseListNode));
if (!newNode) {
printf("内存分配失败!\n");
return *chead;
}
// 将获取的课程信息赋值给新节点
newNode->data = newCourse;
newNode->next = *chead;
*chead = newNode;
return newNode;
}
KeyanListNode* addKeyan(KeyanListNode** khead){
Keyan newKeyan = putdata((Keyan){});
KeyanListNode* newNode = (KeyanListNode*)malloc(sizeof(KeyanListNode));
if (!newNode) {
printf("内存分配失败!\n");
return *khead;
}
newNode->data = newKeyan;
newNode->next = *khead;
*khead = newNode;
return newNode;
}
void printStudentCourses(CourseListNode* chead) {
CourseListNode* current = chead;
if(current==NULL){
printf("无课程信息\n");
}
while (current != NULL) {
printf("课程编号:%d,课程名称:%s,课程学分:%.1f,课程成绩:%.1f,课程绩点:%.1f\n",
current->data.daihao, current->data.name, current->data.xuefen, current->data.chengji,current->data.GPA);
current = current->next;
}
}
void printStudentKeyan(KeyanListNode* khead) {
KeyanListNode* current = khead;
if(current==NULL){
printf("无科研竞赛信息\n");
}
while (current != NULL) {
printf("科研竞赛获奖信息:%s,\n",
current->data.name);
current = current->next;
}
}
struct Lstudent* findStudentByXuehao(struct Lstudent* studentsHead, int targetXuehao) {
struct Lstudent* currStudent = studentsHead;
while (currStudent != NULL) {
if (currStudent->data.xuehao == targetXuehao) {
return currStudent; // 找到匹配的学生节点,返回其指针
}
currStudent = currStudent->next;
}
return NULL; // 如果没有找到匹配的学生,则返回NULL
}
struct Lstudent* findStudentByName(struct Lstudent* studentsHead, const char* targetName) {
struct Lstudent* currStudent = studentsHead;
while (currStudent != NULL) {
if (strcmp(currStudent->data.name, targetName) == 0) { // 按照姓名进行比较
return currStudent; // 找到匹配的学生节点,返回其指针
}
currStudent = currStudent->next;
}
return NULL; // 如果没有找到匹配的学生,则返回NULL
}
void save(struct Lstudent* head, const char* filename) {
FILE* fp = fopen(filename, "ab");
if (fp == NULL) {
printf("无法打开文件进行写入!\n");
return;
}
while (head != NULL) {
// 写入学生信息
fprintf(fp,"s");
fprintf(fp, " %d %s %s %d %s %.2f %.2f\n",
head->data.xuehao, head->data.name,head->data.zhuanye,head->data.Class, head->data.sex,
head->data.ZGPA, head->data.KGPA);
// 写入课程信息
CourseListNode* courseNode = head->chead;
KeyanListNode* keyanNode = head->khead;
while (courseNode != NULL) {
fprintf(fp,"c");
fprintf(fp, " %d %s %.1f %.1f %.1f\n",
courseNode->data.daihao, courseNode->data.name,
courseNode->data.xuefen, courseNode->data.chengji,courseNode->data.GPA);
courseNode = courseNode->next;
}
while (keyanNode != NULL) {
fprintf(fp,"k");
fprintf(fp, " %s\n",
keyanNode->data.name);
keyanNode = keyanNode->next;
}
head = head->next;
}
int result = fclose(fp);
if (result != 0) {
perror("fclose failed");
} else {
printf("文件写入成功并关闭\n");
}
}
void load(struct Lstudent** head, const char* filename) {
*head = NULL; // 如果没读到任何数据需要返回空链表
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf("无法打开文件进行读取!\n");return;
}
// 临时存储学生信息
Student tempStudent;
CourseListNode* tempCourseNode = NULL;
KeyanListNode* tempKeyanNode = NULL;
struct Lstudent* currentStudent = NULL;
struct Lstudent* prevStudent = NULL;
while (1) {
/* 把你的文件格式改成空格分隔,每行开头用一个字符标记是学生还是课程
* 为什么非要把简单的问题复杂化,给自己增加解析难度呢
* S 33 yu 15 男 0.00 0.00
C 3 英语 4.00 150.00
C 2 数学 3.00 140.00
C 1 语文 2.00 120.00
S 123 he 15 男 0.00 0.00
...
*/
char scmark;
fscanf(fp, " %c", &scmark);
if (scmark == 's') {
// 学生
int readCount = fscanf(fp, "%d%s%s%d%s%f%f",
&tempStudent.xuehao, tempStudent.name,tempStudent.zhuanye, &tempStudent.Class, tempStudent.sex,
&tempStudent.ZGPA, &tempStudent.KGPA);
// 抵达文件尾或无效数据
if (readCount != 7)
break;
// 创建新的学生节点
currentStudent = (struct Lstudent*) malloc(sizeof(struct Lstudent));
if (currentStudent == NULL) {
printf("内存分配失败!\n");
break;
}
currentStudent->data = tempStudent;
currentStudent->chead = NULL;
currentStudent->khead=NULL;
currentStudent->next = NULL;
// 如果这不是第一个学生节点,将其链接到链表末尾
if (prevStudent != NULL) {
prevStudent->next = currentStudent;
} else { // 第一个学生节点,更新链表头
*head = currentStudent;
}
prevStudent = currentStudent;
}
else if (scmark == 'c') {
// 课程
// 读取并添加课程信息
tempCourseNode = (CourseListNode*) malloc(sizeof(CourseListNode));
if (tempCourseNode == NULL) {
printf("内存分配失败!\n");
break;
}
int readCount = fscanf(fp, "%d%s%f%f%f",
&tempCourseNode->data.daihao, tempCourseNode->data.name,
&tempCourseNode->data.xuefen, &tempCourseNode->data.chengji,&tempCourseNode->data.GPA);
// 抵达文件尾或无效数据
if (readCount != 5) {
// 需要释放tempCourseNode否则内存泄漏
free(tempCourseNode);
break;
}
if (currentStudent) {
tempCourseNode->next = currentStudent->chead;
currentStudent->chead = tempCourseNode;
}
}else if(scmark == 'k'){
tempKeyanNode=(KeyanListNode*)malloc(sizeof(KeyanListNode));
if(tempKeyanNode==NULL){
printf("内存分配失败!\n");
break;
}
int readCount = fscanf(fp, "%s",&tempKeyanNode->data.name);
// 抵达文件尾或无效数据
if (readCount != 1) {
// 需要释放tempCourseNode否则内存泄漏
free(tempKeyanNode);
break;
}
if (currentStudent) {
tempKeyanNode->next = currentStudent->khead;
currentStudent->khead = tempKeyanNode;
}
}
else
break;
}
fclose(fp);
}
void freeStudentList(struct Lstudent* head) {
struct Lstudent* currentStudent = head;
struct Lstudent* nextStudent;
while (currentStudent != NULL) {
CourseListNode* courseNode = currentStudent->chead;
CourseListNode* nextCourseNode;
KeyanListNode* keyanNode = currentStudent->khead;
KeyanListNode* nextKeyanNode;
while (courseNode != NULL) {
nextCourseNode = courseNode->next;
free(courseNode);
courseNode = nextCourseNode;
}
while(keyanNode!=NULL){
nextKeyanNode = keyanNode->next;
free(keyanNode);
keyanNode = nextKeyanNode;
}
nextStudent = currentStudent->next;
free(currentStudent);
currentStudent = nextStudent;
}
}
void printStudentListAndCourses(struct Lstudent* head) {
while (head != NULL) {
printf("学生信息:学号:%d,姓名:%s,专业:%s, 班级:%d,性别:%s,总平均成绩:%.2f,课程平均绩点:%.2f\n",
head->data.xuehao, head->data.name, head->data.zhuanye,head->data.Class, head->data.sex,
head->data.ZGPA, head->data.KGPA);
// 打印课程信息
CourseListNode* courseNode = head->chead;
if(courseNode==NULL){
printf("无课程信息\n");
}
while (courseNode != NULL) {
printf("课程信息:课程编号:%d,课程名称:%s,课程学分:%.1f,课程成绩:%.1f,课程绩点:%.1f\n",
courseNode->data.daihao, courseNode->data.name,
courseNode->data.xuefen, courseNode->data.chengji,courseNode->data.GPA);
courseNode = courseNode->next;
}
KeyanListNode* keyanNode = head->khead;
if(keyanNode==NULL){
printf("无科研竞赛信息\n");
}
while(keyanNode!=NULL){
printf("科研竞赛信息:%s\n",keyanNode->data.name);
keyanNode = keyanNode->next;
}
printf("\n\n");
head = head->next;
}
}
void printtargetstudent(struct Lstudent* head){
if(head != NULL) {
printf("姓名:%s,学号:%d,专业:%s,班级:%d,性别:%s,课程平均绩点:%.2f, 总平均成绩:%.2f\n",
head->data.name,
head->data.xuehao,
head->data.zhuanye,
head->data.Class,
head->data.sex,
head->data.KGPA,
head->data.ZGPA);
}
}
void cover(struct Lstudent* head, const char* filename) {
FILE* fp = fopen(filename, "wb");
if (fp == NULL) {
printf("无法打开文件进行写入!\n");
return;
}
if (head == NULL) {
return;
}
while (head != NULL) {
// 写入学生信息
fprintf(fp,"s");
fprintf(fp, " %d %s %s %d %s %.2f %.2f\n",
head->data.xuehao, head->data.name,head->data.zhuanye,head->data.Class, head->data.sex,
head->data.ZGPA, head->data.KGPA);
// 写入课程信息
CourseListNode* courseNode = head->chead;
KeyanListNode* keyanNode = head->khead;
while (courseNode != NULL) {
fprintf(fp,"c");
fprintf(fp, " %d %s %.1f %.1f %.1f\n",
courseNode->data.daihao, courseNode->data.name,
courseNode->data.xuefen, courseNode->data.chengji,courseNode->data.GPA);
courseNode = courseNode->next;
}
while (keyanNode != NULL) {
fprintf(fp,"k");
fprintf(fp, " %s\n",keyanNode->data.name);
keyanNode = keyanNode->next;
}
head = head->next;
}
int result = fclose(fp);
if (result != 0) {
perror("fclose failed");
} else {
printf("文件写入成功并关闭\n");
}
}
void KGPA(struct Lstudent* head) {
if (head == NULL) {
return;
}
float a = 0, b = 0, c = 0, d = 0, e = 0;
struct Lstudent* p = head;
struct Lcourse* q;
while (p != NULL) {
if (p->chead == NULL) {
p->data.KGPA = 0.00;
p = p->next;
continue;
}
q = p->chead;
while (q != NULL) {
a = q->data.GPA;
b = q->data.xuefen;
c = a * b;
d = d + c;
e = e + b;
q = q->next;
}
if (e == 0) {
p->data.KGPA = 0.00;
} else {
p->data.KGPA = d / e;
}
d = 0;
e = 0;
p = p->next;
}
}
int countStudents(struct Lstudent* studentsHead) {
int studentCount = 0;
struct Lstudent* currStudent = studentsHead;
while (currStudent != NULL) {
studentCount++;
currStudent = currStudent->next;
}
return studentCount;
}
int countCoursesForStudent(struct Lstudent* student) {
int courseCount = 0;
CourseListNode* currentCourse = student->chead;
while (currentCourse != NULL) {
courseCount++;
currentCourse = currentCourse->next;
}
return courseCount;
}
void ZGPA(struct Lstudent* head) {
if (head == NULL) {
return;
}
float a = 0, b = 0, c = 0, d = 0, e = 0;
struct Lstudent* p = head;
struct Lcourse* q;
while (p != NULL) {
if (p->chead == NULL) {
p->data.ZGPA = 0.00;
p = p->next;
continue;
}
q = p->chead;
while (q != NULL) {
a = q->data.chengji;
b = q->data.xuefen;
c = a * b;
d = d + c;
e = e + b;
q = q->next;
}
if (e == 0) {
p->data.ZGPA = 0.00;
} else {
p->data.ZGPA = d / e;
}
d = 0;
e = 0;
p = p->next;
}
}
struct Lstudent* deleteStudentByXuehao(struct Lstudent* head, int targetXuehao) {
struct Lstudent* curr = head;
struct Lstudent* prev = NULL;
while (curr != NULL && curr->data.xuehao != targetXuehao) {
prev = curr;
curr = curr->next;
}
if (curr != NULL) {
CourseListNode* courseNode = curr->chead;
CourseListNode* temp;
while (courseNode != NULL) {
temp = courseNode->next;
free(courseNode);
courseNode = temp;
}
if (prev == NULL) {
head = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
}
else{
printf("未找到\n");
}
return head;
}
CourseListNode* deleteCourseOfStudentByDaihao(struct Lstudent* studentNode, int targetDaihao) {
if (studentNode == NULL || studentNode->chead == NULL) {
printf("该学生课程信息为空\n");
return NULL;
}
CourseListNode* prev = NULL;
CourseListNode* curr = studentNode->chead;
while (curr != NULL && curr->data.daihao != targetDaihao) {
prev = curr;
curr = curr->next;
}
if (curr != NULL) {
if (prev == NULL) {
studentNode->chead = curr->next;
} else {
prev->next = curr->next;
}
CourseListNode* p = curr;
curr = curr->next;
free(p);
printf("已删除\n");
}
else{
printf("未找到课程编号\n");
}
return curr;
}
CourseListNode* deleteCourseOfStudentByName(struct Lstudent* studentNode, char * targetName) {
if (studentNode == NULL || studentNode->chead == NULL) {
printf("该学生课程信息为空\n");
return NULL;
}
CourseListNode* prev = NULL;
CourseListNode* curr = studentNode->chead;
while (curr != NULL && strcmp(curr->data.name, targetName) != 0) {
prev = curr;
curr = curr->next;
}
if (curr != NULL) {
if (prev == NULL) {
studentNode->chead = curr->next;
} else {
prev->next = curr->next;
}
CourseListNode* p = curr;
curr = curr->next;
free(p);
printf("已删除\n");
}
else{
printf("未找到\n");
}
return curr;
}
struct CourseListNode* clearStudentCourses(struct Lstudent* targetStudent) {
CourseListNode* courseNode = targetStudent->chead;
CourseListNode* tempCourseNode;
while (courseNode != NULL) {
tempCourseNode = courseNode->next;
free(courseNode);
courseNode = tempCourseNode;
}
targetStudent->chead = NULL;
targetStudent->data.KGPA = 0;
targetStudent->data.ZGPA = 0;
return (struct CourseListNode*) targetStudent->chead;
}
//清空所有学生信息
struct Lstudent* clearStudentList(struct Lstudent* head,const char* filename) {
struct Lstudent* current = head;
struct Lstudent* next;
while (current != NULL) {
next = current->next;
CourseListNode* courseNode = current->chead;
KeyanListNode* keyanNode = current->khead;
CourseListNode* tempCourseNode;
KeyanListNode* tempKeyanNode;
while (courseNode != NULL) {
tempCourseNode = courseNode->next;
free(courseNode);
courseNode = tempCourseNode;
}
while(keyanNode != NULL){
tempKeyanNode = keyanNode->next;
free(keyanNode);
keyanNode = tempKeyanNode;
}
free(current);
current = next;
}
head = NULL;
FILE* fp = fopen(filename, "w+");
if (fp != NULL) {
fclose(fp); // 关闭文件即可清空内容("w+" 模式下)
} else {
printf("无法打开文件进行清空!\n");
}
return (struct Lstudent *)head;
}
#ifndef STUDENT_COURSE_LIST_H
#define STUDENT_COURSE_LIST_H
#include <stdio.h>
#include <stdlib.h>
typedef struct student {
int xuehao;
char name[30];
char zhuanye[30];
int Class;
char sex[6];
float ZGPA;
float KGPA;
} Student;
typedef struct keyanjingsai{
char name[50];
}Keyan;
typedef struct course {
int daihao;
char name[30];
float GPA;
float xuefen;
float chengji;
} Course;
typedef struct Lcourse {
struct course data;
struct Lcourse *next;
} CourseListNode;
typedef struct Lkeyan{
struct keyanjingsai data;
struct Lkeyan *next;
}KeyanListNode;
typedef struct Lstudent {
struct student data;
struct Lcourse *chead;
struct Lkeyan *khead;
struct Lstudent *next;
};
// 输入姓名,学号,班级,性别
Student stinputdata(Student);
// 输入课程编号,课程名称,课程学分,课程成绩
Course coinputdata(Course);
// 初始化学生链表
struct Lstudent* initStudentList();
// 向学生链表添加学生
struct Lstudent* addStudentToList(struct Lstudent*);
// 打印学生链表信息
void printStudentList(struct Lstudent*);
// 向学生课程链表添加课程
CourseListNode* addCourseToListForStudent(CourseListNode**);
// 打印学生课程信息
void printStudentCourses(CourseListNode*);
// 根据学号查找学生
struct Lstudent* findStudentByXuehao(struct Lstudent*, int);
void save(struct Lstudent* head, const char* filename);
void load(struct Lstudent** head, const char* filename);
void freeStudentList(struct Lstudent* head) ;
void printStudentListAndCourses(struct Lstudent* head);
void cover(struct Lstudent* head, const char* filename);
void KGPA(struct Lstudent* head);
void ZGPA(struct Lstudent* head);
struct Lstudent* findStudentByName(struct Lstudent* studentsHead, const char* targetName);
int countStudents(struct Lstudent* studentsHead);
int countCoursesForStudent(struct Lstudent* student);
Keyan putdata(Keyan Keyannode);
void printStudentKeyan(KeyanListNode* khead);
struct Lstudent* deleteStudentByXuehao(struct Lstudent* head, int targetXuehao);
CourseListNode* deleteCourseOfStudentByDaihao(struct Lstudent* studentNode, int targetDaihao) ;
CourseListNode* deleteCourseOfStudentByName(struct Lstudent* studentNode, char * targetName);
struct CourseListNode* clearStudentCourses(struct Lstudent* targetStudent);
struct Lstudent* clearStudentList(struct Lstudent* head,const char* filename);
#endif // STUDENT_COURSE_LIST_H
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"source_hello.h"
#include"../souce_add/source_add.h"
#include"../source_paixu/source_paixu.h"
struct zhanghaomima tea[50];
void deletecoursemenu(){
system("cls");
printf("---------------【删除指定学生课程信息】---------------\n\n");
printf("\t\t0.返回\n");
printf("\t\t1.按照课程编号删除\n");
printf("\t\t2.按照课程名称删除\n");
printf("----------------------------------------------\n");
printf("请选择(请输入0-2):");
}
void deletecoursekey(){
int userKey;
struct Lstudent* head=NULL;
load(&head,"信息.txt");
struct Lstudent* targetStudent=NULL;
int targetXuehao;
int targetdaihao;
char continueInput = 'y';
char continueCourseInput='y';
char continuekeyaninput='y';
// 使用循环处理用户输入,直到输入有效整数为止
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
while(1)
{
system("cls");
deletemenu();
deletekey();
system("pause");
}
break;
case 1:
system("cls");
continueInput='y';
char continueCourseInput = 'y';
while(continueInput == 'y' || continueInput == 'Y'){
printf("请输入目标学号:");
scanf("%d",&targetXuehao);
targetStudent = findStudentByXuehao(head, targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
printf("是否继续输入?(y/n): ");
scanf(" %c", &continueInput);}
else
break;}
if(targetStudent!=NULL){
while (continueCourseInput == 'y' || continueCourseInput == 'Y'){
printf("请输入要删除的课程编号:");
scanf("%d",&targetdaihao);
deleteCourseOfStudentByDaihao(targetStudent,targetdaihao);
printf("是否继续删除?(y/n)");
scanf(" %c",&continueCourseInput);
}}
KGPA(head);
ZGPA(head);
head=sort_student_list_by_xuehao(head);
cover(head,"信息.txt");
break;
case 2:
system("cls");
continueInput='y';
continueCourseInput = 'y';
char targetname[50];
while(continueInput == 'y' || continueInput == 'Y'){
printf("请输入目标学号:");
scanf("%d",&targetXuehao);
targetStudent = findStudentByXuehao(head, targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
printf("是否继续输入?(y/n): ");
scanf(" %c", &continueInput);}
else
break;}
if(targetStudent!=NULL){
while (continueCourseInput == 'y' || continueCourseInput == 'Y'){
printf("请输入要删除的课程名称:");
scanf("%s",&targetname);
deleteCourseOfStudentByName(targetStudent,targetname);
printf("是否继续删除?(y/n)");
scanf(" %c",&continueCourseInput);
}}
KGPA(head);
ZGPA(head);
head=sort_student_list_by_xuehao(head);
cover(head,"信息.txt");
break;
}
}
void deletemenu(){
system("cls");
printf("---------------【删除信息】---------------\n\n");
printf("\t\t0.返回教师端\n");
printf("\t\t1.删除指定学生信息\n");
printf("\t\t2.删除学生指定课程信息\n");
printf("\t\t3.删除指定学生所有课程信息\n");
printf("\t\t4.删除科研竞赛信息\n");
printf("\t\t5.清空系统\n");
printf("----------------------------------------------\n");
printf("请选择(请输入0-3):");
}
void deletekey(){
int userKey;
struct Lstudent* head=NULL;
load(&head,"信息.txt");
struct Lstudent* targetStudent=NULL;
int targetXuehao;
int targetdaihao;
char continueInput = 'y';
char continueCourseInput='y';
char continuekeyaninput='y';
// 使用循环处理用户输入,直到输入有效整数为止
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
while(1)
{
system("cls");
teaMenu();
teakey();
system("pause");
}
break;
case 1:
system("cls");
printf("请删除\n");
char continueInput = 'y';
while (continueInput == 'y' || continueInput == 'Y') {
printf("请输入目标学号:");
scanf("%d",&targetXuehao);
targetStudent = findStudentByXuehao(head, targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
}else{
deleteStudentByXuehao(head,targetXuehao);
printf("已删除%d的学生信息。\n",targetXuehao);}
printf("是否继续删除学生信息?(y/n): ");
scanf(" %c", &continueInput);
}
KGPA(head);
ZGPA(head);
cover(head,"信息.txt");
break;
case 2:
while(1){
deletecoursemenu();
deletecoursekey();
system("cls");
}
break;
case 3:
system("cls");
continueInput = 'y';
while (continueInput == 'y' || continueInput == 'Y') {
printf("请输入目标学号:");
scanf("%d",&targetXuehao);
targetStudent = findStudentByXuehao(head, targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
}else{
if(targetStudent->chead==NULL){
printf("%d无课程信息\n");
}else{
clearStudentCourses(targetStudent);
printf("已删除%d的所有课程信息。\n",targetXuehao);
}
}
printf("是否继续删除?(y/n): ");
scanf(" %c", &continueInput);
}
KGPA(head);
ZGPA(head);
cover(head,"信息.txt");
break;
case 4:
break;
case 5:
system("cls");
char queren[50];
printf("您将要清空系统,确认吗?(输入确认将清空,否则退出!)\n");
scanf("%s",&queren);
if(strcmp(queren,"确认")==0){
clearStudentList(head,"信息.txt");
printf("已清空系统\n");
}
system("pause");
break;
default:
break;
}
}
void addmenu(){
system("cls");
printf("---------------【添加信息】---------------\n\n");
printf("\t\t0.返回教师端\n");
printf("\t\t1.添加学生信息\n");
printf("\t\t2.添加课程信息\n");
printf("\t\t3.添加科研竞赛信息\n");
printf("----------------------------------------------\n");
printf("请选择(请输入0-3):");
}
void addkey(){
int userKey;
struct Lstudent* head=NULL;
load(&head,"信息.txt");
struct Lstudent* targetStudent=NULL;
int targetXuehao;
// 使用循环处理用户输入,直到输入有效整数为止
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
while(1)
{
system("cls");
teaMenu();
teakey();
system("pause");
}
break;
case 1:
system("cls");
printf("请录入");
char continueInput = 'y';
char continueCourseInput='y';
char continuekeyaninput='y';
while (continueInput == 'y' || continueInput == 'Y') {
head = addStudentToList(head); // 添加学生到链表
printf("是否继续输入学生信息?(y/n): ");
scanf(" %c", &continueInput); // 注意前面的空格用于跳过回车符
}
head=sort_student_list_by_xuehao(head);
KGPA(head);
ZGPA(head);
cover(head,"信息.txt");
break;
case 2:
system("cls");
printf("请录入");
printf("请输入目标学号:");
scanf("%d",&targetXuehao);
targetStudent = findStudentByXuehao(head, targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
} else {
char continuekeyaninput = 'y';
while (continuekeyaninput == 'y' || continuekeyaninput == 'Y') {
targetStudent->chead = addCourseToListForStudent(&targetStudent->chead);
printf("是否继续输入该学生的课程信息?(y/n): ");
scanf(" %c", &continuekeyaninput);
}
}
KGPA(head);
ZGPA(head);
cover(head,"信息.txt");
break;
case 3:
system("cls");
printf("请录入");
printf("请输入目标学号:");
scanf("%d",&targetXuehao);
targetStudent = findStudentByXuehao(head, targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
} else {
char continueCourseInput = 'y';
while (continueCourseInput == 'y' || continueCourseInput == 'Y') {
targetStudent->khead = addKeyan(&targetStudent->khead);
printf("是否继续输入该学生的科研竞赛信息?(y/n): ");
scanf(" %c", &continueCourseInput);
}
}
KGPA(head);
ZGPA(head);
cover(head,"信息.txt");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
void paixumenu(){
system("cls");
printf("---------------【排序信息】---------------\n\n");
printf("\t\t0.返回教师端\n");
printf("\t\t1.按照学号排序(默认)\n");
printf("\t\t2.按照总绩点排序\n");
printf("----------------------------------------------\n");
printf("请选择(请输入0-2):");
}
void paixuKey(){
int userKey;
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
struct Lstudent* head=NULL;
load(&head,"信息.txt");
KGPA(head);
ZGPA(head);
switch(userKey)
{
case 0:
while(1)
{
system("cls");
teaMenu();
teakey();
system("pause");
}
break;
case 1:
head=sort_student_list_by_xuehao(head);
system("cls");
printf("已按照学号排序\n");
cover(head,"信息.txt");
break;
case 2:
head=sort_student_list_by_kGPA(head);
system("cls");
printf("已按照总绩点排序\n");
cover(head,"信息.txt");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
void insertMenu(){
system("cls");
printf("---------------【查找信息】---------------\n\n");
printf("\t\t0.返回教师端\n");
printf("\t\t1.按照姓名查找\n");
printf("\t\t2.按照学号查找\n");
printf("----------------------------------------------\n");
printf("请选择(请输入0-2):");
}
void insertKey(){
int userKey;
int targetXuehao;
char targetName[50];
struct Lstudent* head=NULL;
load(&head,"信息.txt");
KGPA(head);
ZGPA(head);
struct Lstudent* targetStudent=NULL;
// 使用循环处理用户输入,直到输入有效整数为止
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
while(1)
{
system("cls");
teaMenu();
teakey();
system("pause");
}
break;
case 1:
system("cls");
printf("请输入目标学生姓名:");
scanf("%s", targetName);
targetStudent = findStudentByName(head, targetName); // 调用按姓名查找学生的函数
if (targetStudent == NULL) {
printf("未找到姓名为\"%s\"的学生。\n", targetName);
break;
} else {
printf("\n目标学生的信息如下:\n");
printtargetstudent(targetStudent);
int num=countCoursesForStudent(targetStudent);
printf("学生共%d门课程\n",num);
printStudentCourses(targetStudent->chead);
printStudentKeyan(targetStudent->khead);
}
break;
case 2:
system("cls");
printf("请输入目标学号:");
scanf("%d", &targetXuehao);
targetStudent=findStudentByXuehao(head,targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
break;
};
if (targetStudent != NULL) {
printf("\n目标学生的信息如下:\n");
printtargetstudent(targetStudent);
int num=countCoursesForStudent(targetStudent);
printf("学生共%d门课程\n",num);
printStudentCourses(targetStudent->chead);
printStudentKeyan(targetStudent->khead);
}
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
void insertMenu1(){
system("cls");
printf("---------------【查找信息】---------------\n\n");
printf("\t\t0.返回学生端\n");
printf("\t\t1.按照姓名查找\n");
printf("\t\t2.按照学号查找\n");
printf("----------------------------------------------\n");
printf("请选择(请输入0-2):");
}
void insertKey1(){
int userKey;
int targetXuehao;
char targetName[50];
struct Lstudent* head=NULL;
load(&head,"信息.txt");
KGPA(head);
ZGPA(head);
struct Lstudent* targetStudent=NULL;
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
while(1)
{
system("cls");
stuMenu();
stukey();
system("pause");
}
break;
case 1:
system("cls");
printf("请输入目标学生姓名:");
scanf("%s", targetName);
targetStudent = findStudentByName(head, targetName); // 调用按姓名查找学生的函数
if (targetStudent == NULL) {
printf("未找到姓名为\"%s\"的学生。\n", targetName);
break;
} else {
printf("\n目标学生的信息如下:\n");
printtargetstudent(targetStudent);
int num=countCoursesForStudent(targetStudent);
printf("学生共%d门课程\n",num);
printStudentCourses(targetStudent->chead);
printStudentKeyan(targetStudent->khead);
}
break;
case 2:
system("cls");
printf("请输入目标学号:");
scanf("%d", &targetXuehao);
targetStudent=findStudentByXuehao(head,targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
break;
};
if (targetStudent != NULL) {
printf("\n目标学生的信息如下:\n");
printtargetstudent(targetStudent);
int num=countCoursesForStudent(targetStudent);
printf("学生共%d门课程\n",num);
printStudentCourses(targetStudent->chead);
printStudentKeyan(targetStudent->khead);
}
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
void Menu()
{
system("cls");
printf("---------------【学生管理系统】---------------\n\n");
printf("\t\t0.退出系统\n");
printf("\t\t1.教师端\n");
printf("\t\t2.学生端\n");
printf("\t\t3.退出登录\n");
printf("----------------------------------------------\n");
printf("请选择身份(请输入0-3):");
}
void Key()
{
int userKey;
// 使用循环处理用户输入,直到输入有效整数为止
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
printf("\t\t【退出系统】\n");
system("pause");
exit(0);
break;
case 1:
while(1)
{
system("cls");
teaMenu();
teakey();
system("pause");
}
break;
case 2:
while(1)
{
system("cls");
stuMenu();
stukey();
system("pause");
}
break;
case 3:
while(1)
{
system("cls");
hello();
system("pause");
}
break;
default:
printf("输入错误!请重新输入:");
break;
}
}
void teaMenu()
{
printf("---------------【学生管理系统】---------------\n\n");
printf("------------------【教师端】------------------\n");
struct Lstudent* head=NULL;
load(&head,"信息.txt");
int totalStudents = countStudents(head);
printf("当前系统学生人数:%d\n\n", totalStudents);
printf("\t\t0.退出系统\n");
printf("\t\t1.录入信息\n");
printf("\t\t2.浏览信息\n");
printf("\t\t3.删除信息\n");
printf("\t\t4.修改信息\n");
printf("\t\t5.查找信息\n");
printf("\t\t6.排序成绩\n");
printf("\t\t7.返回主菜单\n");
printf("----------------------------------------------\n");
printf("请输入(0-7):");
}
void teakey()
{
int userKey;
struct Lstudent* head=NULL;
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
printf("\t\t【退出系统】\n");
system("pause");
exit(0);
break;
case 1:
while(1){
addmenu();
addkey();
system("pause");
}
break;
case 2:
system("cls");
printf("\t\t【浏览信息】\n");
head=NULL;
// 调用 load 函数加载数据
load(&head, "信息.txt");
KGPA(head);
ZGPA(head);
if (head != NULL) {
printStudentListAndCourses(head);
freeStudentList(head);
} else {
printf("没有读取到任何学生信息。\n");
}
return 0;
break;
case 3:
printf("\t\t【删除信息】\n");
while(1){
deletemenu();
deletekey();
system("cls");
}
break;
case 4:
printf("\t\t【修改信息】\n");
printf("请输入要修改的学生学号:\n");
break;
case 5:
printf("\t\t【查找信息】\n");
while(1)
{
insertMenu();
insertKey();
system("pause");
}
break;
case 6:
printf("\t\t【排序成绩】\n");
while(1){
system("cls");
paixumenu();
paixuKey();
system("pause");
}
break;
case 7:
while(1)
{
system("cls");
Menu();
Key();
system("pause");
}
break;
default:
printf("输入错误!请重新输入:");
break;
}
}
void stuMenu()
{
printf("---------------【学生管理系统】---------------\n\n");
printf("------------------【学生端】------------------\n\n");
printf("\t\t0.退出系统\n");
printf("\t\t1.查询成绩\n");
printf("\t\t2.返回主菜单\n");
printf("----------------------------------------------\n");
printf("请输入(0-3):");
}
void stukey()
{
int userKey;
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
printf("\t\t【退出系统】\n");
system("pause");
exit(0);
break;
case 1:
printf("\t\t【查找信息】\n");
while(1)
{
insertMenu1();
insertKey1();
system("pause");
}
break;
case 2:
while(1)
{
system("cls");
Menu();
Key();
system("pause");
}
break;
default:
printf("输入错误!请重新输入:");
break;
}
}
void hello()
{
int userKey;
printf("---------------【学生管理系统】---------------\n\n");
printf("-----------------【登录界面】-----------------\n\n");
printf("\t\t0.-退出系统-\n");
printf("\t\t1.-登录账号-\n");
printf("\t\t2.-注册账号-\n\n");
printf("-----------------【欢迎使用】-----------------\n\n");
printf("请输入(0-2):");
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch (userKey)
{
case 1:
denglu();//登录
break;
case 2:
zhuce();//注册
break;
case 0:
printf("\t\t【退出系统】\n");
exit(0);
system("pause");
break;
default:
printf("输入错误!请重新输入:\n");
break;
}
}
void zhuce()
{
char zhanghao[30];
char mima1[6],mima2[6];
int i, a = 0;
printf("\t\t【注册帐号】\n");
FILE *fp = fopen("账号信息档案.txt", "ab");
printf("请输入用户账号和密码(账号、密码可以为数字,中文字母,英文字母,符号,密码必须6位)\n\n");
printf("请输入账号:");
scanf("%s", zhanghao);
strcpy(tea[a].zhanghao, zhanghao);
printf("请输入密码:");
for (i = 0; i<6; i++)
{
mima1[i] = _getch();
printf("*");
tea[a].mima[i] = mima1[i];
if (mima1[i] == '\r')
{
printf("注册失败,请重新注册账号\n");
return;
}
}
printf("\n再次确认密码:");
for (i = 0; i<6; i++)
{
mima2[i] = _getch();
printf("*");
if (tea[a].mima[i] != mima2[i])
{
printf("\n密码不对,请重新注册账号");
return;
}
}
fprintf(fp, "账号\t密码\n");
fprintf(fp, "%s\t %s\n", tea[a].zhanghao, tea[a].mima);
printf("\n注册成功!!!\n");
fclose(fp);
system("pause");
system("cls");
}
void denglu()
{
char zhanghao[30];
char mima[20], mima1;
int i, j, a = 0;
int flag = 0;
system("cls");
printf("\t\t【登录账号】\n");
for (i = 0; i<3; i++)
{
printf("请输入帐号:");
scanf("%s", zhanghao);
printf("请输入密码:");
for (j = 0; j<6; j++)
{
mima1 = _getch();
printf("*");
mima[j] = mima1;
}
FILE *fp;
fp = fopen("账号信息档案.txt", "r");//读取文件操作
while (!feof(fp))
{
fscanf(fp, "%s %sn", tea[a].zhanghao, tea[a].mima);
if ((strcmp(zhanghao, tea[a].zhanghao) == 0) && (strcmp(mima, tea[a].mima) == 0))
{
flag = 1;
fclose(fp);
break;
}
a++;
}
if (flag == 1)
{
printf("\n登录成功!\n");
system("pause");
system("cls");
while (1)
{
Menu();
Key();
system("pause");
system("cls");
}
}
else
{
printf("\n账号或者密码输入错误,你还有%d次机会,请重新输入:\n", 2 - i);
}
}
if (i == 3)
{
printf("登录失败!\n");
return;
}
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"source_hello.h"
#include"../souce_add/source_add.h"
#include"../source_paixu/source_paixu.h"
struct zhanghaomima tea[50];
void deletecoursemenu(){
system("cls");
printf("---------------【删除指定学生课程信息】---------------\n\n");
printf("\t\t0.返回\n");
printf("\t\t1.按照课程编号删除\n");
printf("\t\t2.按照课程名称删除\n");
printf("----------------------------------------------\n");
printf("请选择(请输入0-2):");
}
void deletecoursekey(){
int userKey;
struct Lstudent* head=NULL;
load(&head,"信息.txt");
struct Lstudent* targetStudent=NULL;
int targetXuehao;
int targetdaihao;
char continueInput = 'y';
char continueCourseInput='y';
char continuekeyaninput='y';
// 使用循环处理用户输入,直到输入有效整数为止
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
while(1)
{
system("cls");
deletemenu();
deletekey();
system("pause");
}
break;
case 1:
system("cls");
continueInput='y';
char continueCourseInput = 'y';
while(continueInput == 'y' || continueInput == 'Y'){
printf("请输入目标学号:");
scanf("%d",&targetXuehao);
targetStudent = findStudentByXuehao(head, targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
printf("是否继续输入?(y/n): ");
scanf(" %c", &continueInput);}
else
break;}
if(targetStudent!=NULL){
while (continueCourseInput == 'y' || continueCourseInput == 'Y'){
printf("请输入要删除的课程编号:");
scanf("%d",&targetdaihao);
deleteCourseOfStudentByDaihao(targetStudent,targetdaihao);
printf("是否继续删除?(y/n)");
scanf(" %c",&continueCourseInput);
}}
KGPA(head);
ZGPA(head);
head=sort_student_list_by_xuehao(head);
cover(head,"信息.txt");
break;
case 2:
system("cls");
continueInput='y';
continueCourseInput = 'y';
char targetname[50];
while(continueInput == 'y' || continueInput == 'Y'){
printf("请输入目标学号:");
scanf("%d",&targetXuehao);
targetStudent = findStudentByXuehao(head, targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
printf("是否继续输入?(y/n): ");
scanf(" %c", &continueInput);}
else
break;}
if(targetStudent!=NULL){
while (continueCourseInput == 'y' || continueCourseInput == 'Y'){
printf("请输入要删除的课程名称:");
scanf("%s",&targetname);
deleteCourseOfStudentByName(targetStudent,targetname);
printf("是否继续删除?(y/n)");
scanf(" %c",&continueCourseInput);
}}
KGPA(head);
ZGPA(head);
head=sort_student_list_by_xuehao(head);
cover(head,"信息.txt");
break;
}
}
void deletemenu(){
system("cls");
printf("---------------【删除信息】---------------\n\n");
printf("\t\t0.返回教师端\n");
printf("\t\t1.删除指定学生信息\n");
printf("\t\t2.删除学生指定课程信息\n");
printf("\t\t3.删除指定学生所有课程信息\n");
printf("\t\t4.删除科研竞赛信息\n");
printf("\t\t5.清空系统\n");
printf("----------------------------------------------\n");
printf("请选择(请输入0-3):");
}
void deletekey(){
int userKey;
struct Lstudent* head=NULL;
load(&head,"信息.txt");
struct Lstudent* targetStudent=NULL;
int targetXuehao;
int targetdaihao;
char continueInput = 'y';
char continueCourseInput='y';
char continuekeyaninput='y';
// 使用循环处理用户输入,直到输入有效整数为止
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
while(1)
{
system("cls");
teaMenu();
teakey();
system("pause");
}
break;
case 1:
system("cls");
printf("请删除\n");
char continueInput = 'y';
while (continueInput == 'y' || continueInput == 'Y') {
printf("请输入目标学号:");
scanf("%d",&targetXuehao);
targetStudent = findStudentByXuehao(head, targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
}else{
deleteStudentByXuehao(head,targetXuehao);
printf("已删除%d的学生信息。\n",targetXuehao);}
printf("是否继续删除学生信息?(y/n): ");
scanf(" %c", &continueInput);
}
KGPA(head);
ZGPA(head);
cover(head,"信息.txt");
break;
case 2:
while(1){
deletecoursemenu();
deletecoursekey();
system("cls");
}
break;
case 3:
system("cls");
continueInput = 'y';
while (continueInput == 'y' || continueInput == 'Y') {
printf("请输入目标学号:");
scanf("%d",&targetXuehao);
targetStudent = findStudentByXuehao(head, targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
}else{
if(targetStudent->chead==NULL){
printf("%d无课程信息\n");
}else{
clearStudentCourses(targetStudent);
printf("已删除%d的所有课程信息。\n",targetXuehao);
}
}
printf("是否继续删除?(y/n): ");
scanf(" %c", &continueInput);
}
KGPA(head);
ZGPA(head);
cover(head,"信息.txt");
break;
case 4:
break;
case 5:
system("cls");
char queren[50];
printf("您将要清空系统,确认吗?(输入确认将清空,否则退出!)\n");
scanf("%s",&queren);
if(strcmp(queren,"确认")==0){
clearStudentList(head,"信息.txt");
printf("已清空系统\n");
}
system("pause");
break;
default:
break;
}
}
void addmenu(){
system("cls");
printf("---------------【添加信息】---------------\n\n");
printf("\t\t0.返回教师端\n");
printf("\t\t1.添加学生信息\n");
printf("\t\t2.添加课程信息\n");
printf("\t\t3.添加科研竞赛信息\n");
printf("----------------------------------------------\n");
printf("请选择(请输入0-3):");
}
void addkey(){
int userKey;
struct Lstudent* head=NULL;
load(&head,"信息.txt");
struct Lstudent* targetStudent=NULL;
int targetXuehao;
// 使用循环处理用户输入,直到输入有效整数为止
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
while(1)
{
system("cls");
teaMenu();
teakey();
system("pause");
}
break;
case 1:
system("cls");
printf("请录入");
char continueInput = 'y';
char continueCourseInput='y';
char continuekeyaninput='y';
while (continueInput == 'y' || continueInput == 'Y') {
head = addStudentToList(head); // 添加学生到链表
printf("是否继续输入学生信息?(y/n): ");
scanf(" %c", &continueInput); // 注意前面的空格用于跳过回车符
}
head=sort_student_list_by_xuehao(head);
KGPA(head);
ZGPA(head);
cover(head,"信息.txt");
break;
case 2:
system("cls");
printf("请录入");
printf("请输入目标学号:");
scanf("%d",&targetXuehao);
targetStudent = findStudentByXuehao(head, targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
} else {
char continuekeyaninput = 'y';
while (continuekeyaninput == 'y' || continuekeyaninput == 'Y') {
targetStudent->chead = addCourseToListForStudent(&targetStudent->chead);
printf("是否继续输入该学生的课程信息?(y/n): ");
scanf(" %c", &continuekeyaninput);
}
}
KGPA(head);
ZGPA(head);
cover(head,"信息.txt");
break;
case 3:
system("cls");
printf("请录入");
printf("请输入目标学号:");
scanf("%d",&targetXuehao);
targetStudent = findStudentByXuehao(head, targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
} else {
char continueCourseInput = 'y';
while (continueCourseInput == 'y' || continueCourseInput == 'Y') {
targetStudent->khead = addKeyan(&targetStudent->khead);
printf("是否继续输入该学生的科研竞赛信息?(y/n): ");
scanf(" %c", &continueCourseInput);
}
}
KGPA(head);
ZGPA(head);
cover(head,"信息.txt");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
void paixumenu(){
system("cls");
printf("---------------【排序信息】---------------\n\n");
printf("\t\t0.返回教师端\n");
printf("\t\t1.按照学号排序(默认)\n");
printf("\t\t2.按照总绩点排序\n");
printf("----------------------------------------------\n");
printf("请选择(请输入0-2):");
}
void paixuKey(){
int userKey;
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
struct Lstudent* head=NULL;
load(&head,"信息.txt");
KGPA(head);
ZGPA(head);
switch(userKey)
{
case 0:
while(1)
{
system("cls");
teaMenu();
teakey();
system("pause");
}
break;
case 1:
head=sort_student_list_by_xuehao(head);
system("cls");
printf("已按照学号排序\n");
cover(head,"信息.txt");
break;
case 2:
head=sort_student_list_by_kGPA(head);
system("cls");
printf("已按照总绩点排序\n");
cover(head,"信息.txt");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
void insertMenu(){
system("cls");
printf("---------------【查找信息】---------------\n\n");
printf("\t\t0.返回教师端\n");
printf("\t\t1.按照姓名查找\n");
printf("\t\t2.按照学号查找\n");
printf("----------------------------------------------\n");
printf("请选择(请输入0-2):");
}
void insertKey(){
int userKey;
int targetXuehao;
char targetName[50];
struct Lstudent* head=NULL;
load(&head,"信息.txt");
KGPA(head);
ZGPA(head);
struct Lstudent* targetStudent=NULL;
// 使用循环处理用户输入,直到输入有效整数为止
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
while(1)
{
system("cls");
teaMenu();
teakey();
system("pause");
}
break;
case 1:
system("cls");
printf("请输入目标学生姓名:");
scanf("%s", targetName);
targetStudent = findStudentByName(head, targetName); // 调用按姓名查找学生的函数
if (targetStudent == NULL) {
printf("未找到姓名为\"%s\"的学生。\n", targetName);
break;
} else {
printf("\n目标学生的信息如下:\n");
printtargetstudent(targetStudent);
int num=countCoursesForStudent(targetStudent);
printf("学生共%d门课程\n",num);
printStudentCourses(targetStudent->chead);
printStudentKeyan(targetStudent->khead);
}
break;
case 2:
system("cls");
printf("请输入目标学号:");
scanf("%d", &targetXuehao);
targetStudent=findStudentByXuehao(head,targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
break;
};
if (targetStudent != NULL) {
printf("\n目标学生的信息如下:\n");
printtargetstudent(targetStudent);
int num=countCoursesForStudent(targetStudent);
printf("学生共%d门课程\n",num);
printStudentCourses(targetStudent->chead);
printStudentKeyan(targetStudent->khead);
}
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
void insertMenu1(){
system("cls");
printf("---------------【查找信息】---------------\n\n");
printf("\t\t0.返回学生端\n");
printf("\t\t1.按照姓名查找\n");
printf("\t\t2.按照学号查找\n");
printf("----------------------------------------------\n");
printf("请选择(请输入0-2):");
}
void insertKey1(){
int userKey;
int targetXuehao;
char targetName[50];
struct Lstudent* head=NULL;
load(&head,"信息.txt");
KGPA(head);
ZGPA(head);
struct Lstudent* targetStudent=NULL;
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
while(1)
{
system("cls");
stuMenu();
stukey();
system("pause");
}
break;
case 1:
system("cls");
printf("请输入目标学生姓名:");
scanf("%s", targetName);
targetStudent = findStudentByName(head, targetName); // 调用按姓名查找学生的函数
if (targetStudent == NULL) {
printf("未找到姓名为\"%s\"的学生。\n", targetName);
break;
} else {
printf("\n目标学生的信息如下:\n");
printtargetstudent(targetStudent);
int num=countCoursesForStudent(targetStudent);
printf("学生共%d门课程\n",num);
printStudentCourses(targetStudent->chead);
printStudentKeyan(targetStudent->khead);
}
break;
case 2:
system("cls");
printf("请输入目标学号:");
scanf("%d", &targetXuehao);
targetStudent=findStudentByXuehao(head,targetXuehao);
if (targetStudent == NULL) {
printf("未找到学号为%d的学生。\n", targetXuehao);
break;
};
if (targetStudent != NULL) {
printf("\n目标学生的信息如下:\n");
printtargetstudent(targetStudent);
int num=countCoursesForStudent(targetStudent);
printf("学生共%d门课程\n",num);
printStudentCourses(targetStudent->chead);
printStudentKeyan(targetStudent->khead);
}
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
void Menu()
{
system("cls");
printf("---------------【学生管理系统】---------------\n\n");
printf("\t\t0.退出系统\n");
printf("\t\t1.教师端\n");
printf("\t\t2.学生端\n");
printf("\t\t3.退出登录\n");
printf("----------------------------------------------\n");
printf("请选择身份(请输入0-3):");
}
void Key()
{
int userKey;
// 使用循环处理用户输入,直到输入有效整数为止
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
printf("\t\t【退出系统】\n");
system("pause");
exit(0);
break;
case 1:
while(1)
{
system("cls");
teaMenu();
teakey();
system("pause");
}
break;
case 2:
while(1)
{
system("cls");
stuMenu();
stukey();
system("pause");
}
break;
case 3:
while(1)
{
system("cls");
hello();
system("pause");
}
break;
default:
printf("输入错误!请重新输入:");
break;
}
}
void teaMenu()
{
printf("---------------【学生管理系统】---------------\n\n");
printf("------------------【教师端】------------------\n");
struct Lstudent* head=NULL;
load(&head,"信息.txt");
int totalStudents = countStudents(head);
printf("当前系统学生人数:%d\n\n", totalStudents);
printf("\t\t0.退出系统\n");
printf("\t\t1.录入信息\n");
printf("\t\t2.浏览信息\n");
printf("\t\t3.删除信息\n");
printf("\t\t4.修改信息\n");
printf("\t\t5.查找信息\n");
printf("\t\t6.排序成绩\n");
printf("\t\t7.返回主菜单\n");
printf("----------------------------------------------\n");
printf("请输入(0-7):");
}
void teakey()
{
int userKey;
struct Lstudent* head=NULL;
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
printf("\t\t【退出系统】\n");
system("pause");
exit(0);
break;
case 1:
while(1){
addmenu();
addkey();
system("pause");
}
break;
case 2:
system("cls");
printf("\t\t【浏览信息】\n");
head=NULL;
// 调用 load 函数加载数据
load(&head, "信息.txt");
KGPA(head);
ZGPA(head);
if (head != NULL) {
printStudentListAndCourses(head);
freeStudentList(head);
} else {
printf("没有读取到任何学生信息。\n");
}
return 0;
break;
case 3:
printf("\t\t【删除信息】\n");
while(1){
deletemenu();
deletekey();
system("cls");
}
break;
case 4:
printf("\t\t【修改信息】\n");
printf("请输入要修改的学生学号:\n");
break;
case 5:
printf("\t\t【查找信息】\n");
while(1)
{
insertMenu();
insertKey();
system("pause");
}
break;
case 6:
printf("\t\t【排序成绩】\n");
while(1){
system("cls");
paixumenu();
paixuKey();
system("pause");
}
break;
case 7:
while(1)
{
system("cls");
Menu();
Key();
system("pause");
}
break;
default:
printf("输入错误!请重新输入:");
break;
}
}
void stuMenu()
{
printf("---------------【学生管理系统】---------------\n\n");
printf("------------------【学生端】------------------\n\n");
printf("\t\t0.退出系统\n");
printf("\t\t1.查询成绩\n");
printf("\t\t2.返回主菜单\n");
printf("----------------------------------------------\n");
printf("请输入(0-3):");
}
void stukey()
{
int userKey;
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch(userKey)
{
case 0:
printf("\t\t【退出系统】\n");
system("pause");
exit(0);
break;
case 1:
printf("\t\t【查找信息】\n");
while(1)
{
insertMenu1();
insertKey1();
system("pause");
}
break;
case 2:
while(1)
{
system("cls");
Menu();
Key();
system("pause");
}
break;
default:
printf("输入错误!请重新输入:");
break;
}
}
void hello()
{
int userKey;
printf("---------------【学生管理系统】---------------\n\n");
printf("-----------------【登录界面】-----------------\n\n");
printf("\t\t0.-退出系统-\n");
printf("\t\t1.-登录账号-\n");
printf("\t\t2.-注册账号-\n\n");
printf("-----------------【欢迎使用】-----------------\n\n");
printf("请输入(0-2):");
while (1) {
if (scanf("%d", &userKey) == 1) {
break; // 读取成功,跳出循环
}
// 清除输入缓冲区中的无效字符
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("输入错误!请重新输入整数:\n");
}
switch (userKey)
{
case 1:
denglu();//登录
break;
case 2:
zhuce();//注册
break;
case 0:
printf("\t\t【退出系统】\n");
exit(0);
system("pause");
break;
default:
printf("输入错误!请重新输入:\n");
break;
}
}
void zhuce()
{
char zhanghao[30];
char mima1[6],mima2[6];
int i, a = 0;
printf("\t\t【注册帐号】\n");
FILE *fp = fopen("账号信息档案.txt", "ab");
printf("请输入用户账号和密码(账号、密码可以为数字,中文字母,英文字母,符号,密码必须6位)\n\n");
printf("请输入账号:");
scanf("%s", zhanghao);
strcpy(tea[a].zhanghao, zhanghao);
printf("请输入密码:");
for (i = 0; i<6; i++)
{
mima1[i] = _getch();
printf("*");
tea[a].mima[i] = mima1[i];
if (mima1[i] == '\r')
{
printf("注册失败,请重新注册账号\n");
return;
}
}
printf("\n再次确认密码:");
for (i = 0; i<6; i++)
{
mima2[i] = _getch();
printf("*");
if (tea[a].mima[i] != mima2[i])
{
printf("\n密码不对,请重新注册账号");
return;
}
}
fprintf(fp, "账号\t密码\n");
fprintf(fp, "%s\t %s\n", tea[a].zhanghao, tea[a].mima);
printf("\n注册成功!!!\n");
fclose(fp);
system("pause");
system("cls");
}
void denglu()
{
char zhanghao[30];
char mima[20], mima1;
int i, j, a = 0;
int flag = 0;
system("cls");
printf("\t\t【登录账号】\n");
for (i = 0; i<3; i++)
{
printf("请输入帐号:");
scanf("%s", zhanghao);
printf("请输入密码:");
for (j = 0; j<6; j++)
{
mima1 = _getch();
printf("*");
mima[j] = mima1;
}
FILE *fp;
fp = fopen("账号信息档案.txt", "r");//读取文件操作
while (!feof(fp))
{
fscanf(fp, "%s %sn", tea[a].zhanghao, tea[a].mima);
if ((strcmp(zhanghao, tea[a].zhanghao) == 0) && (strcmp(mima, tea[a].mima) == 0))
{
flag = 1;
fclose(fp);
break;
}
a++;
}
if (flag == 1)
{
printf("\n登录成功!\n");
system("pause");
system("cls");
while (1)
{
Menu();
Key();
system("pause");
system("cls");
}
}
else
{
printf("\n账号或者密码输入错误,你还有%d次机会,请重新输入:\n", 2 - i);
}
}
if (i == 3)
{
printf("登录失败!\n");
return;
}
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"../source_hello/source_hello.h"
#include"../souce_add/source_add.h"
#include"source_paixu.h"
struct Lstudent* merge_sorted_lists(struct Lstudent* a, struct Lstudent* b) {
if (a == NULL) return b;
if (b == NULL) return a;
if (a->data.xuehao > b->data.xuehao) {
b->next = merge_sorted_lists(a, b->next);
return b;
} else {
a->next = merge_sorted_lists(a->next, b);
return a;
}
}
// 对链表进行原地排序
struct Lstudent* sort_student_list_by_xuehao(struct Lstudent* head) {
if (head == NULL || head->next == NULL) {
// 链表为空或只有一个元素,无需排序
return head;
}
struct Lstudent* firstHalf = head;
struct Lstudent* secondHalf = head->next;
struct Lstudent* slow = head;
struct Lstudent* fast = head->next;
// 分割链表为两部分
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
secondHalf = slow->next;
slow->next = NULL;
// 分别对前后两半链表进行递归排序
firstHalf = sort_student_list_by_xuehao(firstHalf);
secondHalf = sort_student_list_by_xuehao(secondHalf);
// 合并两个已排序的链表
head = merge_sorted_lists(firstHalf, secondHalf);
return head;
}
// 定义一个临时辅助函数,用于合并两个有序链表
struct Lstudent* merge_sorted_lists2(struct Lstudent* a, struct Lstudent* b) {
if (a == NULL) return b;
if (b == NULL) return a;
if (a->data.KGPA < b->data.KGPA) {
b->next = merge_sorted_lists2(a, b->next);
return b;
} else {
a->next = merge_sorted_lists2(a->next, b);
return a;
}
}
// 对链表进行原地排序
struct Lstudent* sort_student_list_by_kGPA(struct Lstudent* head) {
if (head == NULL || head->next == NULL) {
// 链表为空或只有一个元素,无需排序
return head;
}
struct Lstudent* firstHalf = head;
struct Lstudent* secondHalf = head->next;
struct Lstudent* slow = head;
struct Lstudent* fast = head->next;
// 分割链表为两部分
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
secondHalf = slow->next;
slow->next = NULL;
// 分别对前后两半链表进行递归排序
firstHalf = sort_student_list_by_kGPA(firstHalf);
secondHalf = sort_student_list_by_kGPA(secondHalf);
// 合并两个已排序的链表
head = merge_sorted_lists2(firstHalf, secondHalf);
return head;
}
#ifndef SOURCE_PAIXU_H_INCLUDED
#define SOURCE_PAIXU_H_INCLUDED
struct Lstudent* merge_sorted_lists(struct Lstudent* a, struct Lstudent* b);
struct Lstudent* sort_student_list_by_xuehao(struct Lstudent* head);
struct Lstudent* merge_sorted_lists2(struct Lstudent* a, struct Lstudent* b);
struct Lstudent* sort_student_list_by_ZGPA(struct Lstudent* head);
#endif // SOURCE_PAIXU_H_INCLUDED
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"../source_hello/source_hello.h"
#include"../souce_add/source_add.h"
int main(){
while(1){
hello();
system("pause");
system("cls");
}
return 0;
}