Test Out My Calculator?

Hi - I've made a calculator and just recently solved a HUGE problem with it. I want just whoever to try out the calculator and give me their thoughts. The problem was with the feature 'a' which was supposed to represent your last answer. However, people wanted to be able to type in a number and press enter and have that be their last answer which they could use for 'a'. The problem was with the way I had coded the calculator and it was a huge struggle but then this function

1
2
3
4
5
6
7
int combine(int a, int b)
{
	int times = 1; 
	while (times <= b)
		times *= 10; 
	return a * times + b;
}


came into my life and allowed me to solve that problem. Now - My code is MESSY, you'll hate looking at the code and you'll hate me for writing it. But please don't judge me on the code - Have only been learning C++ for a month now and I started coding this on day 2. Without further speeches - here is the code for the calculator:

EDIT : THE CODE WAS TOO LONG FOR THIS SITE - https://pastebin.com/1bSXHVjy

^ The Code Is On pastebin.com In The Link - Copy It And Run It In Your Personal Compiler Or The Website's Compiler - http://cpp.sh/ .


EDIT : At The Time Of This Latest Edit - I have uploaded the latest link to my calculator code. I tested it extensively - and hope there aren't any inputs with garbage values I haven't made error codes for. Thanks in advance to anyone who will test out this calculator!


Thanks !
Last edited on
I guess no one really wants to try out the calculator. Well, I'll be around whenever someone is curious enough to try it ! :)
1. It's too long. This much functionality fits in half as much code or so.
2. No parentheses.
3. I see a few gotos. Naughty naughty!
1. Yea - I was gonna re-code (especially since I learned about string functions) but I'm a lazy coder !

2. Since it only handles one equation at a time, those aren't too important unless dealing with negatives. And I don't think adding that functionality in would be a simple task with the way it's coded, sadly..

3. What can I say, I like to live dangerously !
I might make it my goal to code a more efficient calculator.

Thanks helios for testing out the calculator - I appreciate your feedback !
There are expressions that can't be written without parentheses, such as a/(b+c).
Yea, but this calculator can't handle two operators. To do that, you'd be forced to add b and c then divide a by the sum in the next equation. If I get to making another calculator - I'll make sure to allow for several operations within a single equation - which then would require parentheses. Thanks !
Oh. Well then this is way, way too long. It should be four or five times shorter.
Yea, I think this happened since at the beginning this was a very simple calculator that I tried to incorporate more features into.
Hey Zapshe,
Cool job for a start ^_^.

I tried "3 % 5" and had the answer "not applicatble".
This answer is wrong :o) since the result is 3 ;o).

No critics ;o).
I took a look at your code and your main loop is too long.
I suggest you take the habit from now on to call a function anytime you want to do something :o) and even before you code that function. You'll see that your code will tend to be much more readable even if it's still get long.

You can for example, symbolizes your process like this :
1
2
3
4
5
6
7
8
for(;;)
{
	line = readInput();
	command = parse(line);
	auto const& result = compute(command);
	store(result);
	dump();
}

This way, you can always complicated thing in each step but your process stays clean :oD~ and very readable.

Also, get rid of the std::cin etc.
Encapsulate all of these calls into functions.
Maybe a time will come, you'll have to change the library you use... code like that is a hell to maintain :o).

No critics ^_^... just some habits you can acquire from now on that will make your code better instantly.

By the way, what was the reason you wrote this code ?
Was it an exercice ?
About calculators, you may want to try a postfix calcultor... because you postfix every operation, the "syntax" become very very easy to parse and you can compute any formula the most simple way possible ^_^.
The postfix style has been also called the reverse polish...
Whay you did is a copy of the way we write the operations as mathematical formula and this way is very difficult to parse compare to the postfix.
But postfix has a "drawback", you must have a stack for the operands :oP
postfix is very funny to try ;o)
Last edited on
Hi punksheep, sorry I didn't see your post earlier. I wrote this calculator as I was learning the C++ basic in order to constantly incorporate what I learned into an actual program. The reason 3 % 5 got inapplicable is because it's looking for remainder. The '%' sign may mean something different in actual mathematics, but the remainder of 3/5 is not applicable because 3 cannot be divided evenly by 5.

Thanks for the feedback, I'm trying to grow as a programmer !
Topic archived. No new replies allowed.