Calculator Source Code |
| |
|
Formerly theone759. Activity
|
|
Professional Coder Posts: 351 Topics: 53
|
|
|
March 01, 2009, 11:10:33 AM
|
I found this on dreamincode.net, maybe it will be useful to someone #include <cstdlib> #include <iostream> using namespace std; int main() { double num; double num2; char choice; for (;;){ do { cout<<"Welcome to thejoshcalculator. V1.5\n"; cout<<"Please choose an option by entering the number, press q to quit\n"; cout<<"1 - Addition\n"; cout<<"2 - Subtraction\n"; cout<<"3 - Division\n"; cout<<"4 - Multiplication\n"; cout<<"5 - Help\n"; cout<<"6 - About This Program\n"; cout<<"7 - Updates to this program\n"; cin>>choice; } while ( choice < '1' || choice > '7' && choice != 'q'); if (choice == 'q') break; switch (choice) { case '1': cout<<"Please enter a number\n"; cin>>num; cout<<"Another number to be added\n"; cin>>num2; cout<<num + num2; cout<<"\n"; break; case '2': cout<<"Please enter a number\n"; cin>>num; cout<<"Another number to be subtracted\n"; cin>>num2; cout<<num - num2; cout<<"\n"; break; case '3': cout<<"Please enter a number\n"; cin>>num; cout<<"Another one to be divided\n"; cin>>num2; cout<<num / num2; cout<<"\n"; break; case '4': cout<<"Please enter a number\n"; cin>>num; cout<<"Another one to be multiplied\n"; cin>>num2; cout<<num * num2; cout<<"\n"; break; case '5': cout<<"This is a simple calculator made by me - Josh.\n"; cout<<"To select an option, type the number next to the option and press enter\n"; cout<<"E.G. for division, you would type 3 and press enter.\n"; cout<<"\n"; break; case '6': cout<<"thejoshcalculator, made by Joshua Griggs - Copyright 2007. :)\n"; cout<<"Feedback would be nice - joshieboy06@hotmail.com also, what programmes\n"; cout<<"do people need. Please give me ideas for programs. Bye!!\n"; cout<<"\n"; break; case '7': cout<<"Updates include: -double variable instead of int, so that decimals can be used.\n"; cout<<" -do while loop so that you can exit the program yourself\n"; cout<<"\n"; break; default: cout<<"That is not an option"; } } return 0;
}
|
|
| |
|
|
|
Code Guru Gender:  Posts: 1399 Topics: 85
|
|
|
March 02, 2009, 05:51:23 AM
|
|
There is quite a lot of code repetition there, which is bad style. This code could have been written much more succinctly. For instance, if you want the message to read "type a digit" instead of "Please enter a number" you would have to change the code in multiple places... of course not a major issue for something simple like this but in the real world this can cause a real headache.
|
|
| |
|
Formerly theone759. Activity
|
|
Professional Coder Posts: 351 Topics: 53
|
|
|
March 02, 2009, 07:31:04 AM
|
|
Would you mind "fixing" it up so I can learn ways not to do things? This is C++
|
|
| |
|
|
|
Code Guru Gender:  Posts: 1399 Topics: 85
|
|
|
March 02, 2009, 07:41:34 AM
|
This is the way I would do it, I am not a C++ programmer so there may be better C++ ways. Also note how the origional author declares quite a few variables outside the scope they are required in... again I do not like this style particularly. #include <cstdlib> #include <iostream> using namespace std; int main() { while (true) { char choice; do { cout<<"Welcome to thejoshcalculator. V1.5\n"; cout<<"Please choose an option by entering the number, press q to quit\n"; cout<<"1 - Addition\n"; cout<<"2 - Subtraction\n"; cout<<"3 - Division\n"; cout<<"4 - Multiplication\n"; cout<<"5 - Help\n"; cout<<"6 - About This Program\n"; cout<<"7 - Updates to this program\n"; cin>>choice; } while ( choice < '1' || choice > '7' && choice != 'q'); if (choice == 'q') break; if (choice == '1' || choice == '2' || choice=='3' || choice =='4') { double num; double num2; cout<<"Please enter a number\n"; cin>>num; cout<<"Another number\n"; cin>>num2; double theresult; if (choice=='1') theresult=num+num2; if (choice=='2') theresult=num-num2; if (choice=='3') theresult=num/num2; if (choice=='4') theresult=num*num2; cout<<theresult<<"\n"; } if (choice=='5') { cout<<"This is a simple calculator made by me - Josh.\n"; cout<<"To select an option, type the number next to the option and press enter\n"; cout<<"E.G. for division, you would type 3 and press enter.\n\n"; } } return 0; }
« Last Edit: March 02, 2009, 07:44:16 AM by polas »
|
|
| |
|
Formerly theone759. Activity
|
|
Professional Coder Posts: 351 Topics: 53
|
|
|
March 02, 2009, 08:28:03 AM
|
|
Ok so you basically just condensed the code, and removed unnnessacry things?
|
|
| |
|
|
|
Code Guru Gender:  Posts: 1399 Topics: 85
|
|
|
March 02, 2009, 08:49:33 AM
|
Reading code is like reading a page of English - it can look messy with poor style and obfusicated meaning, or it can be written well with few errors. I did a couple of things: 1) Identified that options 1 - 4 are very similar (really only one line is different per option), so restructured them so that they could share the same code as much as possible, removing repetion and also cutting down the code size 2) Variables are declared in the scope that they are required rather than global to the function (cf. num, num2 and choice - see how their declarations are in a different place in my code) 3) I think while(true) is more readable than for (;  - just my opinion, at the end of the day for is syntactic sugar for while anyway. I did not bother to include all the option 6 and 7 crap - easy enough to add in. Could have cut the code down further by combining many of the couts onto one line (e.g. the menu), I would have taken his approach but it would make it slightly less clear for learning purposes. Bear in mind, on average, 70% of effort spend creating software is in the maintenance phase. Whenever you write code, it really is essential that it is written clearly. I am currently working on porting and interfacing to, a 50000 line parallel simulation package written in C, thankfully for us it is written quite well and the code is clear(ish), but some of the less good coding styles adopted have caused real problems for us. Nick
|
|
| |
|
Formerly theone759. Activity
|
|
Professional Coder Posts: 351 Topics: 53
|
|
|
March 02, 2009, 12:14:57 PM
|
|
Thanks nick for helping, going to try and learn some more!
|
|
| |
|
|
|
Code Guru Gender:  Posts: 1399 Topics: 85
|
|
|
March 02, 2009, 12:42:20 PM
|
|
Np, some people argue that programming is an art rather than a skill - I dunno
|
|
| |
|
Formerly theone759. Activity
|
|
Professional Coder Posts: 351 Topics: 53
|
|
|
March 02, 2009, 03:21:04 PM
|
|
I can see it being a art, and a skill too. That would be something that sticks in your head for a while...
|
|
| |
|
|
|
New Poster Posts: 1 Topics: 0
|
|
|
February 02, 2010, 05:07:05 AM
|
Hey this is the calculator code of C language #include <stdio. h> #include <conio. h> main() { float n1,n2,s,d,p,q; clrscr(); printf("\n Enter two nos"); scanf("%f%f"&n1,&n2); s=n1+n2 d=n1-n2; p=n1*n2; q=n1/n2; printf("\n Sum %f",s); printf("\n Difference %f",d); printf("\n Product %f",p); printf("\n Quotient %f",q); getch(); return(0); }
« Last Edit: July 09, 2011, 08:34:49 PM by Admin »
|
|
| |
|
|
|
Code Guru
Location: India
Gender:  Posts: 1387 Topics: 105
|
|
|
February 02, 2010, 08:24:51 AM
|
|
riya199002 thanks for posting the code.
Code is good for simple calculations.
|
|
| |
|
|
|
New Coder Posts: 27 Topics: 5
|
|
|
December 23, 2010, 07:34:44 PM
|
|
Nice code for a calculator. Can I use it as an exercise on my school teaching? I usually small programmes as exercises.
|
|
| |
|
|
|
New Poster Posts: 11 Topics: 0
|
|
|
January 07, 2011, 04:01:31 AM
|
|
Thus I would, I am not a C + +, so it may be better ways C + +. Also note how the original author declares some variables that are not covered, it must be .. again, I do not like this particular style.
|
|
| |
|
|
|
New Coder Posts: 20 Topics: 2
|
|
|
April 13, 2011, 02:16:33 PM
|
|
Also note how the original writer declares some variables that are not covered, it must be again, I do not like this item style.
|
|
| |
|
|
|
New Poster Posts: 3 Topics: 0
|
|
|
April 16, 2011, 02:00:57 AM
|
Program which performs addition, subtraction, multiplication and subtraction. #include <iostream> using namespace std;
// input function void Input (float &x, float &y);
float a=1. 0, b=1. 0, result; char operation;
int main () { cout << "Program which performs addition, subtraction, multiplication and subtraction. \n\n"; cout << "Please input calculation operation (eg. 1 + 2): \n"; cin >> a >> operation >> b; Input (a,b);
cout << "The answer is: " << result << endl; system ("pause"); return 0; }
void Input (float &x, float &y) { a = x; b = y;
switch (operation) { case '+': result = x + y; break;
case '-': result = x - y; break;
case '*': result = x * y; break;
case '/': result = x / y; break;
default: cout << "Improper operation. Please input a correct calculation operation: \n"; cin >> a >> operation >> b; Input (a, b); } }
« Last Edit: July 09, 2011, 08:35:28 PM by Admin »
|
|
| |
|
|
|
New Coder Posts: 25 Topics: 2
|
|
|
April 26, 2011, 12:45:49 PM
|
|
This encipher could individual been scrivened some much succinctly. For instance, if you necessary the communication to indicate "write a appendage" instead of "Gratify save a symbol" you would tally to modify the codification in bigeminal places.
|
|
| |
|
|
|
Professional Coder Real name: Odusee
Location: Australia
Gender:  Posts: 458 Topics: 47
|
|
|
June 07, 2011, 12:05:14 AM
|
There are different style to make your own calculator. I have seen two of the best calculator structures here. Very nice. One thing about coding is to check line by line the execution and making the code clean and readable  Nice posts 
|
|
| |
|
|
|
Professional Coder Gender:  Posts: 242 Topics: 3
|
|
|
June 09, 2011, 06:27:44 AM
|
Np, some people argue that programming is an art rather than a skill - I dunno
i do agree with this one.. btw, i prefer polas' code..
|
|
| |
|
|
|
Professional Coder Posts: 229 Topics: 7
|
|
|
July 26, 2011, 11:24:59 PM
|
|
Thanks for this code, its nice sharing of post. Thanks.
|
|
| |
|
|
|
New Poster Posts: 5 Topics: 0
|
|
|
July 29, 2011, 09:48:15 AM
|
|
this code is very halpful especially the language in which this code is written is easy to understand
|
|
| |