我是新的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'的表达式。 –