需要帮助C ++计算器与word输入

时间:2013-07-18 16:52:27

标签: c++ calculator

嗨,我再次尝试从他的PPP书中进行Bjarne Stroustrup练习。我已经成功完成了大部分练习,但是我遇到了一个问题。

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

我对如何做到这一点的想法是通过我的向量运行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 ("~");
}

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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,当您了解这一点时,请询问更多信息。

参考: