// 选择排序算法 for ( i = 0; i < num_records - 1; i++) { int max_index = i; for ( j = i + 1; j < num_records; j++) { if (sorted[j].score > sorted[max_index].score) max_index = j; } if (i != max_index) { struct Employee temp = sorted[i]; sorted[i] = sorted[max_index]; sorted[max_index] = temp; } } printf("员工编号\t员工姓名\t员工所属部门\t员工绩效分数\n"); // 循环打印排序后的记录 for ( i = 0; i < num_records; i++) { printf("%-8d\t%-10s\t%-12s\t%d\n", sorted[i].id, sorted[i].name, sorted[i].department, sorted[i].score); } }与之相同作用的代码还能怎么打
时间: 2024-02-14 15:09:12 浏览: 132
这段代码可以使用其他排序算法实现相同的功能,例如冒泡排序、插入排序等。下面是使用冒泡排序算法实现的代码示例:
```
for (i = 0; i < num_records - 1; i++) {
for (j = 0; j < num_records - i - 1; j++) {
if (sorted[j].score < sorted[j+1].score) {
struct Employee temp = sorted[j];
sorted[j] = sorted[j+1];
sorted[j+1] = temp;
}
}
}
printf("员工编号\t员工姓名\t员工所属部门\t员工绩效分数\n");
for (i = 0; i < num_records; i++) {
printf("%-8d\t%-10s\t%-12s\t%d\n", sorted[i].id, sorted[i].name, sorted[i].department, sorted[i].score);
}
```
这段代码使用冒泡排序算法,每次比较相邻的两个元素,如果前面的元素比后面的元素大,则交换它们的位置。这样一轮比较下来,最大的元素就会被交换到最后面,接着对剩余的元素进行同样的操作,直到整个数组都被排序完毕。最后,按照员工编号、姓名、所属部门、绩效分数的顺序打印出排序后的员工记录。
相关问题
设计并实现一个成绩分析系统,能够实现录入、保存一个班级学生多门课程的成绩、并对成绩进行分析等功能.具体要求如下: (1)定义一个菜单,方便用户实现下述操作.要求菜单筒沾、易操作、界而美观• (2)建立破盘文件input. dat,用于保存学生及其成绩信息.惮细内容参见表1M. (3)读取磁盘文件input. dat中的数据,并进行处理.要求实现如下功能: •按各门课程成绩排序,将排序的结果保存到磁盘文件中. •计算每人的平均成绩,按平均成绩排序,将排序的结果保存到破盘文件中. •能够统计各门课程的平均成绩、最高分、最低分、不及格人数、60〜69分人数、70〜 79分人数、80〜89分人数、90分以上人数. •根据学生姓名或学号查询其各门课成绩,需考虑重名情况的处理. 用C语言实现
好的,我理解了你的要求。下面是一个简单的成绩分析系统的C语言实现,根据你的要求进行了设计和开发。
首先是程序的主要代码部分:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_RECORDS 100
#define MAX_COURSES 5
typedef struct {
char name[MAX_NAME_LEN];
int id;
int scores[MAX_COURSES];
float avg_score;
} student_t;
typedef struct {
char name[MAX_NAME_LEN];
float avg_score;
float max_score;
float min_score;
int fail_num;
int sixty_num;
int seventy_num;
int eighty_num;
int ninety_num;
} course_t;
student_t records[MAX_RECORDS];
int record_count = 0;
course_t courses[MAX_COURSES];
int course_count = 0;
void display_menu();
void add_record();
void save_records();
void load_records();
void sort_scores();
void sort_avg_scores();
void analyze_scores();
void query_scores();
int main() {
load_records();
display_menu();
return 0;
}
void display_menu() {
int choice = 0;
do {
printf("\nPlease choose an option:\n");
printf("1. Add record\n");
printf("2. Save records\n");
printf("3. Load records\n");
printf("4. Sort scores\n");
printf("5. Sort average scores\n");
printf("6. Analyze scores\n");
printf("7. Query scores\n");
printf("8. Exit\n");
printf("Enter your choice (1-8): ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_record();
break;
case 2:
save_records();
break;
case 3:
load_records();
break;
case 4:
sort_scores();
break;
case 5:
sort_avg_scores();
break;
case 6:
analyze_scores();
break;
case 7:
query_scores();
break;
case 8:
printf("\nGoodbye!\n");
break;
default:
printf("\nInvalid choice. Please try again.\n");
break;
}
} while (choice != 8);
}
void add_record() {
if (record_count == MAX_RECORDS) {
printf("\nError: Maximum number of records reached.\n");
return;
}
student_t new_record;
printf("\nEnter student name: ");
scanf("%s", new_record.name);
printf("Enter student ID: ");
scanf("%d", &new_record.id);
for (int i = 0; i < course_count; i++) {
printf("Enter score for course %s: ", courses[i].name);
scanf("%d", &new_record.scores[i]);
}
records[record_count++] = new_record;
printf("\nRecord added successfully.\n");
}
void save_records() {
FILE *fp = fopen("input.dat", "wb");
if (fp == NULL) {
printf("\nError: Failed to open file.\n");
return;
}
fwrite(&record_count, sizeof(record_count), 1, fp);
fwrite(records, sizeof(student_t), record_count, fp);
fclose(fp);
printf("\nRecords saved successfully.\n");
}
void load_records() {
FILE *fp = fopen("input.dat", "rb");
if (fp == NULL) {
printf("\nError: Failed to open file.\n");
return;
}
fread(&record_count, sizeof(record_count), 1, fp);
fread(records, sizeof(student_t), record_count, fp);
fclose(fp);
printf("\nRecords loaded successfully.\n");
}
void sort_scores() {
int course_index = 0;
printf("\nEnter course index (0-%d): ", course_count - 1);
scanf("%d", &course_index);
if (course_index < 0 || course_index >= course_count) {
printf("\nError: Invalid course index.\n");
return;
}
for (int i = 0; i < record_count - 1; i++) {
for (int j = i + 1; j < record_count; j++) {
if (records[i].scores[course_index] < records[j].scores[course_index]) {
student_t temp = records[i];
records[i] = records[j];
records[j] = temp;
}
}
}
save_records();
printf("\nScores sorted by course %s.\n", courses[course_index].name);
}
void sort_avg_scores() {
for (int i = 0; i < record_count; i++) {
float total_score = 0;
for (int j = 0; j < course_count; j++) {
total_score += records[i].scores[j];
}
records[i].avg_score = total_score / course_count;
}
for (int i = 0; i < record_count - 1; i++) {
for (int j = i + 1; j < record_count; j++) {
if (records[i].avg_score < records[j].avg_score) {
student_t temp = records[i];
records[i] = records[j];
records[j] = temp;
}
}
}
save_records();
printf("\nRecords sorted by average score.\n");
}
void analyze_scores() {
for (int i = 0; i < course_count; i++) {
float total_score = 0;
float max_score = 0;
float min_score = 100;
int fail_num = 0;
int sixty_num = 0;
int seventy_num = 0;
int eighty_num = 0;
int ninety_num = 0;
for (int j = 0; j < record_count; j++) {
total_score += records[j].scores[i];
if (records[j].scores[i] > max_score) {
max_score = records[j].scores[i];
}
if (records[j].scores[i] < min_score) {
min_score = records[j].scores[i];
}
if (records[j].scores[i] < 60) {
fail_num++;
} else if (records[j].scores[i] >= 60 && records[j].scores[i] < 70) {
sixty_num++;
} else if (records[j].scores[i] >= 70 && records[j].scores[i] < 80) {
seventy_num++;
} else if (records[j].scores[i] >= 80 && records[j].scores[i] < 90) {
eighty_num++;
} else {
ninety_num++;
}
}
courses[i].avg_score = total_score / record_count;
courses[i].max_score = max_score;
courses[i].min_score = min_score;
courses[i].fail_num = fail_num;
courses[i].sixty_num = sixty_num;
courses[i].seventy_num = seventy_num;
courses[i].eighty_num = eighty_num;
courses[i].ninety_num = ninety_num;
}
printf("\nScore analysis completed.\n");
}
void query_scores() {
int choice = 0;
do {
printf("\nPlease choose a query option:\n");
printf("1. Query by name\n");
printf("2. Query by ID\n");
printf("3. Return to main menu\n");
printf("Enter your choice (1-3): ");
scanf("%d", &choice);
switch (choice) {
case 1: {
char name[MAX_NAME_LEN];
printf("\nEnter student name: ");
scanf("%s", name);
int found_count = 0;
for (int i = 0; i < record_count; i++) {
if (strcmp(records[i].name, name) == 0) {
found_count++;
printf("\nRecord found:\n");
printf("Name: %s\n", records[i].name);
printf("ID: %d\n", records[i].id);
for (int j = 0; j < course_count; j++) {
printf("Score for course %s: %d\n", courses[j].name, records[i].scores[j]);
}
}
}
if (found_count == 0) {
printf("\nNo record found.\n");
}
break;
}
case 2: {
int id = 0;
printf("\nEnter student ID: ");
scanf("%d", &id);
int found_count = 0;
for (int i = 0; i < record_count; i++) {
if (records[i].id == id) {
found_count++;
printf("\nRecord found:\n");
printf("Name: %s\n", records[i].name);
printf("ID: %d\n", records[i].id);
for (int j = 0; j < course_count; j++) {
printf("Score for course %s: %d\n", courses[j].name, records[i].scores[j]);
}
}
}
if (found_count == 0) {
printf("\nNo record found.\n");
}
break;
}
case 3:
printf("\nReturning to main menu.\n");
break;
default:
printf("\nInvalid choice. Please try again.\n");
break;
}
} while (choice != 3);
}
```
上述代码中,定义了两个结构体,student_t表示学生信息,course_t表示课程信息。同时定义了两个全局数组,records存储学生信息,courses存储课程信息。主函数中首先调用load_records函数从磁盘文件中读取记录,然后显示菜单,根据用户的选择执行相应的操作。
下面是各个操作的具体实现:
- add_record: 添加一条学生记录,包括姓名、学号和多门课程的成绩。
- save_records: 将所有学生记录保存到磁盘文件input.dat中。
- load_records: 从磁盘文件input.dat中读取所有学生记录。
- sort_scores: 按指定的课程成绩从高到低排序,然后将排序结果保存到磁盘文件中。
- sort_avg_scores: 计算每个学生的平均成绩,然后按平均成绩从高到低排序,最后将排序结果保存到磁盘文件中。
- analyze_scores: 统计各门课程的平均成绩、最高分、最低分、不及格人数、60~69分人数、70~79分人数、80~89分人数、90分以上人数。
- query_scores: 根据学生姓名或学号查询其各门课成绩,需考虑重名情况的处理。
程序中使用了文件读写操作、排序算法和简单的统计分析,可以满足你的要求。不过需要注意的是,由于本程序没有进行输入数据的验证,如果输入了错误的数据可能会导致程序崩溃或者输出错误的结果。
python自己找数据实现IC4.5算法,生成对应决策树。 要求 1、自己找数据,数据属性个数大于等于3,记录数大于等于20 2、python实现,代码需要保留注释 3、最后生成的决策树图
在Python中,我们可以使用`sklearn`库来实现集成学习中的ID3/C4.5算法,生成决策树。这里我们先通过`pandas`处理随机生成的数据,然后使用`DecisionTreeClassifier`类。由于实际数据查找通常涉及网络爬虫或文件读取,这里为了演示,我们将用`numpy`生成模拟数据。
首先,我们需要安装必要的库:
```bash
pip install numpy pandas scikit-learn matplotlib
```
下面是使用Python实现ID3/C4.5算法并生成决策树的一个示例(请注意,`sklearn`并未直接提供基于ID3/C4.5的决策树,而是使用更稳定的CART(Classification and Regression Trees)算法。因此,这里的示例将使用`CART`):
```python
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
import matplotlib.pyplot as plt
# 模拟生成数据
# 数据属性个数和记录数可以根据需求调整
num_features = 5 # 包含3个及以上特征
num_records = 50 # 至少20条记录
data = np.random.randint(2, size=(num_records, num_features))
labels = (np.sum(data, axis=1) >= 3).astype(int)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=42)
# 创建决策树模型
clf = DecisionTreeClassifier(criterion='entropy', max_depth=None)
clf.fit(X_train, y_train)
# 预测并评估性能(这里仅作演示,实际上可以忽略)
y_pred = clf.predict(X_test)
accuracy = clf.score(X_test, y_test)
print(f"Accuracy: {accuracy}")
# 可视化决策树
plt.figure(figsize=(20, 10))
tree.plot_tree(clf, filled=True, feature_names=data.columns, class_names=['Class 0', 'Class 1'])
plt.show()
阅读全文