在 Python中使用 cout << 'Hello,world!' << endl

该博客主要展示了一段Python代码,通过自定义`ostream`类实现输出流功能。代码中导入`sys`模块,定义`ostream`类并实现`__lshift__`方法,还创建了`cout`和`cerr`对象用于标准输出和错误输出,最后进行了简单的输出测试。
部署运行你感兴趣的模型镜像

import sys

class ostream:
    def __init__(self,file):
        self.file = file

    def __lshift__(self,obj):
        self.file.write(str(obj));
        return self

cout = ostream(sys.stdout)
cerr = ostream(sys.stderr)
endl = '/n'


age = 23
name = 'albert'

cout << 'Hello,world!' << endl

cout << 'Name:' << name << endl << 'Age:' << age << endl

您可能感兴趣的与本文相关的镜像

Python3.11

Python3.11

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

#pragma once // 防止头文件重复包含 #include <iostream> // 包含输入输出流头文件 #include <vector> // 包含vector容器 #include <string> // 包含string类 using namespace std; // 使用标准命名空间 // 生鲜产品类 class FreshFood { public: string name; // 名称 double price; // 价格 int quantity; // 数量 string origin; // 产地 string date; // 生产日期 FreshFood(string n, double p, int q, string o, string d) : name(n), price(p), quantity(q), origin(o), date(d) {} }; // 生鲜信息管理类 class FreshfoodManager { private: vector<FreshFood> foodList; // 存储生鲜信息的容器 public: FreshfoodManager(); // 构造函数 void Show_Menu(); // 展示菜单 void AddFood(); // 增加生鲜信息 void DisplayFoods(); // 显示生鲜信息 void DeleteFood(); // 删除生鲜信息 void ModifyFood(); // 修改生鲜信息 void SearchFood(); // 查找生鲜信息 ~FreshfoodManager(); // 析构函数 }; // 构造函数实现 FreshfoodManager::FreshfoodManager() { // 可以在这里初始化一些默认数据 } // 显示菜单实现 void FreshfoodManager::Show_Menu() { cout << "****************************" << endl; cout << "*** 欢迎使用生鲜信息平台 ***" << endl; cout << "***** 0.退出管理程序 *******" << endl; cout << "***** 1.增加生鲜信息 *******" << endl; cout << "***** 2.显示生鲜信息 *******" << endl; cout << "***** 3.删除生鲜信息 *******" << endl; cout << "***** 4.修改生鲜信息 *******" << endl; cout << "***** 5.查找生鲜信息 *******" << endl; cout << "****************************" << endl; cout << "请输入您的选择(0-5): "; } // 添加生鲜信息实现 void FreshfoodManager::AddFood() { string name, origin, date; double price; int quantity; cout << "请输入生鲜名称: "; cin >> name; cout << "请输入价格: "; cin >> price; cout << "请输入数量: "; cin >> quantity; cout << "请输入产地: "; cin >> origin; cout << "请输入生产日期(YYYY-MM-DD): "; cin >> date; foodList.push_back(FreshFood(name, price, quantity, origin, date)); cout << "添加成功!" << endl; } // 显示所有生鲜信息实现 void FreshfoodManager::DisplayFoods() { if (foodList.empty()) { cout << "当前没有生鲜信息!" << endl; return; } cout << "========== 生鲜信息列表 ==========" << endl; for (size_t i = 0; i < foodList.size(); ++i) { cout << "编号: " << i + 1 << endl; cout << "名称: " << foodList[i].name << endl; cout << "价格: " << foodList[i].price << endl; cout << "数量: " << foodList[i].quantity << endl; cout << "产地: " << foodList[i].origin << endl; cout << "生产日期: " << foodList[i].date << endl; cout << "------------------------------" << endl; } } // 删除生鲜信息实现 void FreshfoodManager::DeleteFood() { DisplayFoods(); if (foodList.empty()) return; int index; cout << "请输入要删除的生鲜编号: "; cin >> index; if (index < 1 || index > static_cast<int>(foodList.size())) { cout << "无效的编号!" << endl; return; } foodList.erase(foodList.begin() + index - 1); cout << "删除成功!" << endl; } // 修改生鲜信息实现 void FreshfoodManager::ModifyFood() { DisplayFoods(); if (foodList.empty()) return; int index; cout << "请输入要修改的生鲜编号: "; cin >> index; if (index < 1 || index > static_cast<int>(foodList.size())) { cout << "无效的编号!" << endl; return; } FreshFood& food = foodList[index - 1]; cout << "请输入新的生鲜名称(当前: " << food.name << "): "; cin >> food.name; cout << "请输入新的价格(当前: " << food.price << "): "; cin >> food.price; cout << "请输入新的数量(当前: " << food.quantity << "): "; cin >> food.quantity; cout << "请输入新的产地(当前: " << food.origin << "): "; cin >> food.origin; cout << "请输入新的生产日期(当前: " << food.date << "): "; cin >> food.date; cout << "修改成功!" << endl; } // 查找生鲜信息实现 void FreshfoodManager::SearchFood() { if (foodList.empty()) { cout << "当前没有生鲜信息!" << endl; return; } string name; cout << "请输入要查找的生鲜名称: "; cin >> name; bool found = false; for (size_t i = 0; i < foodList.size(); ++i) { if (foodList[i].name == name) { cout << "========== 查找结果 ==========" << endl; cout << "编号: " << i + 1 << endl; cout << "名称: " << foodList[i].name << endl; cout << "价格: " << foodList[i].price << endl; cout << "数量: " << foodList[i].quantity << endl; cout << "产地: " << foodList[i].origin << endl; cout << "生产日期: " << foodList[i].date << endl; cout << "------------------------------" << endl; found = true; } } if (!found) { cout << "未找到名称为 \"" << name << "\" 的生鲜!" << endl; } } // 析构函数实现 FreshfoodManager::~FreshfoodManager() { // 可以在这里添加清理资源的代码 } // 主函数 int main() { FreshfoodManager manager; // 实例化管理器 int choice = 0; // 初始化choice while (true) { // 无限循环,直到用户选择退出 manager.Show_Menu(); cin >> choice; switch (choice) { case 0: cout << "感谢使用,再见!" << endl; return 0; // 直接退出程序(或使用 break 退出循环) case 1: manager.AddFood(); break; case 2: manager.DisplayFoods(); break; case 3: manager.DeleteFood(); break; case 4: manager.ModifyFood(); break; case 5: manager.SearchFood(); break; default: cout << "无效的选择,请重新输入!" << endl; break; } system("pause"); // 暂停,按任意键继续 system("cls"); // 清屏 } return 0; } 请帮我查重此代码,并判断此代码是否为AI所做
最新发布
07-03
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值