Soooo I essentially need to make a program to evaluate any exspression that is plugged in to it, how would I go about starting a program like that ?

I know how to evaluate normal exspressions including + - * / but what if a user wants to do a certain operation that is not pre determined by the programmer...

Please help me get started,
Thank you
Exo1337

Can you explain more about what you want to do?
How will the user be inputting the expression? Will it be a string? a set of integers? a set of floating point numbers?

Here's some reading for you:
http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm

There are many resources available to help you do the research to get this done. Google is your friend. Recursive decent is one popular way.

What do you mean by "a certain operation that is not pre determined by the programmer"? If it's not pre-determined by the programmer, how do you expect it to be evaluated?

From text: Write a program that mimic's a calculator, the program should take as input 2 integers and the operation to be performed. it should then output the numbers the operator and the result. for division if the denominator is 0 output an appropriate message.

OK, that information would have been helpful in the original post. You don't need recursive decent (I suspect that would very much impress your instructor, though).

You should really make an attempt at this and ask specific questions because I have no idea what level you're on and I don't want to do it for you.

Basically, get your input and then for each possible operation, evaluate the two integers with that operator.

I will try to do it for myself, in C++ we just covered If, and IF else statements in this chapter, so i appreciate the input and would refer you dont do it for me ^^; I just need a general direction to head in after i the Inputs and how would you represent a mathematical sign in an input?

cin >> num1 >> num2 >> endl; for numbers
what would i do for a + - * or /

endl doesn't go in cin statements.

int num1, num2;
char op;

You would input the operator the same as with an int.

Since you've learned if statements, use these for each operator and do the calculation within it. Remember to account for division by 0.

Thanks to all of you for your help i might post on this topic in another 10 or so minutes if i get stuck, off to program it :P

#include <iostream>

using namespace std;

int main()
{
    int num1, num2;
    char op;

    cout << "Hello, please input a mathematical exspression for me to evaluate!";
    cin >> num1 >> op >> num2;

    if (num1 + num2)
        cout << num1 + num2 << endl;
    else if (num1 - num2)
        cout << num1 - num2 << endl;
    else if (num1 * num2)
        cout << num1 * num2 << endl;
    else if (num1 / num2)
        cout << num1 / num2 << endl;

    return 0;
}

Ok so its just adding everything together not evaluating every other expression and im not sure what im supposed to do for 0

id assume it would look like
if (num1 / 0)
Error msg but, would i have to do something special for 0 like say if num2 = 0?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I think you're supposed to determine what op is to see what calculation you'd like. And when it comes to division, you'd test for div-by-0 and not do such if that were the case. So, yes, if ( num2 == 0 ) .

Your conditional should determine which operator was entered, not evaluate the expression, which is what you are doing in the if statements.

you just confused me could you break it down for me a little more ?

c++ doesn't pattern match in if statements (conditionals).

So if (num1 + num2) doesn't behave as you are expecting it to. What that is doing is adding num1 and num2 and checking the value of that addition for true or false.

You need to use that statement to check to see which operator was entered. op == '+'

I feel ridiculose asking for more help but i cant get the / by 0 to function correctly to save my life.

else if (op == '/' && num2 == 0)
else if (num2 == 0)
else if (num1 '/' num2 == 0)

Where am i going wrong here?

Everything else works? Post everything.

The first line looks good. That should be before you test for op == '/'.

#include <iostream>

using namespace std;

int main()
{
    int num1, num2;
    char op;

    cout << "Hello, please input a mathematical exspression for me to evaluate!";
    cin >> num1 >> op >> num2;

    if (op == '+')
        cout << num1 + num2 << endl;
    else if (op == '-')
        cout << num1 - num2 << endl;
    else if (op == '*')
        cout << num1 * num2 << endl;
    else if (op == '/')
        cout << num1 / num2 << endl;
    TEST HERE
    return 0;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Thank you so much it works just fine now, I'm sure I will see you again on this forum. Thanks again for all the help.

Shoot totally forgot i need it to display the awnser as

X + Y = Z

num1 op num2 = result

how would i do that?

This article has been dead for over six months. Start a new discussion instead.