本文介绍了前缀表示法字符串到int转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想一种将字符串转换为整数的方法,我知道C中的旧atoi()以及将字符串类型转换为整数的sstream函数。我正在尝试编写一个程序,它接受前缀表示法并递归地生成结果。程序工作,当我使用char而不是字符串,但我不确定我是如何使用字符串来解决这个问题。我必须拥有它,以便用户输入+ 3 3,结果为6.

I've been trying to think of a way to convert a string to an integer, I know the old atoi() in C and as well as the sstream function to convert a string type to a integer. I am trying to a write a program that that takes in a prefix notation and produces a result recursively. The program works, when I use char instead of string, but I am not really sure how I am suppose to use strings to solve this problem. I have to have it so that the user enter + 3 3 and result is 6.

#include <iostream>
#include <string>
using namespace std;

int stringToAscii(string value){
    if (value == '+')
        return '+';
    if (value == '*')
        return '*';
    if (value == '-')
        return '-';
    if (value == '/')
        return '/';
}

int prefixNotationCalc(string value){
    char newValue = value;
    int number1=0;
    int number2=0;
    //while () {
        switch (newValue){
        case '*':
            cin >> number1;
            cin >> number2;
            return (number1*number2);
            break;
        case '+':
            cin >> number1;
            cin >> number2;
            return (number1+number2);
            break;
        case '-':
            cin >> number1;
            cin >> number2;
            return (number1-number2);
            break;
        case '/':
            cin >> number1;
            cin >> number2;
            return (number1/number2);
            break;
        }
    //}
}

int main (){
    //The function takes in a string value
    string value;
    cin >> value;
    cout << "Result is: "<< prefixNotationCalc(value)<< endl;
    return 0;
}


推荐答案

对于你的简单案例,伪代码解决方案可以是:

For simple case as yours, a pseudo code solution can be:

//assuming input like + 3 * 4 - * 6 10 8
//(note: the ints can have more than one digit)
int prefixNotationCalc(string input, int &start)
{
  string token = scan_from_start_of_string_to_first_whitespace
  int whitespace_pos = whitespace_position
  if (token contains digits)
    return int_equivalent_of_token
  else
    int op1 = prefixNotationCalc(input, whitespace_pos)
    int op2 = prefixNotationCalc(input, whitespace_pos)
    switch(token as operator)
      case + : return op1 + op2
       //...
}

请注意,在提取op1之后,whitespace_pos应该在函数中发生了变化。

note that after op1 is extracted, whitespace_pos should have changed in the function.

输入的样本运行= + 3 * 4 - * 6 10 8

sample run for input = + 3 * 4 - * 6 10 8

令牌,op1,op2

+,3,* 4 - * 6 10 8

3

*,4, - * 6 10 8

4

- ,* 6 10,8

*,6,10

6

10

8

token , op1 , op2
+ , 3 , * 4 - * 6 10 8
3
* , 4 , - * 6 10 8
4
- , * 6 10, 8
* , 6 , 10
6
10
8

请注意,我还没有测试过。此外,这可以以更好的方式在循环(而不是递归)中实现

Please note that I have not tested it. Also that this can be implemented in a loop (instead of recursion) in much better way

这篇关于前缀表示法字符串到int转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:46