Stroustrup Ex.7,第4章 - C ++语法

时间:2017-05-27 21:07:03

标签: c++ function ppp

我是C ++的新手,而且和其他许多人一样,我试图从Bjarne Stroustrup的编程 - 使用C ++的原理和实践中学习它。

我坚持练习7,第4章,其中的想法是写一个计算器,当输入是一个整数和/或一个字符串后跟一个字符(+, - ,*或/),输出应声明"" 输入 1的sum / diff / prod / ratio 并输入 2 是结果;所以,如果(" 两个" 3 *)是输入,则输出应为" 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&gt;&gt; val) - 为什么这是有条件的? val是==矢量数的大小,是11,所以条件数是11吗?这有什么用? 什么回来了?本身?

3 - //尝试读取由数字组成的整数 - 如何完成,为什么这有用?

谢谢,对不起这个问题的长格式感到抱歉。

2 个答案:

答案 0 :(得分:2)

  1. for中,整个函数get_number() i从0到1小于numbers.size()并将i放在输入字符串不在包含与numbers向量中的一个字符串进行比较的数字val(因此您将数字名称转换为数字值)。之后,检查val是否与向量numbers的大小相同,因为如果是,则没有匹配(有人输入的单词不是您可以处理的任何数字的名称)。

  2. if (cin >> x) - Why can you use that condition? cin>>val(如果您输入至少一个字母,则cin从输入读取到变量val)将返回false。

  3. 如果您输入没有字母的数字,您可以将其返回(我们需要代表姓名或正常数字的字符串)。

答案 1 :(得分:0)

很抱歉收起你的回答,伙计,但我自己想出来了,事情要简单得多(而且很聪明)。

for循环正常工作,将输入字符串与向量内的字符串进行比较,并返回对应的索引号。

但是将numbers.size()的值赋值给not_a_symbol的原因,给val值number.size()的原因在于,如果第一个IF失败,那么第二个IF将变为true因为val已经初始化了。这就是为什么有2个独立的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已经等于它的事情,而这个事实是对第一个条件的失败起作用的。

相关问题