2017-05-27 200 views
0

我是新的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 - //嘗試讀取由數字組成的整數 - 這是如何完成的,爲什麼這會有幫助?

謝謝,對不起問題的長格式。

+0

'numbers.size()'比最後一個數字多一個,因爲索引從0開始。就是這樣 - 不是向量中的數字之一。 –

+0

但矢量有字符串,並且有11個元素(0到10)和函數numbers.size()返回矢量內的元素數目。我不知道你的意思。 – JohnnyJohnny

+1

對於'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'的表達式。 –

回答

0

對不起,拿走你的答案,男人,但我自己想出來的,事情更簡單(和聰明)。

for-loop按照它應該的方式工作,將輸入字符串與向量內的字符串進行比較並返回相應的索引號。

但是,將numbers.size()的值賦值給not_a_symbol,給val賦值爲numbers.size()的原因在於,如果第一個IF失敗,只有第二個IF會失敗true,因爲val已經初始化。這就是爲什麼有兩個獨立的IF語句,而不是一個IF-ELSE:一個ELSE不會進行先前初始化的VAL計數,因爲來自字符串「s」的輸入將接管,阻止val的初始值處理(val = not_a_symbol)在ELSE內部。

忘記的功能,把它裏面的所有主:

int main() { 

    vector<string> numbers{ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" }; 
    int not = numbers.size(); 
    int val = numbers.size(); //val is initialized 
    string s; 
    cin >> s; 
    for (int i = 0; i<numbers.size(); ++i) 
     if (numbers[i] == s) val = i; // first IF; if this condition is not met, it will return val as it was initialized (val=numbers.size() or 11) 
    if (val == not) val = 88; // then this condition will be checked. It will be true. It will return val = 88 (a random number); 

    cout << "val is: " << val << "\n" << "not is: " << not << "\n"; 
} 

所以,它不是VAL與元素的向量的數量進行比較的問題,是Val已經等於它和的問題事實根據第一種情況的失敗行事。

2
  1. for是在整體功能get_number()i從0比numbers.size()少了一個,並把i其中不包含數字與numbers矢量字符串之一爲val(這樣比較輸入字符串你轉換號碼的名稱成數值)。之後,您檢查val是否與矢量numbers的大小相同,因爲如果是,則不匹配(某人輸入的單詞不是您可以處理的任何數字的名稱)。

  2. if (cin >> x) - Why can you use that condition?cin>>val(cin從輸入讀入變量val)將返回false,如果您至少發現一個字母。

  3. 如果您輸入沒有字母的數字,您可以將其返回(我們希望表示數字名稱或正常數字的字符串)。

+0

謝謝。這有很大幫助。 – JohnnyJohnny

+0

我讀過你提供的鏈接。這是非常豐富的。不幸的是,這本書取得了很大的進步,如果它是好的因爲它迫使我們去追尋答案,那麼這也是一種有時甚至是荒謬的方式:我無法猜測類,繼承和運算符重載。非常感謝你對我的問題感興趣。選擇你的答案不僅因爲它是唯一的答案,而且因爲它真的幫助了我。 – JohnnyJohnny