C语言求助,自己写不全,希望看看大佬的
(1)信息维护:要求:学生信息数据要以文件的形式保存,能实现学生信息数据的维护。此模块包括子模块有:增加学生信息、删除学生信息、修改学生信息。(2)信息查询:要求:查询时可实现按姓名查询、按学号查询
(3)成绩统计:要求:A输入任意的一个课程名(如数学)和一个分数段(如60--70),统计出在此分数段的学生情况。
(4)排序:能对用户指定的任意课程名,按成绩升序或降序排列学生数据并显示排序结果(使用表格的形式显示排序后的输出结果)(使用多种方法排序者,加分)
二、课程设计应完成的工作
1、整个系统均用C语言(C++)实现;
2、利用指针、结构体、链表来实现学生成绩的数据结构设计;
3、系统具有输入、显示、查询(查询时可实现按姓名查询、按学号查询)、删除、排序、插入,保存、读取基本功能;
4、系统的各个功能模块都用函数的形式来实现;
5、学生信息数据要以文件的形式保存,能实现学生信息数据的维护。此模块包括子模块有:增加学生信息、删除学生信息、修改学生信息;文件要求至少有十条记录;
6、可以输入任意的一个课程名(如数学)和一个分数段(如60--70),统计出在此分数段的学生情况;
7、能对用户指定的任意课程名,按成绩升序或降序排列学生数据并显示排序结果;
8、可以将学生信息从文件中读取出来。
9、至少采用文本菜单界面(如果能采用图形菜单界面更好)
10、写出课程设计报告。 基本的设计方案,使用C语言来构建一个学生信息和成绩管理系统。
该系统将涵盖列出的所有要求,包括信息维护、查询、成绩统计、排序和数据持久化。
系统设计
数据结构设计:学生结构 (Student): 包含姓名、学号、以及课程成绩(可以使用结构体中的数组或者动态分配的课程成绩)。
链表节点 (Node): 包含一个 Student 结构体和指向下一个 Node 的指针。
主要功能模块:
[*]增加学生信息 (addStudent): 向链表中添加新的学生记录。
[*]删除学生信息 (deleteStudent): 从链表中删除指定的学生记录。
[*]修改学生信息 (editStudent): 修改链表中现有的学生记录。
[*]按姓名查询 (searchByName): 在链表中查找特定姓名的学生。
[*]按学号查询 (searchByNumber): 在链表中查找特定学号的学生。
[*]成绩统计 (gradeStatistics): 统计指定课程中特定分数段的学生数量。
[*]排序 (sortStudents): 对学生在指定课程的成绩进行升序或降序排序。
[*]保存到文件 (saveToFile): 将学生数据保存到文件。
[*]从文件读取 (loadFromFile): 从文件中读取学生数据。
文件格式:使用文本文件保存数据,每条记录一行,字段之间用逗号隔开。
用户界面:使用命令行菜单来允许用户选择要执行的操作。
这里给出一些核心函数的伪代码/示例,:
定义学生结构体:
typedef struct {
char name;
char number;
int mathGrade;
int englishGrade;
// 更多课程可以添加在这里
} Student;
typedef struct Node {
Student data;
struct Node* next;
} Node;
添加学生信息:
void addStudent(Node** head, Student newStudent) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = newStudent;
newNode->next = *head;
*head = newNode;
}
保存到文件:
void saveToFile(Node* head, const char* filename) {
FILE* file = fopen(filename, "w");
Node* current = head;
while (current != NULL) {
fprintf(file, "%s,%s,%d,%d\n", current->data.name, current->data.number,
current->data.mathGrade, current->data.englishGrade);
current = current->next;
}
fclose(file);
}
从文件读取:
void loadFromFile(Node** head, const char* filename) {
FILE* file = fopen(filename, "r");
Student temp;
while (fscanf(file, "%49[^,],%19[^,],%d,%d\n", temp.name, temp.number, &temp.mathGrade, &temp.englishGrade) == 4) {
addStudent(head, temp);
}
fclose(file);
}
请鱼油去测试每个功能确保正确性。
然后询问 @FishC 更具体的代码实现 不二如是 发表于 2024-04-24 21:16
基本的设计方案,使用C语言来构建一个学生信息和成绩 ...
以下是一个使用C语言实现学生信息和成绩管理系统的示例代码。该代码实现了上述提到的主要功能模块,并提供了一个简单的命令行菜单来与用户交互。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name;
char number;
int mathGrade;
int englishGrade;
} Student;
typedef struct Node {
Student data;
struct Node* next;
} Node;
void addStudent(Node** head, Student newStudent) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = newStudent;
newNode->next = *head;
*head = newNode;
}
void deleteStudent(Node** head, char* number) {
Node* current = *head;
Node* previous = NULL;
while (current != NULL) {
if (strcmp(current->data.number, number) == 0) {
if (previous == NULL) {
*head = current->next;
} else {
previous->next = current->next;
}
free(current);
return;
}
previous = current;
current = current->next;
}
printf("Student with number %s not found.\n", number);
}
void editStudent(Node* head, char* number, Student newStudent) {
Node* current = head;
while (current != NULL) {
if (strcmp(current->data.number, number) == 0) {
current->data = newStudent;
return;
}
current = current->next;
}
printf("Student with number %s not found.\n", number);
}
void searchByName(Node* head, char* name) {
Node* current = head;
while (current != NULL) {
if (strcmp(current->data.name, name) == 0) {
printf("Name: %s\n", current->data.name);
printf("Number: %s\n", current->data.number);
printf("Math Grade: %d\n", current->data.mathGrade);
printf("English Grade: %d\n", current->data.englishGrade);
return;
}
current = current->next;
}
printf("Student with name %s not found.\n", name);
}
void searchByNumber(Node* head, char* number) {
Node* current = head;
while (current != NULL) {
if (strcmp(current->data.number, number) == 0) {
printf("Name: %s\n", current->data.name);
printf("Number: %s\n", current->data.number);
printf("Math Grade: %d\n", current->data.mathGrade);
printf("English Grade: %d\n", current->data.englishGrade);
return;
}
current = current->next;
}
printf("Student with number %s not found.\n", number);
}
int countStudentsInGradeRange(Node* head, int minGrade, int maxGrade) {
int count = 0;
Node* current = head;
while (current != NULL) {
if (current->data.mathGrade >= minGrade && current->data.mathGrade <= maxGrade) {
count++;
}
current = current->next;
}
return count;
}
void sortStudents(Node* head, int sortBy) {
int swapped;
Node* current = head;
Node* last = NULL;
if (head == NULL) {
return;
}
do {
swapped = 0;
current = head;
while (current->next != last) {
if (sortBy == 0 && strcmp(current->data.number, current->next->data.number) > 0) {
Student temp = current->data;
current->data = current->next->data;
current->next->data = temp;
swapped = 1;
} else if (sortBy == 1 && current->data.mathGrade > current->next->data.mathGrade) {
Student temp = current->data;
current->data = current->next->data;
current->next->data = temp;
swapped = 1;
} else if (sortBy == 2 && current->data.englishGrade > current->next->data.englishGrade) {
Student temp = current->data;
current->data = current->next->data;
current->next->data = temp;
swapped = 1;
}
current = current->next;
}
last = current;
} while (swapped);
}
void saveToFile(Node* head, const char* filename) {
FILE* file = fopen(filename, "w");
Node* current = head;
while (current != NULL) {
fprintf(file, "%s,%s,%d,%d\n", current->data.name, current->data.number,
current->data.mathGrade, current->data.englishGrade);
current = current->next;
}
fclose(file);
}
void loadFromFile(Node** head, const char* filename) {
FILE* file = fopen(filename, "r");
if (file) {
char line;
while (fgets(line, sizeof(line), file)) {
Student temp;
sscanf(line, "%[^,],%[^,],%d,%d", temp.name, temp.number,
&temp.mathGrade, &temp.englishGrade);
addStudent(head, temp);
}
fclose(file);
} else {
printf("Failed to open file.\n");
}
}
void printMenu() {
printf("\n-- Student Information and Grade Management System --\n");
printf("\nPlease select an option:\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Edit student\n");
printf("4. Search by name\n");
printf("5. Search by number\n");
printf("6. Grade statistics\n");
printf("7. Sort students\n");
printf("8. Save to file\n");
printf("9. Load from file\n");
printf("0. Exit\n");
printf("\nEnter your choice: ");
}
int main() {
Node* head = NULL;
int choice;
do {
printMenu();
scanf("%d", &choice);
switch (choice) {
case 1: {
Student newStudent;
printf("Enter name: ");
scanf("%s", newStudent.name);
printf("Enter number: ");
scanf("%s", newStudent.number);
printf("Enter math grade: ");
scanf("%d", &newStudent.mathGrade);
printf("Enter english grade: ");
scanf("%d", &newStudent.englishGrade);
addStudent(&head, newStudent);
break;
}
case 2: {
char number;
printf("Enter student number to delete: ");
scanf("%s", number);
deleteStudent(&head, number);
break;
}
case 3: {
char number;
Student newStudent;
printf("Enter student number to edit: ");
scanf("%s", number);
printf("Enter new name: ");
scanf("%s", newStudent.name);
printf("Enter new number: ");
scanf("%s", newStudent.number);
printf("Enter new math grade: ");
scanf("%d", &newStudent.mathGrade);
printf("Enter new english grade: ");
scanf("%d", &newStudent.englishGrade);
editStudent(head, number, newStudent);
break;
}
case 4: {
char name;
printf("Enter name to search: ");
scanf("%s", name);
searchByName(head, name);
break;
}
case 5: {
char number;
printf("Enter number to search: ");
scanf("%s", number);
searchByNumber(head, number);
break;
}
case 6: {
int minGrade, maxGrade;
printf("Enter minimum grade: ");
scanf("%d", &minGrade);
printf("Enter maximum grade: ");
scanf("%d", &maxGrade);
int count = countStudentsInGradeRange(head, minGrade, maxGrade);
printf("Number of students in grade range: %d\n", count);
break;
}
case 7: {
int sortBy;
printf("Sort by:\n");
printf("0. Number\n");
printf("1. Math grade\n");
printf("2. English grade\n");
printf("Enter your choice: ");
scanf("%d", &sortBy);
sortStudents(head, sortBy);
printf("Students sorted.\n");
break;
}
case 8: {
char filename;
printf("Enter filename to save: ");
scanf("%s", filename);
saveToFile(head, filename);
printf("Data saved to file.\n");
break;
}
case 9: {
char filename;
printf("Enter filename to load: ");
scanf("%s", filename);
loadFromFile(&head, filename);
printf("Data loaded from file.\n");
break;
}
case 0:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
printf("\n");
} while (choice != 0);
return 0;
}
请注意,这只是一个简单的示例代码,可能还有一些边界情况没有处理。在实际开发中,您可能需要进一步完善和改进代码。同时,还可以根据自己的需求添加其他功能。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]