Hey..
I wanna make a calculator .. which takes input as a string...
example:
Input : "1 + 2"
Output: 3
"1+2" is passed as a parameter which is in the format of a string. The return value is 3 which an integer format.

calculator needs only to perform +,*,-,/

i can make calculating logic myself.. jst m not able to think how to exrtract data from string..

thnx in advance :)

Recommended Answers

All 6 Replies

Please correct me if I am wrong,for I do not have much experiance with type casting.But. Why don't you try to change the charecter data to intiger by type casting?

jst m not able to think how to exrtract data from string..

We can use our standard string parsing algorithm. In this case, we can use find_first_of() since we will have multiple delimeters (+, -, *, and / for example)

string expression = "5+104-32/5";
string terms[20];
int prev=-1, curr=-1; 

while(curr_pos != string::npos)
{     
     prev_pos = curr_pos;     
     curr_pos = find_first_of("+-*/", prev_pos+1);      

     if(curr_pos != string::npos)           

          terms[i] = expression.substr(prev_pos+1, (curr_pos - prev_pos)); 
}

You can learn about other <string> class member here that will help you with your parsing needs.

Hope this helps to point ye' in the right direction. This is a basic string parsing algorithm for <string> class objects. This is untested code and may contain easy to fix errors.

We can use our standard string parsing algorithm. In this case, we can use find_first_of() since we will have multiple delimeters (+, -, *, and / for example)

string expression = "5+104-32/5";
string terms[20];
int prev=-1, curr=-1; 

while(curr_pos != string::npos)
{     
     prev_pos = curr_pos;     
     curr_pos = find_first_of("+-*/", prev_pos+1);      

     if(curr_pos != string::npos)           

          terms[i] = expression.substr(prev_pos+1, (curr_pos - prev_pos)); 
}

You can learn about other <string> class member here that will help you with your parsing needs.

Hope this helps to point ye' in the right direction. This is a basic string parsing algorithm for <string> class objects. This is untested code and may contain easy to fix errors.

I've never seen or used this technique before.. nd m sure there is some easier way also to do this .. as from where i got this problem.. this was files under level 1 problem.
although i've understood the approach given by you..
if i'm nt able to think of something else.. i'll sure use this to make sub strings and then convert the char to int type by using atoi function...
also i have to make a simlple calculator.. with only one operation at a time.. i.e only 5+4 at a time and not 5+4*3 bcoz dat will involve precedence rules...

Of course you can always perform array operations on your string if you are not comfortable with using string class members:

#include<cctype>

string expression = "5+104-32/5";
string operators[10];
string terms[20];
int term_counter=0;
int op_counter=0;

for(int i=0; i<expression.size(); i++)
{
     if(expression[i] == '+' || expression[i] == '-' ||
        expression[i] == '*' || expression[i] == '/')
     {
          //extract all operators (in sequential order)
          operators[op_counter] = expresssion[i];
          op_counter++;
          term_counter++;
     }
     else if(isdigit(expression[i]))
     {
          //Extract terms into individial 'string' containers
          terms[term_counter] += expression[i];          
     }             
}

also i have to make a simlple calculator.. with only one operation at a time.. i.e only 5+4 at a time and not 5+4*3 bcoz dat will involve precedence rules...

All of the above code will work for what you are asking.

commented: thnx :) +1

well by precedence I was not meaning which comes first in order.. but precedence rules of mathematics.. BODMAS ...
but anyway I've got the hint now how to solve this problem..
thnx again :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.