Dev - C++ - ayuda con calculadora

 
Vista:
sin imagen de perfil

ayuda con calculadora

Publicado por cristhian (1 intervención) el 18/11/2016 21:47:34
hola amigos tengo un problema con un proyecto de la U y es que nos pidieron hacer una calculadora que resuelva ejercicios de este tipo 8*(5+5) bueno eso todo bien

el problema es este si quiero hacer una suma normal ejemplo 12+2 me da el resultado de 4 osea toma 2+2 y no toma el 1 aca dejo el codido por si alguien me ayuda

#include<iostream>
#include <stack>
#define opciones 4

using namespace std;

const string wx[opciones] = {"/", "*", "+", "-"};
int ps[opciones] = {1,3};
bool x( const string );
int y( const string );
int main() {
string pila, postfijo, num;
stack <string> M;
stack <double> resp;
size_t D;
char c;
double j, o;
cout <<"INGRESE UN VALOR :">
getline( cin, pila );
for ( D = 0; D < pila.size(); D++ ) {
c = pila[D];
num.clear();
num += c;
cout<< c ;

if ( c == ' ' ) continue;

if ( c >= '0' && c <= '9' ) {
cout<<"\tvalor numerico encontrado"<<endl<<endl;
postfijo = postfijo + " " + c;
continue;
}


if ( x( num ) ) {
cout << "\tes operador colocar en la pila" << endl<<endl;
while ( !M.empty() && y(M.top() )
>= y( num ) ) {
postfijo = postfijo + " " + M.top();
M.pop();
}
M.push( num );
continue;
}

if ( num == "(" ) {
cout << "\t Parentesis encontrado" << endl << endl;
M.push( num );
continue;
}


if ( num == ")" ) {
while ( !M.empty() && M.top() != "(" ) {
cout << "\t Parentesis encontrado" + M.top() << endl << endl;
postfijo = postfijo + " " + M.top();
M.pop();
}
if ( !M.empty() )
M.pop();
}
}

while ( !M.empty() ) {
postfijo = postfijo + " " + M.top();
M.pop();
}


cout << "Orden postfijo" << postfijo << endl;
j = 0;
for ( D = 0; D < postfijo.size(); D++ ) {
num.clear();
c = postfijo[D];
num += c;
if ( c >= '0' && c <= '9' ) {
resp.push( c - '0' );
}
if ( x( num ) ) {
if ( !resp.empty() ) {
o = resp.top();
resp.pop();
}
if ( !resp.empty() ) {
j = resp.top();
resp.pop();
}
if ( num == "+" ) {
j += o;
resp.push( j );
}
else if ( num== "-" ) {
j -= o;
resp.push( j );
}
else if ( num == "*" ) {
j *= o;
resp.push( j );
}
else if (num == "/" ) {
j /= o;
resp.push( j );
}
}
}
if ( !resp.empty() )
cout << endl << "La respuesta es : "<<j<< endl;
return 0;
}
bool x( const string num ) {

for ( int i = 0; i < opciones; i++ )
if ( wx[i] == num )
return true;
}
int y( const string num ) {
for ( int i = 0; i < opciones; i++ )
if ( wx[i] == num )
return ps[i];
return -1;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder