Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

Calculator Source Code

 
webmaster forum
Thomas  Offline
Formerly theone759.
Activity
0%
 
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

Code:
#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;


}
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
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.

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
Thomas  Offline
Formerly theone759.
Activity
0%
 
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++
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
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.

Code:
#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 »

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
Thomas  Offline
Formerly theone759.
Activity
0%
 
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?
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
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 (;Wink - 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

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
Thomas  Offline
Formerly theone759.
Activity
0%
 
Professional Coder
Posts: 351
Topics: 53
March 02, 2009, 12:14:57 PM

Thanks nick for helping, going to try and learn some more!
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
March 02, 2009, 12:42:20 PM

Np, some people argue that programming is an art rather than a skill - I dunno

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
Thomas  Offline
Formerly theone759.
Activity
0%
 
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...
 
webmaster forum
Activity
0%
 
New Poster
Posts: 1
Topics: 0
February 02, 2010, 05:07:05 AM

Hey this is the calculator code of C language

Code:
#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 »
 
webmaster forum
Admin  Offline
*
 
Code Guru
Location: India
Gender: Male
Posts: 1387
Topics: 105
NaviBuster NaviBuster
WWW
February 02, 2010, 08:24:51 AM

riya199002 thanks for posting the code.

Code is good for simple calculations.
 
webmaster forum
isidoriu  Offline
Activity
0%
 
New Coder
Posts: 27
Topics: 5
WWW
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.

Pc Service eSecretary eLearning Lessons via Internet Marketing Promotion English Lessons IT Lessons
 
webmaster forum
stiffen88  Offline
Activity
0%
 
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.

 
webmaster forum
Activity
0%
 
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.

cabinet doors | games | de-duplication software
 
webmaster forum
Activity
0%
 
New Poster
Posts: 3
Topics: 0
April 16, 2011, 02:00:57 AM

Program which performs addition, subtraction, multiplication and subtraction.

Code:

#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 »
 
webmaster forum
Activity
0%
 
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.

Karaoke Microphone | speed bag platform | tennis net
 
webmaster forum
Activity
0%
 
Professional Coder
Real name: Odusee
Location: Australia
Gender: Female
Posts: 458
Topics: 47
WWW
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 Cheesy Nice posts Cheesy

Find it all at ONE Address~ Check it Out!
 
webmaster forum
Life Is Good!
Activity
0%
 
Professional Coder
Gender: Female
Posts: 242
Topics: 3
WWW
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..

Affordable Custom Web Design Services
 
webmaster forum
kellylsn  Offline
Activity
0%
 
Professional Coder
Posts: 229
Topics: 7
July 26, 2011, 11:24:59 PM

Thanks for this code, its nice sharing of post. Thanks.

Payday Loan
Online Easy Loans
 
webmaster forum
Activity
0%
 
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
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page January 30, 2012, 08:45:12 PM