我是新的C++和其他很多在這裏,我想從Bjarne的Stroustrup的編程學習它 - 原理與實踐使用C++。Stroustrup的實施例7,Chap.4 - C++語法
我卡上練習7,Chap.4,其中所述想法是寫一個計算器,當輸入是要麼的整數和/或後跟一個字符(字符串 +, - ,*或/),輸出應公佈「輸入和輸入的總和/差異/產品/比率是結果;所以如果(「兩個」 *)是輸入,輸出應該是「2 * 3 = 6的乘積」。
這裏的Stroustrup的解決方案(我要離開Stroustrup的評論):
- 有沒有侵犯版權,因爲這是所有從他的網站 -
/*The solution uses two functions (in addition to main():
initialize_numbers() to initialize the vector of number string
representations
get_number() to read a number that is either a string or a sequence of
digits
*/
vector<string> numbers; // representation of numbers as strings
// numbers[i] is the string representation for i
// for numbers[0] to numbers[numbers.size()-1]
void initialize_numbers()
{
numbers.push_back("zero");
numbers.push_back("one");
numbers.push_back("two");
numbers.push_back("three");
numbers.push_back("four");
numbers.push_back("five");
numbers.push_back("six");
numbers.push_back("seven");
numbers.push_back("eight");
numbers.push_back("nine");
numbers.push_back("ten"); // why not? :-)
}
int get_number()
{
const int not_a_symbol = numbers.size(); // not_a_symbol is a value that does not correspond
// to a string in the numbers vector
int val = not_a_symbol;
if (cin>>val) return val; // try to read an integer composed of digits
cin.clear(); // clear string after failed attempt to read an integer
string s;
cin>>s;
for (int i=0; i<numbers.size(); ++i) // see if the string is in numbers
if (numbers[i]==s) val = i;
if (val==not_a_symbol) error("unexpected number string: ",s);
return val;
}
int main()
try
{ initialize_numbers();
cout<< "please enter two floating-point values separated by an operator\n The operator can be + - */% : ";
while (true) { // "forever"; that is until we give an unacceptable input or make a computations error
int val1 = get_number();
char op = 0;
cin>>op; // get the operator
int val2 = get_number();
string oper; // text appropriate for an operator
double result;
switch (op) {
case '+':
oper = "sum of ";
result = val1+val2;
break;
case '-':
oper = "difference between ";
result = val1-val2;
break;
case '*':
oper = "product of ";
result = val1*val2;
break;
case '/':
oper = "ratio of ";
if (val2==0) error("trying to divide by zero");
result = val1/val2;
break;
case '%':
oper = "remainder of ";
if (val2==0) error("trying to divide by zero (%)");
result = val1%val2;
break;
default:
error("bad operator");
}
cout << oper << val1 << " and " << val2 << " is " << result << '\n';
cout << "Try again: ";
}
}
更具體地說,我的問題與以下部分:
int get_number()
{
const int not_a_symbol = numbers.size(); // not_a_symbol is a value that does not correspond
// to a string in the numbers vector
int val = not_a_symbol;
if (cin>>val) return val; // try to read an integer composed of digits
cin.clear(); // clear string after failed attempt to read an integer
等等等等...... }
我只是不明白是怎麼回事,在大環境。我無法理解整個get_number()函數,以及它如何與代碼的其餘部分相關。
1 - 爲什麼將number.size()的值賦值爲not_a_symbol?這完成了什麼?
2 - if(cin >> val) - 爲什麼是條件? val是==矢量數的大小,它是11,那麼條件數是11?這有幫助嗎? 它返回什麼?本身?
3 - //嘗試讀取由數字組成的整數 - 這是如何完成的,爲什麼這會有幫助?
謝謝,對不起問題的長格式。
'numbers.size()'比最後一個數字多一個,因爲索引從0開始。就是這樣 - 不是向量中的數字之一。 –
但矢量有字符串,並且有11個元素(0到10)和函數numbers.size()返回矢量內的元素數目。我不知道你的意思。 – JohnnyJohnny
對於'if(cin >> val)',條件是'std :: cin.operator bool()'的返回值。長話短說,流操作符(運算符'<<' and '>>',當在流上使用時)返回左邊的參數(例如'cin >> val'返回'cin','cout << x'返回'cout' ),所以它們可以鏈接在一起(例如,因爲'cout << x'返回'cout','cout << x << y'有效並且變成'(cout << x)<< y',或者'cout << x; cout << y')。 'if'接受可以轉換爲'bool'的表達式。 –