K-expression calculator

I am new to C++ and my teacher told me to write a program about a"K-expression calculator". That means:

Sample Input 1:
3 + 4
Sample Output 1:
7

Sample Input 2:
6.9 * 7
Sample Output 2:
48.300

Sample Input 3:
9 / 6
Sample Output 3:
1.500

Please help me with my code. Thanks a lot! :)

I don't know what a K-expression is, but it looks like you just need to write a program that reads a double, an operator (char), and another double, performs the operation on them, and prints the output.

You need to try yourself before you'll get much more help. Post your code if you get stuck.

Good luck!
Sorry I made a mistake. Cross out Sample 2. The question requires me to input 2 positive integers. But I don't know about how to determine whether a number is an integer or double. Please help me with this part. Thanks! :)
@ Sakudo
Do not have a programming book? Many advanced guys, here, recommend this book: C++ Primer 5th Edition. I have it...It's really, amazing!
Last edited on

But I don't know about how to determine whether a number is an integer or double.


Simply put, an 'int' variable is a whole number of type -1, 0 ,1 ,2. A double is a double precision floating number (meaning that it can hold decimals with great precision) I.E 2.1234512512.

Keep in mind that it is a simple explanation. The two have a set amount that they can maximally store. Since some of the results in the sample use decimals, I would suggest using a double.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int main(){
    double num1,num2;
    double result=0;
    char operation;
    cout<<"Enter operation (+, -, *, /): ";
    cin>>operation;
    switch(operation){

    case '+':
        cout<<"Enter first number: ";
        cin>>num1;
        cout<<"Enter second number: ";
        cin>>num2;
        result=num1+num2;
        break;
    case '-':
        cout<<"Enter first number: ";
        cin>>num1;
        cout<<"Enter second number: ";
        cin>>num2;
        result=num1-num2;
        break;
    case '*':
        cout<<"Enter first number: ";
        cin>>num1;
        cout<<"Enter second number: ";
        cin>>num2;
        result=num1*num2;
        break;
    case '/':
        cout<<"Enter first number: ";
        cin>>num1;
        cout<<"Enter second number: ";
        cin>>num2;
        result=num1/num2;
        break;
    default:
        cout<<"That is not a valid operator. ";
    }
    cout<<"The result is: " <<result;
    return 0;
}
@Group of Eight:
meaning that it can hold decimals with great precision


to be precise, it doesn't hold decimals, it holds double floating-point number.
actually, any built in type can hold any of the following:
a decimal value.
a hex value.
an octal value.

that's because data is actually stored in binary, but the language handles conversion between these bases.
Hey there, Group of Eight. Nice way to do the OP's homework for him!
Actually I found a way that is to use an integer to store the double result. If the integer is the same with the double result, then the double result is an integer.
Group of Eight, you saw for whom you worked?
Last edited on
Also, @condor Thanks for recommending me a reference book! :)
Sakudo, you're welcome anytime for these things! Cheerio!
Last edited on
What's wrong with posting code to help someone? In order to be successful in their course they would have to understand what I coded there, so, in reality I'm just presenting him with the core idea of what they need to do.
@Group of Eight
i don't know if you're naive or just acting naive. no offence.
where i come from, if a professor wants a homework done, he never asks the student about it, he assumes that the student solved it himself and understands every word in it.
the case isn't always that simple, there are some student who steal solutions in various ways and present it to the professor, and get the marks for free.
in exam, they pass because they know how to cheat every thing.
people CAN pass a course they don't understand.

anyway, it's your choice to solve the whole homework for someone.

Sakudo please don't misunderstand me, i wasn't talking about you, i was talking in general.
@Group of Eight:

There's an old expression.

"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."


In programming, work later in the course is often build on top of lessons learned in previous assignments. By doing the work for him this time, he can just turn the code in as-is without having learned anything from the assignment, and then he'll be ill-prepared for the next assignment where it's assumed he learned certain concepts from this one.

You should not give out full solutions, but rather help guide the person to finding the solution themselves.


You're right that giving code can help... but you should give example code that isn't exactly on point. Something they can't directly copy. That way they have to understand it and adapt it to what they're doing.
Last edited on
Topic archived. No new replies allowed.