1

大家好,我正在尝试 Bjarne Stroustrup 的 PPP 书中的练习。我已经设法完成了大部分练习,但是我遇到了一个问题。

在这个程序中,基本思想是有一个可以接受整数和字符串输入的计算器。实际的计算器部分工作正常,我可以毫无问题地输入整数。我的问题在于尝试将字符串输入说“一”转换为整数一。

我如何做到这一点的想法是在我的向量中运行一个 for 循环,该向量以单词的形式存储数字 1-10,当它找到一个包含与用户输入匹配的字符串的索引时,它使用 for 循环计数器变量;这应该等于用户输入的数量。

这个想法应该可行,并且 Bjarne 的示例代码使用了类似的想法,但是我的略有不同并且似乎不起作用,我遇到的问题似乎是当将用户输入与向量索引进行比较时它没有似乎找不到任何匹配项,我已经搞砸了几个小时,似乎找不到原因。无论如何这里是代码:

//simple calculator program, users can input words 1-10 and an integer will be     returned. 

// header files
#include "../../std_lib_facilities.h"

//global varible- vector set up here. 
vector<string> numbers;

//functions, one to initiliase vectors, one to get number, and a main function. 

void initiliase() {  
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");
}

int get_number(){
char choice;
string type_val;
int val = 0;
cout << "do you wish to enter a number or word? n/w" << endl;
cin >> choice;

if ( choice == 'n'){
cin >> val;
return val;
}

else if(choice == 'w'){
cin >> type_val;
for (int i = 0; i<numbers.size(); i++){
    if (numbers[i] == type_val)
    val = i;
else
    cout << "number not found in vector.";
    return val;
 }
}
}

      void print_answer(int ans, char oper, int val1,int val2) {
cout << "Your Answer is:" << ' ' << val1 << ' ' << oper << ' ' << val2 << ' ' << '=' <<   ans << endl;

}

void main() {

initiliase(); 
int val1, val2, answer;
char op;
val1 = get_number();
val2 = get_number();
cout << "Please input operation:";
cin >> op; 
switch (op){

case '+': cout << "You have chosen addition!" << endl;
      answer = val1 + val2;
      print_answer (answer, op, val1, val2);
      break;
case '-': cout << "you have chosen subtraction!" << endl;
      answer = val1 - val2;
          print_answer (answer, op, val1, val2);
      break;
case '*': cout << "you have chosen multiplication!" << endl;
          answer = val1 * val2;
          print_answer (answer, op, val1, val2);
      break;
case '/': cout << "you have chosen division!" << endl; 
          answer = val1 / val2;
      print_answer  (answer, op, val1, val2);
      break;
case '%': cout << "you have chosen modulos!" << endl;
      answer = val1 % val2;
      print_answer (answer, op, val1, val2);
      break;
default: cout << "incorrent operation" << endl;
}
keep_window_open ("~");
}
4

2 回答 2

1

您的最终for循环有问题。
目前,它不能返回 0 以外的任何内容,问题是您的else语句在循环内,因此引用if (numbers[i] == type_val).

您可能想尝试类似的方法:

if ( choice == 'n')
{
    cin >> val;
}
else if(choice == 'w')
{
    cin >> type_val;
    bool found = false;
    for (int i = 0; i<numbers.size() && !found; i++){
        if (numbers[i] == type_val)
        {
            val = i;
            found = true;
        }
    }
    if(!found){ cout << "Number not found."; }
}
return val;
于 2013-07-18T17:05:30.970 回答
0

您可以使用哈希图来存储字符串和数字之间的对应关系:

std::unordered_map<std::string,int> string_to_integer;

//Hashmap setup:
string_to_integer["one"] = 1;
string_to_integer["two"] = 2;
//... etc.

//Example of use:

std::string input;
int input_number;

std::cin >> input;
input_number = string_to_integer[input];

请注意,这对操作员也有效,以避免switch. 但我没有提供这方面的示例,因为您是从 C++ 开始的,我不想让您与更高级的代码混淆。首先尝试string_to_input,当您了解这一点时,请询问更多信息。

参考:

于 2013-07-18T17:07:18.270 回答