Stroustrup的实施例7,Chap.4 - C++语法
我是新的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 - //尝试读取由数字组成的整数 - 这是如何完成的,为什么这会有帮助?
谢谢,对不起问题的长格式。
对不起,拿走你的答案,男人,但我自己想出来的,事情更简单(和聪明)。
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已经等于它和的问题事实根据第一种情况的失败行事。
在
for
是在整体功能get_number()
i
从0比numbers.size()
少了一个,并把i
其中不包含数字与numbers
矢量字符串之一为val
(这样比较输入字符串你转换号码的名称成数值)。之后,您检查val
是否与矢量numbers
的大小相同,因为如果是,则不匹配(某人输入的单词不是您可以处理的任何数字的名称)。从if (cin >> x) - Why can you use that condition?
cin>>val
(cin从输入读入变量val)将返回false,如果您至少发现一个字母。如果您输入没有字母的数字,您可以将其返回(我们希望表示数字名称或正常数字的字符串)。
谢谢。这有很大帮助。 – JohnnyJohnny
我读过你提供的链接。这是非常丰富的。不幸的是,这本书取得了很大的进步,如果它是好的因为它迫使我们去追寻答案,那么这也是一种有时甚至是荒谬的方式:我无法猜测类,继承和运算符重载。非常感谢你对我的问题感兴趣。选择你的答案不仅因为它是唯一的答案,而且因为它真的帮助了我。 – JohnnyJohnny
'numbers.size()'比最后一个数字多一个,因为索引从0开始。就是这样 - 不是向量中的数字之一。 –
但矢量有字符串,并且有11个元素(0到10)和函数numbers.size()返回矢量内的元素数目。我不知道你的意思。 – JohnnyJohnny
对于'if(cin >> val)',条件是'std :: cin.operator bool()'的返回值。长话短说,流操作符(运算符'>',当在流上使用时)返回左边的参数(例如'cin >> val'返回'cin','cout