The C calculator program allows the user to enter an equation that consists of numerical values (digits) and arithmetic operators (+, -, *, /) in a valid format and the “=” sign to terminate the equation so as for the program to compute for an answer. The program will examine the equation and reject an invalid input by printing “ERROR!” The Backspace key can be used to correct input errors. The calculator must consider the precedence of the arithmetic operators when computing the equation. The answer is displayed right after the ‘=’ sign. Note that the numerical result is printed with an accuracy of decimal places only if the answer is a decimal number. The program repeats the calculating service until an empty equation is entered.
Please help me with it, I don't know how to write it. Thanks in advance.
Wow, wow. That's not a trivial project, especially if you can't use a lexer/parser combination.
Start by using scanf to get a string that's terminated with an = sign. Check if it's empty or not, and if it is... quit. Then, try splitting up the string into smaller strings that contain either a number or an arithmetic operator and nothing more. strtok might work for this, but be careful because it eats up your separators.
That should get you started and maybe even give you an idea of what to do next. Let us know if you have any further problems.
I had a VB class last year and as a project in there I made a calculator. I despise VB, hate using it, hate the idea of it. But, thats besides the point. Logic is logic. Anyways, I had the idea at first of doing what you stated up there, then realized that with my allotted time there was just no way it was going to happen.
I had an idea on accepting input by a character by character basis, and storing it in container. Like an array of char in c for example. I was then going to iterate through this container, and getting the location of all operators (+,-,*,/,=). Then from there it was going to be a lot of condition checking and odd iterating through this container. I hadn't worked out any validation logic, mostly because it was VB and I told the user what they could and couldn't enter.
But yea, I don't know if C has a built in anything for this problem or not, this is just some logic I had devised when I had a similar project.