|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
要求在case5中对已按平均成绩排好序的4个班的同学的构造一个所有按平均成绩的以非递增方式排列的新的单链表,请修改代码
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <fstream>
#include <sstream>
#include <numeric>
using namespace std;
//定义学生结构体
struct student {
string name;
int id;
int score[5];//5个课程的成绩
double average;//平均成绩
};
//菜单
void showmenu() {
cout << "***** 成绩管理系统: *****" << endl;
cout << "***** 1.生成成绩 *****" << endl;
cout << "***** 2.计算平均成绩*****" << endl;
cout << "***** 3.冒泡排序 *****" << endl;
cout << "***** 4.选择排序 *****" << endl;
cout << "***** 5.构造链表 *****" << endl;
cout << "***** 6.退出系统 *****" << endl;
cout << "请输入选项: ";
}
void getname(student students[], int num)
{
std::vector<student> stu;
// Windows路径中的反斜杠需要转义,也可以使用单斜杠或原始字符串字面量
std::ifstream inputFile("D:\\c++学习\\students.txt");
if (!inputFile.is_open()) {
std::cerr << "无法打开文件" << std::endl;
}
// 逐行读取文件,并将学生姓名赋给结构体
for (int i = 0; i < num; ++i) {
std::string studentName;
if (std::getline(inputFile, studentName)) {
student students;
students.name = studentName;
stu.push_back(students);
}
else {
std::cerr << "文件中学生数量不足" << std::endl;
break;
}
}
// 关闭文件
inputFile.close();
}
//输出学号
void getid(student students[],int num)
{
int a = 2023217400;
for (int i = 0; i < num; i++)
{
students[i].id = a + i;
}
}
//随机输入成绩
void getscores(student students[], int num) {
srand(time(0));
for (int i = 0; i < num; i++)
{
for (int j = 0; j < 5; j++)
{
students[i].score[j] = rand() % 101; // 生成0~100的随机整数
}
}
}
// 计算平均成绩
void calculateaverage(student students[], int num) {
for (int i = 0; i < num; i++) {
int sum = 0;
for (int j = 0; j < 5; j++) {
sum += students[i].score[j];
}
students[i].average = sum / 5.0;
}
}
//平均成绩分到四个组中
void fun1(student students[], student students1[45], student students2[45], student students3[45], student students4[45], int num) {
for (int i = 0; i < num; i++) {
if (i < 45) {
students1[i].average = students[i].average;
students1[i].name = students[i].name;
students1[i].id = students[i].id;
}
else if (i >= 45 && i < 90) {
students2[i - 45].average = students[i].average;
students2[i - 45].name = students[i].name;
students2[i - 45].id = students[i].id;
}
else if (i >= 90&&i < 135) {
students3[i-90].average = students[i].average;
students3[i-90].name = students[i].name;
students3[i-90].id = students[i].id;
}
else {
students4[i-135].average = students[i].average;
students4[i-135].name = students[i].name;
students4[i-135].id = students[i].id;
}
}
}
//冒泡排序
void bubbleSort(student students[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (students[j].average < students[j + 1].average)
{
double temp = students[j].average;
students[j].average = students[j + 1].average;
students[j + 1].average = temp;
string tempname= students[j].name;
students[j].name = students[j + 1].name;
students[j + 1].name= tempname;
int tempid = students[j].id;
students[j].name = students[j + 1].id;
students[j + 1].id = tempid;
}
}
}
}
//选择排序
void selectionSort(student students[], int num) {
for (int i = 0; i < num - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < num; j++) {
if (students[j].average > students[maxIndex].average) {
maxIndex = j;
}
}
// 将最大值交换到当前位置
double temp = students[i].average;
students[i].average = students[maxIndex].average;
students[maxIndex].average = temp;
string tempname = students[i].name;
students[i].name = students[maxIndex].name;
students[maxIndex].name = tempname;
int tempid = students[i].id;
students[i].id = students[maxIndex].id;
students[maxIndex].id= tempid;
}
}
//单链表
template<typename T>
struct Node {
T data;
Node* next;
Node(T value) : data(value), next(nullptr) {}
};
template<typename T>
class LinkedList {
private:
Node<T>* head;
Node<T>* tail;
public:
LinkedList() : head(nullptr), tail(nullptr) {}
~LinkedList() {
while (head != nullptr) {
Node<T>* temp = head;
head = head->next;
delete temp;
}
}
void insert(T value) {
Node<T>* newNode = new Node<T>(value);
if (head == nullptr) {
head = newNode;
tail = newNode;
}
else {
tail->next = newNode;
tail = newNode;
}
}
void display() const {
Node<T>* current = head;
while (current != nullptr) {
std::cout << current->data << " -> ";
current = current->next;
}
std::cout << "nullptr" << std::endl;
}
};
void swap(double &a, double &b)
{
if (a > b)
{
double temp = a;
a =b;
b = temp;
}
}
LinkedList<int> list;
void makelist(student students[])
{
double arr1[] = { 0 };
double arr2[] = { 0 };
double arr3[] = { 0 };
double arr4[] = { 0 };
double* begin1 = arr1;
double* end1 = arr1 + sizeof(arr1) / sizeof(arr1[0]);
double a = accumulate(begin1, end1, 0);
double* begin2 = arr2;
double* end2 = arr2 + sizeof(arr2) / sizeof(arr2[0]);
double b = accumulate(begin2, end2, 0);
double* begin3 = arr3;
double* end3 = arr3 + sizeof(arr3) / sizeof(arr3[0]);
double c = accumulate(begin3, end3, 0);
double* begin4 = arr4;
double* end4 = arr4 + sizeof(arr4) / sizeof(arr4[0]);
double d = accumulate(begin4, end4, 0);
swap(a, b);
swap(b, c);
swap(c, d);
swap(a, b);
swap(b, c);
list.insert(a);
list.insert(b);
list.insert(c);
list.insert(d);
list.display();
}
const int class_num = 4;//班级数量
const int student_num = 45;//每班学生数量
const int nums = class_num * student_num;//学生总数量
int main() {
student students1[student_num];
student students2[student_num];
student students3[student_num];
student students4[student_num];
srand(time(nullptr));
student students[nums];//定义数组
while (true) {
showmenu();
int choice;
cin >> choice;
switch (choice) {
case 1://生成成绩
getname(students, nums);
getid(students, nums);
getscores(students, nums);
for (int i = 0; i < nums; i++) {
cout << students[i].name << " "<<students[i].id<<"的成绩: ";
for (int j = 0; j < 5; j++) {
cout << students[i].score[j] << " ";
}
cout << endl;
}
cout << "成绩已成功生成" << endl;
system("pause");//清屏操作
system("cls");
break;
case 2://计算平均成绩
calculateaverage(students, nums);
fun1(students, students1, students2, students3, students4, nums);
cout << "已成功计算平均成绩,并分到四个班中" << endl;
system("pause");//清屏操作
system("cls");
break;
case 3://冒泡排序
// 对每个班级内的成绩进行排序
bubbleSort(students1, 45);
bubbleSort(students2, 45);
bubbleSort(students3, 45);
bubbleSort(students4, 45);
// 输出排序后的结果
cout << "排序后的结果:" << std::endl;
// 输出班级1
cout << "班级1排序后的结果:" << std::endl;
for (int i = 0; i < 45; i++) {
cout << students1[i].name <<" " <<students1[i].id<< "的成绩: ";
cout << students1[i].name << endl;
}
cout << std::endl;
// 输出班级2
std::cout << "数组2排序后的结果:" << std::endl;
for (int i = 0; i < 45; i++) {
cout << students2[i].name << " " << students2[i].id << "的成绩: ";
cout << students2[i].name << endl;
}
cout << std::endl;
// 输出班级3
std::cout << "数组3排序后的结果:" << std::endl;
for (int i = 0; i < 45; i++) {
cout << students3[i].name << " " << students3[i].id << "的成绩: ";
cout << students3[i].name << endl;
}
cout <<endl;
// 输出班级4
std::cout << "数组4排序后的结果:" << std::endl;
for (int i = 0; i < 45; i++) {
cout << students4[i].name << " " << students4[i].id << "的成绩: ";
cout << students4[i].name << endl;
}
std::cout << std::endl;
system("pause");//清屏操作
system("cls");
break;
case 4://选择排序
selectionSort(students1, 45);
selectionSort(students2, 45);
selectionSort(students3, 45);
selectionSort(students4, 45);
// 输出排序后的结果
std::cout << "排序后的结果:" << std::endl;
// 输出班级1
std::cout << "数组1排序后的结果:" << std::endl;
for (int i = 0; i < 45; i++) {
cout << students1[i].name << " " << students1[i].id << "的成绩: ";
cout << students1[i].name << endl;
}
std::cout << std::endl;
// 输出班级2
std::cout << "数组2排序后的结果:" << std::endl;
for (int i = 0; i < 45; i++) {
cout << students2[i].name << " " << students2[i].id << "的成绩: ";
cout << students2[i].name << endl;
}
std::cout << std::endl;
// 输出班级3
std::cout << "数组3排序后的结果:" << std::endl;
for (int i = 0; i < 45; i++) {
cout << students3[i].name << " " << students3[i].id << "的成绩: ";
cout << students3[i].name << endl;
}
std::cout << std::endl;
// 输出班级4
std::cout << "数组4排序后的结果:" << std::endl;
for (int i = 0; i < 45; i++) {
cout << students4[i].name << " " << students4[i].id << "的成绩: ";
cout << students4[i].name << endl;
}
cout << endl;
system("pause");//清屏操作
system("cls");
break;
case 5://链表
makelist(students);
system("pause");//清屏操作
system("cls");
break;
case 6://退出系统
cout << "你将退出系统" << endl;
system("pause");//清屏操作
return 0;
break;
default:
cout << "你输入了错误的选项" << endl;
system("pause");//清屏操作
system("cls");
break;
}
}
system("pause");
return 0;
} |
|