You are here:

C++/Calculator (recursion version)

Advertisement


Question
Brief Description: User enter an expression such as "1+3+(2*5)" and the result is displayed i.e. 14.

Here the code i managed to get so far with the help of a friend:

CODE:
------------------------------------------------
#include<iostream>
#include<sstream>
#include<string>
#include<cctype>
#include<cmath>
using namespace std;

enum {PLUS='+',MINUS='-',MUL='*',DIV='/'};


double Value_Of_Num(string &Expr)


{

   istringstream is(Expr);
   double value=0.0;
   is >> value;
   return value;
}

double Value_Of_Expr(string &Expr)

{
  int i=0,p=0,pos_mul=0,pos_div=0;

   if(Expr.at(0)=='('&&Expr.at(Expr.length()-1)==')')
  {
     for(;i<Expr.length();i++)


         {
        
        if(Expr.at(i)=='(') p++;
         else if(Expr.at(i)==')') p--;
         if(p==0) break;
        
        }

       if(i==Expr.length()-1)
       return Value_Of_Expr(Expr.substr(1,Expr.length()-2));
   }

   for(;i<Expr.length();i++)


       {
     
     if(Expr.at(i)=='(') p++;
       else if(Expr.at(i)==')') p--;
       else if(p==0&&ispunct(Expr.at(i)))
         
        {
        
        switch(Expr.at(i))


         {
         case PLUS:
         return Value_Of_Expr(Expr.substr(0,i))+Value_Of_Expr(Expr  .substr(i+1,Expr.length()-i-1));
         case MINUS:
         return Value_Of_Expr(Expr.substr(0,i))-Value_Of_Expr(Expr.substr(i+1,Expr.length()-i-1));
         case MUL: pos_mul=i; break;
         case DIV: pos_div=i; break;
         
         }

        }

     }

  if(pos_mul)
  return Value_Of_Expr(Expr.substr(0,pos_mul))*Value_Of_Exp  r(Expr.substr(pos_mul+1,Expr.length()-pos_mul-1));
  
  if(pos_div)
  return Value_Of_Expr(Expr.substr(0,pos_div))/Value_Of_Expr(Expr.substr(pos_div+1,Expr.length()-pos_div-1));
  
  return Value_Of_Num(Expr);
}






// several important validation checks on the input
bool Check(string input_string)
{
   
   for (int r = 0; r<input_string.length(); r++)
   {
       if((input_string[r]=='+')||(input_string[r]=='-'))
       {
         if((input_string[r+1]=='+')||(input_string[r+1]=='-'))
         {
         return false;
         }
       }
   }


   string array="0123456789+-*/()";

   int count=0;
   for (int s=0; s<input_string.length(); s++)
   {
       for(int z=0; z<array.length(); z++)
       {
         if(input_string[s]==array[z])
         {
         count++;
         }
       }
   }

   if (count == input_string.length())
   {
     return true;
   }
   else
   {
     return false;
   }

}





int main()

{
string expression;

cout <<"Enter your expression:";
cin >> expression;
  
  if(Check(expression)==true)

  {
     cout << Value_Of_Expr(expression)<<endl;
  }
  else
  {
     cout<<""<<endl;
     cout<< "Error in the input" << endl;
  
  }
return 0;
}
-----------------------------------------------
END OF CODE


It is for a homework and it's need to be of recursive type.
The code above work @ 100% but I need to implement the following additional features:
> Able to compute negative numbers
> Multiple brackets such as "((1+3) +(9/3))*2)

I do not knw how to proceed further - PLZ HELP - Any suggestions or corrections in the code above will be greatly appreciated. Thx in advance.

Answer
Hello John, thank you for the question.

I am glad to see you are using std::strings. These can help make your life way easier with the multiple bracketpart of your question. You should take a look at its member functions find_first_of, and find_last_of. You can use those to parse your multiple brackets efficiantly. They return an index where the char is, and you can parse accordingly. I glanced over your code, and I see no reason why you would not be able to do negative numbers. std::istringstream again makes your life easy, and should parse a negative number out normally. What kind of errors are you getting if this is not the case?

I hope this information is helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.