prefix notation string to int conversion

孤人 提交于 2019-12-25 08:24:07

问题


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;
}

回答1:


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
       //...
}

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

sample run for input = + 3 * 4 - * 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




回答2:


declare a main string and a temp string;
declare an int number variable;
declare an int STL stack;
ask the user for the string and enter it into the main string;
declare an index variable and set its value to (main string length - 1);
start at the end of the string and check if that element is a digit;
     if it is a digit, push that digit into the temporary string, decrease
     the index variable, and check if the next element is also a digit;
     repeat this until you run into an element other than a digit;
     reverse the temp string;
     number = atoi(temp.c_str());
     push number onto the stack;
     repeat;


来源:https://stackoverflow.com/questions/10038226/prefix-notation-string-to-int-conversion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!