高中模拟测试卷成绩的照片

高中模拟测试卷成绩的照片

首页技巧更新时间:2025-03-07 16:14:22

在当今数字化时代,各类新奇有趣的小游戏层出不穷,今天就给大家带来一款别具一格的 C 编程小游戏 —— 高考理科成绩模拟生成游戏(完整可编译运行源码和代码解释在文章后面)。运行结果见截图。

想象一下,你只需在程序里输入学生的名字,甚至仅仅是一个数字,程序就能瞬间为其随机生成一份高考生的理科成绩。这里涵盖了语文、数学、英语、物理、化学、生物六门重要科目,每一门成绩都是随机诞生,充满了不确定性。

更让人兴奋的是,程序会自动将所有录入学生的成绩按照总分从高到低进行排序。一份清晰明了的成绩排行榜就此诞生,不仅能看到每个学生的总分,还能知晓每一门科目的具体分数。而且,排行榜会以表格的形式呈现,每一行成绩之间都有分隔线隔开,看起来十分直观。

在显示方面,这个程序也独具匠心。每个学生的数据颜色都不一样,同时巧妙地避开了黑色背景下难以看清成绩的颜色,让你能轻松区分不同学生的信息。

对于编程爱好者来说,这款游戏的代码在 Dev - C 环境下就能成功编译运行。代码结构清晰,逻辑严谨,你可以深入研究其中的随机数生成、排序算法等编程技巧,还能根据自己的想法对代码进行二次开发,比如增加更多科目、调整成绩生成规则等。

对于普通玩家而言,这就是一个充满乐趣的小游戏。你可以输入朋友、家人的名字,看看他们在这个虚拟的高考世界里成绩如何;也可以输入一连串数字,看看随机生成的学生们谁能成为 “高考状元”。

现在,就来试试这个有趣的高考理科成绩模拟生成游戏吧,感受编程带来的独特魅力和无限乐趣!

代码部分

```cpp

#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

#include <cstdlib>

#include <ctime>

#include <windows.h>

#include <iomanip>

// 定义学生结构体

struct Student {

std::string name;

int chinese;

int math;

int english;

int physics;

int chemistry;

int biology;

int totalScore;

int rank;

Student(const std::string& n) : name(n) {

chinese = rand() % 101;

math = rand() % 101;

english = rand() % 101;

physics = rand() % 101;

chemistry = rand() % 101;

biology = rand() % 101;

totalScore = chinese math english physics chemistry biology;

rank = 0;

}

};

// 比较函数,用于按总分从高到低排序

bool compareByTotalScore(const Student& a, const Student& b) {

return a.totalScore > b.totalScore;

}

// 设置控制台文本颜色

void setColor(int color) {

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute(hConsole, color);

}

// 显示所有学生的成绩排行榜(表格形式)

void showRanking(const std::vector<Student>& students) {

std::cout << "\n学生成绩排行榜(按总分从高到低):" << std::endl;

std::cout << " ------ ---------- -------- -------- -------- -------- -------- -------- -------- " << std::endl;

std::cout << "| 名次 | 姓名 | 语文 | 数学 | 英语 | 物理 | 化学 | 生物 | 总分 |" << std::endl;

std::cout << " ------ ---------- -------- -------- -------- -------- -------- -------- -------- " << std::endl;

int colors[] = {1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 14}; // 避免黑色及接近黑色的颜色

int colorIndex = 0;

for (const auto& student : students) {

setColor(colors[colorIndex % 12]);

std::cout << "| " << std::setw(4) << student.rank << " | "

<< std::setw(8) << student.name << " | "

<< std::setw(6) << student.chinese << " | "

<< std::setw(6) << student.math << " | "

<< std::setw(6) << student.english << " | "

<< std::setw(6) << student.physics << " | "

<< std::setw(6) << student.chemistry << " | "

<< std::setw(6) << student.biology << " | "

<< std::setw(6) << student.totalScore << " |" << std::endl;

std::cout << " ------ ---------- -------- -------- -------- -------- -------- -------- -------- " << std::endl;

colorIndex ;

}

setColor(7); // 恢复默认颜色

}

// 根据数字生成人名

std::string generateName(int num) {

return "学生" std::to_string(num);

}

// 计算排名

void calculateRanks(std::vector<Student>& students) {

std::sort(students.begin(), students.end(), compareByTotalScore);

int currentRank = 1;

for (size_t i = 0; i < students.size(); i) {

if (i > 0 && students[i].totalScore < students[i - 1].totalScore) {

currentRank = static_cast<int>(i 1);

}

students[i].rank = currentRank;

}

}

int main() {

std::srand(static_cast<unsigned int>(std::time(nullptr)));

std::vector<Student> students;

std::string input;

while (true) {

std::cout << "请输入学生的名字(输入 '结束' 停止录入),或输入数字自动生成人名: ";

std::cin >> input;

if (input == "结束") {

break;

}

try {

int num = std::stoi(input);

std::string name = generateName(num);

students.emplace_back(name);

} catch (const std::invalid_argument&) {

students.emplace_back(input);

}

calculateRanks(students);

showRanking(students);

}

return 0;

}

代码解释:
  1. Student 结构体:用于存储学生的各项信息,包括姓名、各科成绩(语文、数学、英语、物理、化学、生物)、总分和排名。构造函数会随机生成每门科目的成绩(范围 0 - 100),并计算总分,初始排名设为 0。
  2. compareByTotalScore 函数:作为 std::sort 的比较函数,按照学生的总分从高到低对学生进行排序。
  3. setColor 函数:使用 Windows API 中的 SetConsoleTextAttribute 函数来设置控制台文本的颜色。
  4. showRanking 函数:以表格形式输出学生成绩排行榜,表格包含名次、姓名、各科成绩和总分。为每个学生的输出设置不同的颜色,避免使用在白色背景下难以看清的颜色(如白色和灰色)。在每个学生成绩行下方添加分隔线,增强表格的可读性。最后恢复默认文本颜色。
  5. generateName 函数:根据输入的数字生成对应的人名,格式为 “学生 数字”。
  6. calculateRanks 函数:先对学生按总分进行排序,然后计算每个学生的排名,总分相同的学生排名相同。
  7. main 函数:初始化随机数种子,确保每次运行程序时生成的随机成绩不同。进入循环,提示用户输入学生名字或数字。如果输入 “结束”,则退出循环。若输入为数字,调用 generateName 函数生成人名并创建学生对象;若输入为字符串,则直接以该字符串作为人名创建学生对象。调用 calculateRanks 函数计算排名,调用 showRanking 函数显示排行榜。
注意事项:

原创声明:本文为原创内容,未经授权,禁止抄袭。文中所涉及的代码和创意均为独立创作,旨在为读者带来新颖、有趣的内容体验。



,
大家还看了
也许喜欢
更多栏目

© 1998-2024 shitiku.com.cn,All Rights Reserved.