之前在群里看见一张吊图
1 2 3 4 5 6 7 8 9 10 11
| while(working) { coffee.drink(); work.execute(); if(coffee == "empty") { if (CoffeePot == "empty") CoffeePot.brew(); coffee.refill(); } }
|
于是就趁着网课 摸了一个根据工作进度而减少咖啡量并自动补充的终端C++程序
环境
我的环境
开撸
根据代码片段可知
在工作中思考会去喝咖啡 然后工作进度+1% (整数或随机数都可以 后期在源码基础上修改即可)
1 2 3
| #include<iostream> #include<string>
|
首先要先知道是否在工作中
再写些虚拟容器
1 2 3 4 5 6
| class Container { public: virtual bool operator==(const char* string)const = 0; virtual inline unsigned int getRemain()const = 0; virtual ~Container() {} };
|
根据逻辑工作时思考回去喝咖啡🤔
所以出现了这一坨玩意
1 2 3 4 5 6 7 8 9 10
| class CoffeePot :public Container { private: unsigned int remain; public: CoffeePot() :remain{ 100 } {} void pour() { if (remain >= 5)remain -= 5; } void brew() { remain = 100; } bool operator==(const char* string)const { return((this->remain == 0 && string == "empty") ? true : false); } inline unsigned int getRemain() const { return remain; } }CoffeePot;
|
继续添加 关于coffee(液体)和工作 这两项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Coffee :public Container { private: unsigned int remain; public: Coffee() :remain{ 100 } {} void drink() { if (remain >= 10)remain -= 10; } void refill() { remain = 100; CoffeePot.pour(); } bool operator==(const char* string) const { return((this->remain == 0 && string == "empty") ? true : false); } inline unsigned int getRemain() const { return remain; } }coffee;
class work { private: unsigned int progress; public: work() :progress{ 0 } {} void execute() { progress++; working = ((progress != 100) ? true : false); } inline unsigned int getProgress() const { return progress; }
}work;
|
最后就是修饰一下图片上的代码片段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| int main() { while (working) { coffee.drink(); work.execute(); if (coffee == "empty") { if (CoffeePot == "empty") CoffeePot.brew(); coffee.refill(); }
std::cout << "work: \t" << work.getProgress() << "%" << std::endl; std::cout << "coffeepot: \t" << CoffeePot.getRemain() << "%" << std::endl; std::cout << "coffee: \t" << coffee.getRemain() << "%" << std::endl; } return 0; system("pause"); }
|