2013-10-15 141 views
-1

好吧,所以我實際上已經用C++編程了很長一段時間了,但我目前難以接受的東西很可能是很顯然。我決定寫一個基本的計算器以獲得樂趣。加法,減法,乘法,除法,整個很多。正如你在下面看到的,我有一個名爲choice的int變量,用於尋找1,2,3或者4.一旦選擇,它將調用相應的函數。然而,我決定我希望能隨時鍵入「幫助」來顯示幫助。我怎樣才能做到這一點?我知道我可以簡單地選擇一個字符串,但我覺得這只是對這個問題進行繃帶(對未來的問題沒有幫助)。我想在任何時候抓住「幫助」。但是,使用另一個if()語句來捕獲「幫助」顯然會給我一個錯誤 - 因爲選擇是一個int。C++ - 尋找「幫助」

請幫助我,我相信這很簡單,但由於某種原因,我無法弄清楚!

#include <iostream> 

int firstnum; 
int secondnum; 

int multiplication(){ 
    std::cout << "Multiplication chosen. Please enter first number." << std::endl; 
    std::cin >> firstnum; 
    std::cout << "Please enter second number." << endl; 
    std::cin >> secondnum; 
    std::cout << "Your answer is: " << firstnum * secondnum << "." << std::endl; 
} 

int division(){ 
    std::cout << "Division chosen. Please enter first number." << std::endl; 
    std::cin >> firstnum; 
    std::cout << "Please enter second number." << std::endl; 
    std::cin >> secondnum; 
    std::cout << "Your answer is: " << firstnum/secondnum << "." << std::endl; 
} 

int addition(){ 
    std::cout << "Addition chosen. Please enter first number." << std::endl; 
    std::cin >> firstnum; 
    std::cout << "Please enter second number." << std::endl; 
    std::cin >> secondnum; 
    std::cout << "Your answer is: " << firstnum + secondnum << "." << std::endl; 
} 

int subtraction(){ 
    std::cout << "Subtraction chosen. Please enter first number." << std::endl; 
    std::cin >> firstnum; 
    std::cout << "Please enter second number." << std::endl; 
    std::cin >> secondnum; 
    std::cout << "Your answer is: " << firstnum - secondnum << "." << std::endl; 
} 

int main(){ 
    int choice; 
    std::cout << "Calculator." << std::endl; 
    std::cout << "Multiplication: 1. Division: 2. Addition: 3. Subtraction: 4. Help: help." << std::endl; 
    std::cin >> choice; 
    if(choice == 1){ 
     multiplication(); 
    } 
    if(choice == 2){ 
     division(); 
    } 
    if(choice == 3){ 
     addition(); 
    } 
    if(choice == 4){ 
     subtraction(); 
    } 

////if the user types "help" it will show help. 

    return 0; 
} 
+0

咦?你什麼意思? – Crju

+0

哦,不,你看我的問題吧?我希望用戶能夠鍵入「幫助」。它與繞過stackoverflow過濾器無關。 – Crju

+0

對不起,我必須+1這個如果只爲可怕的雙關:) – Leeor

回答

0

要做到這一點,你所描述的,你首先需要閱讀你的輸入到STRING公尺檢查,如果「幫助」被輸入,如果沒有,如果輸入可以解析爲整數的方式。然後你會解析這個整數,並像其他函數一樣調用它。

5

我只想改變選擇爲std :: string

std::string choice; 
std::cin >> choice; 

if (choice == "1") { .... } 
if (choice == "help") { .... } 

但我也將改變if語句結構。
而不是列表if statements我會使用地圖。將該命令映射到函數調用。

#include <iostream> 
#include <map> 
#include <functional> 

int one() 
{ 
    std::cout << "one\n"; 
} 
int two() 
{ 
    std::cout << "two\n"; 
} 

int main() 
{ 
    std::map<std::string, std::function<int()> >  action = {{"one", one}, {"two", two}}; 

    auto act = action.find("one"); 
    act->second(); 

} 
+0

對不起,正如我所說,似乎只是在這個問題上寫了一個繃帶。 – Crju

+0

我甚至會拋棄「xxx選擇」部分,並存儲大多數由「std :: plus」等填充的「std :: function」。然後只有一組陳述來獲得這兩個數字。 – chris

+3

@SydZ:爲什麼看起來這樣? –

1

我喜歡使用這種命令模式。 (https://en.wikipedia.org/wiki/Command_pattern

基本上你有一個Command類/接口,諸如:

abstract class Command 
{ 
    virtual void run() = 0; 
} 

,然後具有從繼承不同的命令:

class HelpCommand : Command 
{ 
    virtual void run() 
    { 
     // do stuff 
    } 
} 

然後,在從得到的命令處理程序用戶,你將有一個stringCommand的地圖。因此,當用戶輸入一個命令(例如「help」)時,它會從地圖中獲取適當的命令,然後調用它的run()方法。

unordered_map< string, Command* > commands; 
commands[ "help" ] = new HelpCommand(); 

// ... 

// get input 
commands[ input ]->run();