Hi guys!

So I need help with a small problem. I'm using a char pointer array which contains a mathematical expression.

For example: "sqrt 25"

Now I want to use the inbuild c++ function sqrt(int)). I'm first searching in the array for the letters 'sqrt' so that my custom function knows what to math function to call. As the example is only a small part of a long math expression. Now I'm running through a while-loop intill it finds a whitespace. So that I know where the number stops.

while(Expression[i] != '')

Dont know should I read the number into another int array?Which creates new problems.The idea is that I want to use the 25 as a whole number and not 2 and 5 seperately because that will give an incorrect answer.

Is there another way of doing this? There must be a simpler way...I think I just forgot about something

Any help will be appreciated :)

Thanks!

Recommended Answers

All 6 Replies

Just read it into an integer.

std::string operation;
int operand;

cin >> operation >> operand;

// verify cin is good (i.e. no input errors occured

//lookup operation and call it with operand.

Thanks for the response!

But the thing is I'm getting the whole expression AND operand all in one string together! I can't split it up as an interger and string :/ What I mean with the split is that I can't read it in as segments like your example!

I need to tokenize it from the string and then do the functions from there.

Use a std::istringstream to extract the tokens from the string.
http://www.artima.com/cppsource/streamstrings3.html

#include <iostream>
#include <string>
#include <sstream>
#include <cmath>

inline void error() { std::cerr << "error in input\n" ; }

int main()
{
    std::string input ;
    while( std::cout << "? " && std::getline( std::cin, input ) )
    {
        std::istringstream stm(input) ;
        std::string function_name ;
        if( stm >> function_name )
        {
            if( function_name == "sqrt" )
            {
                double arg ;
                if( stm >> arg ) 
                    std::cout << "sqrt(" << arg << ") == " 
                                         << std::sqrt(arg) << '\n' ;
                else error() ;
            }

            else if( function_name == "pow" )
            {
                double arg1, arg2 ;
                if( stm >> arg1 >> arg2 ) 
                    std::cout << "pow(" << arg1 << ',' << arg2 << ") == "
                              << std::pow(arg1,arg2) << '\n' ;
                else error() ;
            }

            // else if etc ...

            else error() ;
        }
        else error() ;
    }
}

What you are trying to do is not trivial. You need to look at the equation as a whole, not search for small parts and process piecemeal.

It might be useful to study up on Reverse Polish notation to break up the equation into individual atoms.

You can use a structure to store the atoms which would contain

1) type (whether this is an operator or a number)
Values here could be 1 for a number, -1 for an operator.

2) value (a number from the equation or a code for the operation)
Each operator (+ - * sqrt mod) has a unique value. Consider that there are operators that handle two values (* - / mod) and others that handle one (- sqrt pow).

Alternatively:

Copy the equation. 
Search for the thing you want to process.
Get the values to process.
Do the subequation.
Replace that section with the answer.  

Everything stays in the string. So if you have
10 * sqrt 25 + 3
and you want to process the sqrt, grab the sqrt 25 and replace it:

10 * sqrt 25 + 3 -- becomes
10 *       5 + 3

Note the spacing. The entire section sqrt 25 gets replaced by SPACEs and a 5.

Thanks the responses it helped a lot!

I just want to ask WaltP something.

Do you suggest maybe using a stack especially when you say 'replace' and just insert the answer back in the equation? And you can then more easily get lets say the 'left' and 'right' integers of the '+' sign?

Or did you just use the string?

The thing is the equation is not being inputted, I'm receiving it from a predefined string in my main.c

Just a last thing - I did look at the reverse polish notation. I'm just looking into it on how I'm going test the precedence. Because then the whole thing changes again and the order in which it executes.

Thanks for the help thus far!

you: Do you suggest maybe using a stack especially when you say 'replace' and just insert the answer back in the equation? And you can then more easily get lets say the 'left' and 'right' integers of the '+' sign?

Or did you just use the string?

As I said:

me: Everything stays in the string.

Reread my post.

you: The thing is the equation is not being inputted, I'm receiving it from a predefined string in my main.c

Then you'll have to copy the source string to another string so it can be modified. This is a good thing.

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.