C++面向对象程序设计 030:你真的搞清楚为啥 while(cin >> n) 能成立了吗? ---- (北大Mooc)


专题博客链接

北大C++ POJ课后习题博客全解记录


原题题目

在这里插入图片描述

#include <iostream>
using namespace std;
class MyCin
{
// 在此处补充你的代码
};
int main()
{
    MyCin m;
    int n1,n2;
    while( m >> n1 >> n2) 
        cout  << n1 << " " << n2 << endl;
    return 0;
}

代码实现

private:
    int judge;
public:
    MyCin():judge(1){}
    MyCin& operator >>(int& n)
    {
        cin>>n;
        if(n == -1)
            judge = 0;
        return *this;
    }
    operator int(){return judge;}
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,让我来为您编写一个选修课程系统的完整代码: ```c++ #include <iostream> #include <string> #include <vector> using namespace std; // 课程类 class Course { public: Course(string name, int credit, string teacher) : name(name), credit(credit), teacher(teacher) {} string getName() { return name; } int getCredit() { return credit; } string getTeacher() { return teacher; } private: string name; int credit; string teacher; }; // 学生类 class Student { public: Student(string name, string id) : name(name), id(id) {} string getName() { return name; } string getId() { return id; } void addCourse(Course& course) { courses.push_back(course); } vector<Course> getCourses() { return courses; } private: string name; string id; vector<Course> courses; }; // 界面类 class GUI { public: void showMenu() { cout << "1. 添加学生" << endl; cout << "2. 添加课程" << endl; cout << "3. 选课" << endl; cout << "4. 查询选课情况" << endl; cout << "5. 退出" << endl; } void showPrompt(string prompt) { cout << prompt << endl; } Student getStudentInfo() { string name, id; cout << "请输入学生姓名:"; cin >> name; cout << "请输入学号:"; cin >> id; return Student(name, id); } Course getCourseInfo() { string name, teacher; int credit; cout << "请输入课程名称:"; cin >> name; cout << "请输入课程学分:"; cin >> credit; cout << "请输入任课教师:"; cin >> teacher; return Course(name, credit, teacher); } void showCourseList(vector<Course> courses) { cout << "课程列表:" << endl; for (int i = 0; i < courses.size(); i++) { Course course = courses[i]; cout << i + 1 << ". " << course.getName() << "(" << course.getCredit() << "学分,任课教师:" << course.getTeacher() << ")" << endl; } } }; // 程序类 class Program { public: void run() { GUI gui; vector<Student> students; vector<Course> courses; while (true) { gui.showMenu(); int choice; cin >> choice; if (choice == 1) { Student student = gui.getStudentInfo(); students.push_back(student); gui.showPrompt("添加成功!"); } else if (choice == 2) { Course course = gui.getCourseInfo(); courses.push_back(course); gui.showPrompt("添加成功!"); } else if (choice == 3) { gui.showCourseList(courses); int courseIndex, studentIndex; cout << "请选择课程编号:"; cin >> courseIndex; cout << "请选择学生编号:"; cin >> studentIndex; if (courseIndex <= 0 || courseIndex > courses.size() || studentIndex <= 0 || studentIndex > students.size()) { gui.showPrompt("输入有误!"); } else { Student& student = students[studentIndex - 1]; Course& course = courses[courseIndex - 1]; student.addCourse(course); gui.showPrompt("选课成功!"); } } else if (choice == 4) { int studentIndex; cout << "请选择学生编号:"; cin >> studentIndex; if (studentIndex <= 0 || studentIndex > students.size()) { gui.showPrompt("输入有误!"); } else { Student& student = students[studentIndex - 1]; vector<Course> courses = student.getCourses(); cout << student.getName() << "选修的课程有:" << endl; for (int i = 0; i < courses.size(); i++) { Course course = courses[i]; cout << i + 1 << ". " << course.getName() << endl; } } } else if (choice == 5) { break; } else { gui.showPrompt("输入有误!"); } } } }; int main() { Program program; program.run(); return 0; } ``` 这个程序使用了面向对象编程的技术,采用了C++语言开发,实现了一个选修课程系统,包括添加学生、添加课程、选课和查询选课情况等功能。界面使用了命令行方式,用户可以通过命令选择需要执行的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Love 6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值