作業-進階
大一程設
東華大學
東華大學資管系
基本程式概念
資管經驗分享
還有更快的解法,但這邊以老師上課,最純粹的概念來解這一題
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;
}
前言 目前大家學下來,我們總是在 main function 裡面完成我們想做的事,隨著遇到的題目變難,或是需求上升,程式碼會越來越冗長,然而這是一件不太有效率,而且妨礙閱讀的行為,如果今天事情很繁雜,我們還只能透過 main function 來處理一切事物,那真的太糟糕了,所以為了增加程式易讀性並且有效拆分細部功能以重複利用,今天我們要來講程式開發最重要的工具之一 - 函式 function。 函式定義 在了解函式的用法之前,我們先了解他的長相,以及它的使用規則吧! return_type function_name(arguments){ /* function statements */
Mar 12, 2023前言 上一份筆記講到如何為陣列附值,也講到如何宣告一維、二維陣列,而這份筆記將更深入地針對陣列來談一些東西。 一維陣列宣告與儲存 相信有看過前一份筆記都知道陣列要如下宣告: int score[10]; 透過這樣一行我們知道我們宣告了一個陣列名為 score 含有十個儲存空間,每個空間必須都儲存整數,而一個整數在 C++ 佔 4 bytes(通常情況,不考慮 OS 的不同),所以我們知道這個陣列總共被派出了 40 bytes(4 * 10)。 <span style="color:red">不希望看到的宣告方式</span>
Mar 12, 2023二維陣列 一維的陣列其實非常簡單,但他能夠處理的事情很有限,二維,三維的陣列能夠有效地幫我們處理非常多的問題。 二維陣列的宣告 int scores[30][5]; 還記得在上一份筆記我們有舉個例子,如果有想要紀錄一個班級全部學生的五科成績這樣的情況就非常適合用二維陣列。 假設一個班有 30 位同學,就需要 150 格來儲存。當然你可以這樣寫。 int array[150]; for(int i = 0; i < 150; i++){
Mar 12, 2023前言 在撰寫程式時會需要宣告眾多的變數,實體化眾多的物件,而你在哪裏宣告這些變數與物件是非常重要的,在不同的程式區段(block)內宣告的變數他能夠存活的區域也有不同,以下會來分析變數在各個情況下的存活範圍為何 區域變數 Local Variable 變數於函示 function 之中 我們先看一段很常見的程式碼 void swap(int a, int b){ int temp; temp = a;
Mar 12, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up