int main()
{
long long x,y,z,result;
char f,g;
cin >>x>>y>>z;
**result** =
cout << result ;
return 0;
}
如何使result = x(+或 - 或/或*)y(+或 - 或/或*)z!?如何在C++中輸入2個字符
int main()
{
long long x,y,z,result;
char f,g;
cin >>x>>y>>z;
**result** =
cout << result ;
return 0;
}
如何使result = x(+或 - 或/或*)y(+或 - 或/或*)z!?如何在C++中輸入2個字符
讀運營商在數字之間是簡單的:
long long x,y,z;
char f,g;
cin >>x>>f>>y>>g>>z;
// See what you've got
cout << x << " " << f << endl;
cout << y << " " << g << endl;
cout << z endl;
然而,摸清了操作的結果是棘手:您需要檢查你f
和g
得值,並執行根據需要進行操作。請注意,您的號碼和操作員之間不能有空格,否則輸入將被錯誤地處理。
這可能是你正在解決這項工作的核心,所以我會建議你寫這樣的功能:
long long compute(long long a, long long b, char op) {
... // Check the operator, and return the result
}
有了這些功能,你可以產生的結果在一個簡單的調用:
long long result = compute(compute(x, y, f), z, g);
一旦你寫的compute
功能,這應該得到的結果爲你的前面面觀。
@AmrMorsy你的輸入應該是這樣的:'12345 + 678 * 91011',不是這樣的:'12345 + 678 * 91011'。否則,空格將變爲'f',並且'z'根本不會讀取。 – dasblinkenlight 2014-11-03 21:58:08
你可以做cin >> astring。並用分隔符分隔字符串並將它們轉換爲整數。
例如: 1,2,3 將變成'1','2','3'。
'result = x + y + z;'我錯過了什麼? – Degustaf 2014-11-03 21:48:21