【问题标题】:Need help C++ calculator with word input需要帮助带有单词输入的 C++ 计算器
【发布时间】:2013-07-18 16:52:27
【问题描述】:

您好,我正在尝试 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 ("~");
}

【问题讨论】:

  • 首先缩进你的代码。我是认真的!此外,main 返回 int,而不是 void。此外,它拼写为“初始化”。
  • @Mgetz:嗯不是真的。

标签: c++ calculator


【解决方案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;

【讨论】:

  • 你可以使用std::map&lt; std::string, int &gt;而不是向量来改进它
【解决方案2】:

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

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,当你明白了,再询问更多信息。

参考:

【讨论】:

  • 只对C++11有效!
  • 我知道。他从编程开始(不仅仅是 C++ 编程,正如他的问题所示),所以我提供了一种不仅适用于 C++ 的技术,一种通用技术。如果旧版本的 C++ 不支持此技术(如果您愿意,请将 std::unordered_map 更改为 std::map)不是重点。
猜你喜欢
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多