Try   HackMD
tags: 作業-進階 大一程設 東華大學 東華大學資管系 基本程式概念 資管經驗分享

HW4-1 - 巧克力工廠參考作法

還有更快的解法,但這邊以老師上課,最純粹的概念來解這一題
Orange

#include <iostream> #include <ctime> #include <cstdlib> #include <vector> using namespace std; class blackSauce{ public: blackSauce(){ this->blackSaucePoint = 0; } double getBlackSauce(){ return this->blackSaucePoint; } void setBlackSauce(double p){ this->blackSaucePoint = p; } private: double blackSaucePoint; }; class whiteSauce{ public: whiteSauce(){ this->whiteSaucePoint = 0; } double getWhiteSauce(){ return this->whiteSaucePoint; } void setWhiteSauce(double p){ this->whiteSaucePoint = p; } private: double whiteSaucePoint; }; class Choco{ public: Choco(){ } Choco(blackSauce b, whiteSauce w){ this->impurities = rand() % 40; this->bs.setBlackSauce(b.getBlackSauce()); this->ws.setWhiteSauce(w.getWhiteSauce()); } blackSauce getChocoBlackSauce(){ return this->bs; } whiteSauce getChocoWhiteSauce(){ return this->ws; } double getImpurities(){ return this->impurities; } string getChocoType(){ if(this->bs.getBlackSauce() > 70){ return "黑巧克力"; } else if(this->ws.getWhiteSauce() > 70){ return "白巧克力"; } else if(this->ws.getWhiteSauce() == 50 && this->bs.getBlackSauce() == 50){ return "絕對讓你愛上我巧克力"; } else{ return "你絕對不會愛上我巧克力"; } } private: string type; double impurities; blackSauce bs; whiteSauce ws; }; class Factory{ public: Factory(blackSauce b, whiteSauce w){ this->bs = b; this->ws = w; } blackSauce getFactoryBlackSauce(){ return this->bs; } whiteSauce getFactoryWhiteSauce(){ return this->ws; } Choco generate(){ blackSauce cb; whiteSauce cw; // random value of each choco bw sauce // assume 10 double totalSauce = 100; double randomPart = rand() % 100; cb.setBlackSauce(randomPart); cw.setWhiteSauce(100-randomPart); Choco c(cb, cw); // check the total amount sauce of Factory this->bs.setBlackSauce(this->bs.getBlackSauce() - randomPart); this->ws.setWhiteSauce(this->ws.getWhiteSauce() - (100-randomPart)); return c; } void reduction(Choco c){ this->bs.setBlackSauce(this->bs.getBlackSauce() + c.getChocoBlackSauce().getBlackSauce() * 0.55); this->ws.setWhiteSauce(this->ws.getWhiteSauce() + c.getChocoWhiteSauce().getWhiteSauce() * 0.55); } private: blackSauce bs; whiteSauce ws; }; int main(int argc, char** argv) { srand(time(0)); // bw Sauce of Choco Factory blackSauce fb; whiteSauce fw; fb.setBlackSauce(1000); fw.setWhiteSauce(1000); Factory f(fb, fw); // generate a choco vector<Choco>c_container; for(int i = 0; i < 100; i++){ Choco c = f.generate(); if(f.getFactoryBlackSauce().getBlackSauce() < 0 || f.getFactoryWhiteSauce().getWhiteSauce() < 0){ cout << "Factory out of ingredient!!!" << endl; break; } c_container.push_back(c); cout << "black sauce: " << c_container[i].getChocoBlackSauce().getBlackSauce() << " " << "white_sauce: " << c_container[i].getChocoWhiteSauce().getWhiteSauce() << endl << "impurities: " << c_container[i].getImpurities() << endl << "type of Choco# " << i+1 << " is: " << c_container[i].getChocoType() << endl; if(c_container[i].getImpurities() >= 20){ f.reduction(c_container[i]); c_container.pop_back(); cout << "Choco#" << i+1 << " have to be reduced." << endl; i--; } cout << "black sauce of factory is " << f.getFactoryBlackSauce().getBlackSauce() << endl; cout << "white sauce of factory is " << f.getFactoryWhiteSauce().getWhiteSauce() << endl; cout << "---------------------------------" << endl; } c_container.shrink_to_fit(); cout << "total have " << c_container.size() << " chocolates" << endl; return 0; }