![]() |
my 1st programming goal was to write a four-function calculator in C, C++, Java, and Python. here are the first three.
in Java: :
//import the java input/output libraryin C: :
in C++: :
#include <iostream> |
Hmmm...in the C version, after you choose to clear the number, it asks you to enter a new value, but in the C++ one, it clears it to '0' and doesn't let you give a new value for it.
|
not a problem, just like to pick out silly things :P
|
Some nice programs... How much you want to bet that the Python program will be one of the shortest ones :P
Beeg |
That's a lot of bulk for not much functionality. For example, would you really want to fire this program up whenever you needed a calculator? Probably not, because it only works with two numbers and takes too much of the user's effort to handle even that. Of course, writing a parser that allows one to type expressions isn't a trivial task, which is why the Python program will be simplicity itself (just call eval on the input string).
If I might offer a suggestion in C: :
#include <ctype.h>This gives rise to a powerful, if somewhat cryptic (at first), notation. For example, to calculate : :
7 - 4:
7 4 - .:
( 7 - 4 ) * 5:
7 4 - 5 * .:
7 4 - |
hehe, when I look at these codes I just think of how much I like pythons simple commands... phew
Beeg |
>when I look at these codes I just think of how much I like pythons simple commands...
Well, that is the point of high level languages. The compiler/interpreter does most of the tedious work for you so that you can focus on solving new problems rather than old ones. Of course, for this new power you give up a certain amount of control and flexibility. Writing a clone in Python of the program I gave isn't as short and sweet as you might think. To get the same functionality you still need to work a bit. :) |
interesting solution
i wrote these beginner programs for a four-function calculator awhile back and then a professional programmer wrote a much more complex, yet much more functional solution to the problem. interesting to look at the code. oh, and a good project for beginners because you can incorporate OOP into this type of program so you can practice with classes and functions and hell, you could go buck-wild with pointers and references and arrays and what not. anyway, if you're just starting out, a simple calculator is a good project. (i have given up on the python calculator, i have too much to study with C and C++) now i'm searching for a good test of my now-intermediate C-style skills...any suggestions?
|
| All times are GMT -5. The time now is 2:07 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC