prefijos pasar numero notacion normal fraccion convertir conversiones con como cientifica c++ string int prefix

c++ - pasar - prefijos conversiones



cadena de notación de prefijo a conversión int (2)

Para un caso simple como el suyo, una solución de pseudo código puede ser:

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

tenga en cuenta que después de extraer op1, whitespace_pos debería haber cambiado en la función.

muestra ejecutada para 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

Tenga en cuenta que no lo he probado. También que esto se puede implementar en un bucle (en lugar de recursividad) de una manera mucho mejor

He estado tratando de pensar en una forma de convertir una cadena en un entero, conozco el viejo atoi () en C y también la función sstream para convertir un tipo de cadena en un entero. Intento escribir un programa que tome una notación de prefijo y produzca un resultado recursivo. El programa funciona, cuando uso char en lugar de string, pero no estoy seguro de cómo se supone que debo usar strings para resolver este problema. Tengo que tenerlo para que el usuario ingrese + 3 3 y el resultado es 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; }


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;