Sunday, June 15, 2014

My First Dive at Making a C++ Calculator

Ever since I started C++, I've wanted to make my own calculator in C++. One that allows you to enter expressions and can use operator precedence (BOMBDAS) to arrive at a correct answer. Over the past few days, I've been working on a calculator called CPP-Calc. It's a start, I guess. The code looks kind of ugly at best, but it's easy to compile because everything is in a single file called main.cpp. Maybe that's what makes ti so ugly, but I'll eventually get around to correcting that :). Also, as users on Code Review pointed out, there are several problems with my code.
It works, but its design is a bit lack luster. Firstly, it seems to be nowhere near as simple as it could have been (I've been told I could have used a recursive descent parser or the shunting yard algorithm) and the code just seems jumbled. I think the algorithm I used is okay, though. It allows me to make use of stacks, vectors, string methods (eg. replace() ), pointers and doubles. I'll need to implement a better design though. Some member variables aren't private, some variables are unused and it just looks cramped. 
I ought to point out the problems I'm facing with the use of doubles. Being floating point numbers, doubles will only allow the calculator to give the a certain degree of accuracy up to a point. Maybe I'll be able to use some sort of other datatype, but for now, I'll concentrate on the design.
Greetings to all who have read the article and I welcome your reviews on my program :
http://codereview.stackexchange.com/questions/54273/simple-c-calculator-which-follows-bomdas-rules

If you'd like to download/examine my program more closely, use this: Download

No comments:

Post a Comment