iT邦幫忙

2022 iThome 鐵人賽

DAY 27
0
Software Development

C++超級菜鳥也可以懂的物件導向攻略系列 第 27

Day 27 C++ 虛擬貨幣 Crypto bot - 把之前的code變成 function

  • 分享至 

  • xImage
  •  

繼前面兩篇Day25、Day26的初步功能跟menu,接下來要邁向物件導向的分裝大法,讓我們先來把他們一個一個分類開來變成function吧~
到時候就可以直接包進method去。

首先來做menu,menu沒有return value,因此使用void:

void menu(){
    cout << "*****MENU*******" <<endl;
    cout << "1.Show Current Price" << endl;
    cout << "2.Find Crypto info" <<  endl;
    cout << "3.Find Market Cap" <<  endl;
    cout << "4.Help" <<  endl;
    cout << "5.Exit" <<  endl;
    cout << "****************" <<  endl;
}

這樣我們就能用menu()來呼叫出上面的主頁選單,

int main(){
    menu();
}

接下來是我們的swtich case,動作區域。
首先看到整塊區域因為也沒有return value,所以一樣用void包起來就可以了。

void action(){
    int option;
    cout << "Type the service you'd like to do: " << endl;
    cin >> option;
    do{
        switch(option){
            case 1:
            {
                cout << "Enter the Crypto (1.BTC, 2.ETH, 3.USDT 4. Exit):"<<endl;
                int cname;
                cin >> cname;
                cout << "You entered: " << cname << endl;
                cryptprice(cname);
                break;
            }
            case 2:
            {
                cout <<"Enter the name of the crypto you'd like to see info with:" << endl;
                cout << "1.BTC, 2.ETH, 3.USDT 4. Exit" << endl;
                int x;
                cin >> x;
                cout << "you entered: " << x<< endl;
                cryptinfo(x);
                break;}
            case 3:
            {   cout <<"Enter the crypto you'd like to see Market Cap with:" << endl;
                cout << "1.BTC, 2.ETH, 3.USDT 4. Exit" << endl;
                int y;
                cin >> y;
                cout << "Market cap is: $" << marketcap(y) << endl;
                break;}
            case 4:
                cout <<"Enter the name of the crypto you'd like to see info with:" << endl;
                // cin >> x;
                break;
            case 5:
                cout <<"Enter the name of the crypto you'd like to see info with:" << endl;
                // cin >> x;
                break;
        }
    }while (option != 5);
    
}

這樣一來int main()只需要用兩行,就可以展示出一模一樣的資訊。

int main(){
    menu();
    action();

    return 0;
}

接下來把其他的功能,如選項一、選項二等等一一包進function。

以上分類好的function讓我們能夠在執行功能後回到選單的選項,可以在選項後加上menu()、action(),如下所示:

void cryptprice(int c){
    if(c == 1){
        ifstream file("BTC.txt", ios::in);
        if(file.is_open()){
            string s;
            while(file >> s){
                cout << s << endl;
            }
        }else{
            cout <<"Error";
        }
        file.close();
    }else if(c == 2){
        ifstream file("ETH.txt", ios::in);
        if(file.is_open()){
            string s;
            while(file >> s){
                cout << s << endl;
            }
        }else{
            cout <<"Error";
        }
        file.close();
    }else if(c == 3){
        ifstream file("USDT.txt", ios::in);
        if(file.is_open()){
            string s;
            while(file >> s){
                cout << s << endl;
            }
        }else{
            cout <<"Error";
        }
        file.close();
    }else{ //如果都不是上面的選項
        menu();  //回到選單 顯示menu
        action();   //給用戶選擇要執行哪個選項 
     }
}
void cryptinfo(int c){
    if(c == 1){
        cout << "Total Bitcoin: 19,166,487 BTC" << endl;
        cout << "First Block(Bitcoin creation date): 2009-01-09" << endl;
    }else if(c == 2){
        cout << "Total ETH: 19,166,487 BTC" << endl;
        cout << "First Block(Bitcoin creation date): 2009-01-09" << endl;
    }else if (c == 3){
        cout << "Total USDT: 19,166,487 BTC" << endl;
        cout << "First Block(Bitcoin creation date): 2009-01-09" << endl;
    }else {
        menu();
        action();
    }
}
void marketcap(int c){
    if(c == 1){
        cout << "367,982,010,627" << endl;
     }else if(c == 2){
         cout << "163,879,592,332" << endl;
     }else if(c == 3){
        cout << "68,269,066,618" <<endl;
     }else if(c == 4) {
        menu();
        action();
    }
}

void help(int c){
    if(c == 1){
        cout << "If you endure any system problem please restart the bot and login again." <<endl;
    }else if(c == 2){
    cout <<"Telephone: 120-332-593. Email: crypto-servie@cryptobot.com Open:Mon-Fri AM9:00~PM4:30"<< endl;
    }else {
        menu();
        action();
    }
}

上一篇
Day 26 C++ 物件導向7 Polymorphism
下一篇
Day 28 C++ Polymorphism II - Virtual function 虛擬函數
系列文
C++超級菜鳥也可以懂的物件導向攻略30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言