函數調用後,C++是否有任何類型的實用程序返回函數的開頭?例如,例如在計算函數中調用help()。返回函數開頭的方法
void help()
{
cout << "Welcome to this annoying calculator program.\n";
cout << "You can add(+), subtract(-), multiply(*), divide(/),\n";
cout << "find the remainder(%), square root(sqrt()), use exponents(pow(x,x)),\n";
cout << "use parentheses, assign variables (ex: let x = 3), and assign\n";
cout << " constants (ex: const pi = 3.14). Happy Calculating!\n";
return;
}
void clean_up_mess() // purge error tokens
{
ts.ignore(print);
}
const string prompt = "> ";
const string result = "= ";
void calculate()
{
while(true) try {
cout << prompt;
Token t = ts.get();
if (t.kind == help_user) help();
else if (t.kind == quit) return;
while (t.kind == print) t=ts.get();
ts.unget(t);
cout << result << statement() << endl;
}
catch(runtime_error& e) {
cerr << e.what() << endl;
clean_up_mess();
}
}
雖然在技術上我的幫助功能的實現工作正常,但並不完美。在呼叫幫助後,返回,繼續嘗試cout < <結果< < statement()< < endl;這是不可能的,因爲沒有輸入數值。因此它給出了一點錯誤信息(程序中的其他地方),然後繼續執行程序。功能沒問題,但它很醜,我不喜歡它(:P)。
那麼當幫助函數返回時,還有什麼辦法可以回到計算開始並重新開始嗎? (我在if(t.kind == help_user)塊中插入一個函數調用來調用calculate,但是正如我認爲這只是延遲問題而不是解決它。)
或者,如果你只是滾動下來一點,你會發現,你可以做到這一點不只是一個簡單的改變你的邏輯繼續下去,如不執行代碼,你不想... – stefanB 2009-06-30 03:41:37