AI上榜1,战绩1:1
2026-02-28 09:32:57
发布于:浙江
//Python.exe
//以下都是AI写的,本人不会写了...
#include <iostream>
#include <string>
#include <vector>
#include <random>
#include <ctime>
#include <limits>
// EasyX 图形库头文件(模拟 turtle)
#include <graphics.h>
using namespace std;
// ===================== 1. 模拟 Python 基础功能 =====================
// 模拟 Python input():带提示+类型检测
template <typename T>
T py_input(const string& prompt = "") {
if (!prompt.empty()) cout << prompt;
T val;
while (true) {
if (cin >> val) break;
// 输入错误处理
cout << "\n❌ 输入类型错误!请重新输入:";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return val;
}
// 模拟 Python print():支持多参数、换行控制
void py_print(const string& content, bool end = true) {
cout << content;
if (end) cout << endl;
}
// 模拟 Python list 基础操作
template <typename T>
class py_list {
private:
vector<T> data;
public:
// 添加元素(append)
void append(const T& val) { data.push_back(val); }
// 获取长度(len())
int len() const { return data.size(); }
// 索引访问
T& operator[](int idx) { return data[idx]; }
// 遍历输出
void print() {
cout << "[";
for (int i = 0; i < data.size(); i++) {
cout << data[i];
if (i != data.size() - 1) cout << ", ";
}
cout << "]" << endl;
}
};
// ===================== 2. 模拟 Python random 模块 =====================
class py_random {
private:
mt19937 rng; // 随机数生成器
public:
py_random() {
// 初始化随机种子(对应 Python random.seed())
rng.seed(time(0));
}
// 随机整数 [a, b](对应 random.randint(a,b))
int randint(int a, int b) {
uniform_int_distribution<int> dist(a, b);
return dist(rng);
}
// 随机浮点数 [0,1)(对应 random.random())
double random() {
uniform_real_distribution<double> dist(0.0, 1.0);
return dist(rng);
}
// 随机选择列表元素(对应 random.choice())
template <typename T>
T choice(const py_list<T>& lst) {
int idx = randint(0, lst.len() - 1);
return lst[idx];
}
};
// ===================== 3. 模拟 Python turtle 模块 =====================
class py_turtle {
private:
int x, y; // 画笔坐标
int angle; // 朝向(0=右,90=上,180=左,270=下)
COLORREF color; // 画笔颜色
int pen_width; // 画笔宽度
bool pen_down; // 是否落笔
public:
py_turtle() {
// 初始化画布(800x600)
initgraph(800, 600);
// 初始状态(对应 turtle 默认状态)
x = 400; y = 300; // 画布中心
angle = 0; // 初始朝右
color = BLACK; // 黑色画笔
pen_width = 2; // 画笔宽度
pen_down = true; // 默认落笔
}
// 前进(对应 turtle.forward())
void forward(int step) {
int new_x = x + step * cos(angle * PI / 180);
int new_y = y - step * sin(angle * PI / 180); // EasyX y 轴向下,所以减
if (pen_down) {
setlinecolor(color);
setlinestyle(PS_SOLID, pen_width);
line(x, y, new_x, new_y);
}
x = new_x;
y = new_y;
}
// 左转(对应 turtle.left())
void left(int deg) {
angle = (angle + deg) % 360;
}
// 右转(对应 turtle.right())
void right(int deg) {
angle = (angle - deg + 360) % 360;
}
// 设置画笔颜色(对应 turtle.pencolor())
void pencolor(COLORREF c) {
color = c;
}
// 提笔(对应 turtle.penup())
void penup() {
pen_down = false;
}
// 落笔(对应 turtle.pendown())
void pendown() {
pen_down = true;
}
// 等待关闭窗口(对应 turtle.done())
void done() {
getch();
closegraph();
}
};
// ===================== 主函数:演示所有功能 =====================
int main() {
// -------------------- 1. 基础功能演示 --------------------
py_print("===== Python 基础功能演示 =====");
// 输入输出
int num = py_input<int>("请输入一个整数:");
py_print("你输入的整数是:" + to_string(num));
// 模拟 list
py_list<string> lst;
lst.append("apple");
lst.append("banana");
lst.append("orange");
py_print("列表内容:");
lst.print();
py_print("列表长度:" + to_string(lst.len()));
// -------------------- 2. random 模块演示 --------------------
py_print("\n===== random 模块演示 =====");
py_random rand;
py_print("随机整数(1-100):" + to_string(rand.randint(1, 100)));
py_print("随机浮点数(0-1):" + to_string(rand.random()));
py_print("随机选择列表元素:" + rand.choice(lst));
// -------------------- 3. turtle 模块演示(画正方形+圆形) --------------------
py_print("\n===== turtle 模块演示(绘图窗口) =====");
py_turtle t;
// 画红色正方形
t.pencolor(RED);
for (int i = 0; i < 4; i++) {
t.forward(100);
t.right(90);
}
// 提笔移动,画蓝色圆形(简化版)
t.penup();
t.forward(150);
t.pendown();
t.pencolor(BLUE);
for (int i = 0; i < 360; i++) {
t.forward(1);
t.right(1);
}
t.done(); // 等待关闭绘图窗口
return 0;
}
全部评论 1
f






14小时前 来自 浙江
0这年头AI太太太杀菜了
13小时前 来自 浙江
0

















有帮助,赞一个