Compare commits
5 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
70c8d40628 | 1 year ago |
|
|
9e5ed30bad | 1 year ago |
|
|
4ccd64c5d6 | 1 year ago |
|
|
e126828031 | 1 year ago |
|
|
33fb2e37a6 | 1 year ago |
@ -0,0 +1,46 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// 函数声明
|
||||
void displayMenu();
|
||||
|
||||
int main() {
|
||||
char input; // 用于存储用户输入的字符
|
||||
|
||||
while (1) {
|
||||
displayMenu(); // 显示菜单
|
||||
|
||||
printf("Please enter your choice: ");
|
||||
scanf(" %c", &input); // 读取用户输入
|
||||
|
||||
switch (input) {
|
||||
case 'i':
|
||||
printf("You are trying to Input info\n");
|
||||
break;
|
||||
case 'o':
|
||||
printf("You are trying to Output info\n");
|
||||
break;
|
||||
case 'm':
|
||||
printf("You are trying to Make things ordered\n");
|
||||
break;
|
||||
case 'q':
|
||||
printf("You are about to Quit\n");
|
||||
return 0; // 退出程序
|
||||
default:
|
||||
printf("Wrong input\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 菜单显示函数
|
||||
void displayMenu() {
|
||||
printf("\n");
|
||||
printf(" 1.Input\n");
|
||||
printf(" 2.Output\n");
|
||||
printf(" 3.Order\n");
|
||||
printf(" 4.Quit\n");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
@ -0,0 +1,117 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_STUDENTS 4 // 最多 4 个学生(原有 3 个 + 新插入 1 个)
|
||||
|
||||
typedef struct {
|
||||
int id; // 学号
|
||||
int clas; // 班级
|
||||
double math; // 高数成绩
|
||||
double physics; // 大学物理成绩
|
||||
double english; // 英语成绩
|
||||
double total; // 总成绩
|
||||
} Student;
|
||||
|
||||
// 函数声明
|
||||
void displayStudents(const Student students[], int studentCount);
|
||||
void sortStudents(Student students[], int studentCount);
|
||||
void insertStudent(Student students[], int *studentCount);
|
||||
|
||||
int main() {
|
||||
// 初始化已有的三个学生信息
|
||||
Student students[MAX_STUDENTS] = {
|
||||
{10001, 11, 85.0, 88.0, 92.0, 85.0 + 88.0 + 92.0},
|
||||
{10002, 12, 75.0, 65.0, 70.0, 75.0 + 65.0 + 70.0},
|
||||
{10003, 11, 95.0, 98.0, 90.0, 95.0 + 98.0 + 90.0}
|
||||
};
|
||||
int studentCount = 3; // 初始已有 3 个学生
|
||||
|
||||
printf("Original student information:\n");
|
||||
displayStudents(students, studentCount);
|
||||
|
||||
// 插入一个新学生信息
|
||||
insertStudent(students, &studentCount);
|
||||
printf("\nStudent information after insertion:\n");
|
||||
displayStudents(students, studentCount);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 显示学生信息
|
||||
void displayStudents(const Student students[], int studentCount) {
|
||||
int i; // 循环变量声明移到外部
|
||||
printf(" ID Class Math Physics English Total\n");
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
printf("%05d %-7d%-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].clas,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
}
|
||||
}
|
||||
|
||||
// 按班级和总成绩排序学生信息
|
||||
void sortStudents(Student students[], int studentCount) {
|
||||
int i, j; // 循环变量声明移到外部
|
||||
for (i = 0; i < studentCount - 1; i++) {
|
||||
for (j = 0; j < studentCount - i - 1; j++) {
|
||||
if (students[j].clas > students[j + 1].clas ||
|
||||
(students[j].clas == students[j + 1].clas && students[j].total < students[j + 1].total)) {
|
||||
// 交换学生信息
|
||||
Student temp = students[j];
|
||||
students[j] = students[j + 1];
|
||||
students[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 插入学生信息
|
||||
void insertStudent(Student students[], int *studentCount) {
|
||||
if (*studentCount >= MAX_STUDENTS) {
|
||||
printf("Student list is full. Cannot insert more students.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
Student newStudent;
|
||||
int i; // 循环变量声明移到外部
|
||||
|
||||
printf("\nEnter information for the new student:\n");
|
||||
|
||||
// 输入学号
|
||||
printf("ID: ");
|
||||
scanf("%d", &newStudent.id);
|
||||
|
||||
// 检查学号是否重复
|
||||
for (i = 0; i < *studentCount; i++) {
|
||||
if (students[i].id == newStudent.id) {
|
||||
printf("Student with ID %d already exists. Insertion failed.\n", newStudent.id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 输入班级和成绩
|
||||
printf("Class: ");
|
||||
scanf("%d", &newStudent.clas);
|
||||
printf("Math score: ");
|
||||
scanf("%lf", &newStudent.math);
|
||||
printf("Physics score: ");
|
||||
scanf("%lf", &newStudent.physics);
|
||||
printf("English score: ");
|
||||
scanf("%lf", &newStudent.english);
|
||||
|
||||
// 计算总成绩
|
||||
newStudent.total = newStudent.math + newStudent.physics + newStudent.english;
|
||||
|
||||
// 插入新学生
|
||||
students[*studentCount] = newStudent;
|
||||
(*studentCount)++;
|
||||
|
||||
// 重新排序
|
||||
sortStudents(students, *studentCount);
|
||||
|
||||
printf("New student inserted successfully.\n");
|
||||
}
|
||||
|
||||
@ -0,0 +1,91 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_STUDENTS 3 // 最多 3 个学生(初始已知)
|
||||
|
||||
typedef struct {
|
||||
int id; // 学号
|
||||
int clas; // 班级
|
||||
double math; // 高数成绩
|
||||
double physics; // 大学物理成绩
|
||||
double english; // 英语成绩
|
||||
double total; // 总成绩
|
||||
} Student;
|
||||
|
||||
// 函数声明
|
||||
void displayStudents(const Student students[], int studentCount);
|
||||
void deleteStudent(Student students[], int *studentCount);
|
||||
|
||||
int main() {
|
||||
// 初始化已知的三个学生信息
|
||||
Student students[MAX_STUDENTS] = {
|
||||
{10001, 11, 99.5, 88.5, 89.5, 99.5 + 88.5 + 89.5},
|
||||
{10003, 11, 92.5, 99.0, 60.5, 92.5 + 99.0 + 60.5},
|
||||
{10002, 12, 77.9, 56.5, 87.5, 77.9 + 56.5 + 87.5}
|
||||
};
|
||||
int studentCount = 3; // 当前学生数量
|
||||
|
||||
printf("Original student information:\n");
|
||||
displayStudents(students, studentCount);
|
||||
|
||||
// 删除一个学生信息
|
||||
deleteStudent(students, &studentCount);
|
||||
|
||||
printf("\nStudent information after deletion:\n");
|
||||
displayStudents(students, studentCount);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 显示学生信息
|
||||
void displayStudents(const Student students[], int studentCount) {
|
||||
int i; // 循环变量声明移到外部
|
||||
if (studentCount == 0) {
|
||||
printf("No student information available.\n");
|
||||
return;
|
||||
}
|
||||
printf(" ID Class Math Physics English Total\n");
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
printf("%05d %-7d%-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].clas,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除学生信息
|
||||
void deleteStudent(Student students[], int *studentCount) {
|
||||
int deleteId, i, j;
|
||||
int found = 0; // 标志是否找到学生信息
|
||||
|
||||
if (*studentCount == 0) {
|
||||
printf("No student information to delete.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("\nEnter the ID of the student to delete: ");
|
||||
scanf("%d", &deleteId);
|
||||
|
||||
// 查找学生信息
|
||||
for (i = 0; i < *studentCount; i++) {
|
||||
if (students[i].id == deleteId) {
|
||||
found = 1;
|
||||
|
||||
// 删除学生,将后续元素前移
|
||||
for (j = i; j < *studentCount - 1; j++) {
|
||||
students[j] = students[j + 1];
|
||||
}
|
||||
(*studentCount)--; // 学生数量减少
|
||||
printf("Student with ID %d has been deleted successfully.\n", deleteId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
printf("Student with ID %d does not exist.\n", deleteId);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue