Binary Calculator

Unlike my first post, this is NOT a school assignment, this is something that was on last year's midterm that my teacher gave us to study from for this years test (if that makes sense).

Design, code and test a C++ program that will function as a simple binary calculator. Binary expressions will be read from standard input. Each expression consists of a sequence of binary numbers separated by operators - and terminated with an '='. A typical program run might look like ...

$./a.out
Enter a binary expression (= to evaluate): 1101
+10
/ 101 =

The result is 11 in base 2 = 3

The input expression will contain at least 1 binary number followed by the terminating =, as shown here ...

$./a.out
Enter a binary expression (= to evaluate): 101=

The result is 101 in base 2 = 5


I don't really know what I'm doing here, so any help would be appreciated. In addition to an explanation of what to do, I'm hoping for somebody to post the completed code, or at least fragments of it to help me understand.

Thanks in advance,

newuserkang
Ok, so what do you have so far? Don't say that you don't know where to start because at the very least you can take input from the user and do the calculations. You can evaluate his input to see if it is a number or a symbol and what that symbol indicates etc. So you see you do in fact know what your first step is.
you need a 'state' variable. possible values NONE, ADD, SUBTRACT, MULTIPLY, DIVIDE
you also need a 'result' integer variable.
in a loop
    wait for a binary number.
        I believe iomanip doesn't have a 'bin' basefield flag.
        You will have to get a string and then convert it to an integer. Store it in variable 'temp'
        if 'state' is other than NONE, do the appropriate calculation with 'result' and 'temp'
        if 'state' is NONE, 'result' = 'temp'
    
    wait for a symbol.
        set 'state' to appropriate value, based on what user enters.
        if user entered '=', print 'result'.

I think that should work..
Last edited on
hamsterman, can you come up with the C++ code to show me what you mean, I didn't understand a lot of that, sorry :).

My midterm exam is today at 5pm PST (which is in 6 hours) and my instructor told us that this question will almost certainly be replicated on our exam (with a few small variations of course).

If you could post the code for me to reference to before my exam, it would be greatly appreciated, I'm not too bright when it comes to programming.

Thanks a bunch,

newuserkang
I doubt anyone here will do that for you. It is highty likely you will learn nothing if you don't write it yourself.
All I know is that I have a terrible teacher and I have an exam in 5 and 1/2 hours, and if I dont know how to wirte this kind of program, theres a good chance I'm going to lose out on a 20-30 mark question. It isnt as though I am asking for somebody to give me a homework answer, I need serious help for this exam or I will fail it. I don't need to magically learn how to do it right now, I just need to know how to write it. Of course, I will put a serious effort into learning how this type of program works after this exam, since it will most certainly be on the final exam as well.

Im asking for anybody that knows how to write this program, please help me pass this exam, I can't afford to fail a test worth 30% of my final grade.
Indeed, as Zhuge said, giving you the code would have little benefit.
If there's a specific thing that you don't understand, we could explain it though.
Try to simplify the problem. Can you make a normal calculator? Can you convert an integer to a string representing a binary number or vice versa?
If your intention is to get a good grade and then come out to work by "smoking" your interviewers to secure a job then I don't think anyone in this forum is ready to help you. If you really don't like IT then in the first place you should not have opt to study IT or computer science equivalent.

Of cuz there are other reasons like you cannot go to the faculty of your first choice and got to settle for other faculties but that is besides the point. Such things happen in my times (90's era)and I guess this phenomenon isn't going to disappear in the near future too.

How so often ppl just want to get the certificate and not really learning anything ? They intend to use the certificate to secure a better advantage in job interviews ? That is deceiving yourselves and the employers-to-be but if that is how you intend to live out your life, by all means and I respect your decision.
In your problem, there is two kind of problem.
1. If the program process the input from the left to right. Every operator has the same priority. You should just convert the binary to decimal and operate them one by one.
2. If every operator has different priority. It's more complex, you should use stack to process this problem

You can do it yourself firstly, then paste your code here.
Im not looking as pursuing a career in this field, I only took this course because I needed one more course to qualify for Student Loans, and I have a strong math background so I figured it would be easy. Clearly, i was wrong lol
You are begging for unjust help. No matter how bad your professor, you could have done the work already to get you enough knowledge to answer this.

Fortunately, the example does not use any operator precedence, so you really only need to read and calculate as you go:

1101 + 10 / 101 =

Start by reading your initial number: "1101".
Read the next operator symbol. It is a '+' so we need to read the addend: "10".
Perform the addition. Your current number is (1101 + 10) --> 1111.
Read the next operator symbol. It is a '/' so we need to read the divisor: "101".
Perform the division. Your current number is (1111 / 101) --> 11.
Read the next operator symbol. It is an '=', so we are done. Read the newline (cin.ignore( numeric_limits <streamsize> ::max(), '\n' );) and print the results.

The only trick will be reading a binary number from cin instead of an octal, decimal, or hexadecimal. Since there is no special manipulator for that, you must write your own method. For maximum simplicity, I suggest just writing a little function to do it.

Good luck!
Thank you Duoas, i realize that i was asking for an answer to a solution which isnt allowed on these forums, but I also didnt understand how to do it. I have since read the C++ tutorials on this website on functions and I now have, at the very least, a basic understanding of functions. This question was on my midterm exam, so it is no use to me to find the solution, but I will try your suggestion and see where i get.

Thanks!

newuserkang
Perhaps this method could involve boolean arrays.
Perhaps this method could involve boolean arrays.
Not really. You could write all operators for arrays of booleans, but that would be slow, painful and useless. All you have to do is parse a string into an integer.
It shouldnt involve Arrays, since this question was on an exam before we had been introduced to them
Well, parsing binary numbers will be a little harder without arrays:
Read an int (as if it was decimal), extract the n'th digit (do you know how?), multiply it by 2n-1 and add to the reslut.
You don't actually need to read it as decimal. Just read it one digit at a time and shift and set each iteration until you hit a value other than '0' or '1'. Use istream::peek() to see what the next character is.
Topic archived. No new replies allowed.