Results 1 to 4 of 4

Thread: Calculator in C++ Language

  1. #1
    Join Date
    Jan 2009
    Posts
    57

    Calculator in C++ Language

    I have a s,all query regarding c++ language. I want to create a program in order to run as a calculator in application. I am actually getting confused as not a better programmer in c++

    Please provide some assistance

  2. #2
    Join Date
    Apr 2008
    Posts
    4,088

    Re: Calculator in C++ Language

    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    void instructUser();
    double doDivideZero(double &);
    int main()
    {
    instructUser();
    double displayedVal;
    double newEntry;
    char command_character ;
    displayedVal = 0.0;
    cout << " Enter accepted Operator:" ;
    cin >> command_character;
    while (command_character != 'Q' || command_character != 'q')
    {
    switch(command_character)
    {
    case 'c':
    case 'C': displayedVal = 0.0;
    break;
    case '+': cout << " Enter Number:";
    cin >> newEntry;
    displayedVal = displayedVal + newEntry;
    break;
    case '-': cout << " Enter Number:";
    cin >> newEntry;
    displayedVal = displayedVal - newEntry;
    break;
    case '*': cout << " Enter Number:";
    cin >> newEntry;
    displayedVal = displayedVal * newEntry;
    break;
    case '/': cout << " Enter Number:";
    cin >> newEntry;
    displayedVal = displayedVal / newEntry;
    if (newEntry == 0)
    {
    doDivideZero(double &);
    }

    break;
    case '^': cout << " Enter Number:";
    cin >> newEntry;
    displayedVal = pow (displayedVal,newEntry);
    break;
    default : cout << " Unacceptable Operator(" << command_character << ")" << endl;
    }
    cout << " The result so far is: " <<displayedVal<< endl;
    cout << " Enter Operator:";
    cin >> command_character;
    }
    system ("pause");
    return 0;
    }
    void instructUser()
    {
    cout << " " <<endl;
    cout << "

  3. #3
    Join Date
    Apr 2008
    Posts
    2,139

    Re: Calculator in C++ Language

    #include <iostream.h>
    #include <conio.h>
    int mc(int x, int y) //Multiply two numbers
    {
    cout <<"\n\n"<< x <<" times "<< y <<" equals ";
    return (x*y);
    }
    int ac(int a, int b) //Add two numbers
    {
    cout <<"\n\n"<< a <<" plus "<< b <<" equals ";
    return (a+b);
    }
    int sc(int z, int c) //Subtract two numbers
    {
    cout <<"\n\n"<< z <<" minus "<< c <<" equals ";
    return (z-c);
    }
    int dc(int o, int t) //Divide two numbers
    {
    cout <<"\n\n"<< o <<" divided by "<< t <<" equals ";
    return (o/t);
    }
    void calc(char choice)
    {
    int on,tw,thr;
    if (choice == '+') //This whole block checks what the user wants to calculate, and refers to the proper routine to calculate it.
    {
    cout<<"You selected "<<choice<<". Please enter two numbers,\nsepperated by spaces,";
    cout<<"that you want to add."<<endl;//print instructions for the user
    cin>>on;//Get the value of variable on
    cin>>tw;//Get the value of variable tw
    thr=ac(on,tw);//Get the sum of on and tw, and assign that value to thr
    cout<<thr<<"\n\n\n\aThanks for using my calculator!";//Print a thank you message
    }
    else if (choice =='-')
    {
    cout<<"You selected "<<choice<<". Please enter two numbers,\nsepperated by spaces, that you want to subtract."<<endl;
    cin>>on;
    cin>>tw;
    thr=sc(on,tw);
    cout<<thr<<"\n\n\n\aThanks for using my calculator!";
    }
    else if (choice =='*')
    {
    cout<<"You selected "<<choice<<". Please enter two numbers,\nsepperated by spaces, that you want to multiply."<<endl;
    cin>>on;
    cin>>tw;
    thr=mc(on,tw);
    cout<<thr<<"\n\n\n\aThanks for using my calculator!";
    }
    else if (choice =='/')
    {
    cout<<"You selected "<<choice<<". Please enter two numbers,\nsepperated by spaces, that you want to divide."<<endl;
    cin>>on;
    cin>>tw;
    thr=dc(on,tw);
    cout<<thr<<"\n\n\n\aThanks for using my calculator";
    }
    else
    {
    cout<<"\nPlease reenter that value.\n\a";
    cin>>choice;
    calc(choice,);
    }
    }
    void main()
    {
    clrscr();
    int one, two, three;
    char choice;
    while (choice != 'e')
    {
    cout<<"\nPlease enter +,-,*, or / and then two numbers,\nsepperated by spaces, that you wish to\nadd,subtract,multiply,or divide.\nType e and press enter to exit.";
    cin>>choice;
    if (choice != 'e')
    {
    calc(choice);
    }
    }
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    3,755

    Re: Calculator in C++ Language

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <math.h>

    using namespace std;

    int main()
    {
    cout<<" Quadratic Equation Calculator"<<endl<<"By Nerd"<<endl<<endl;
    double a,b,c,d,x,y; //double or float should not matter
    cout << "ax^2+bx+c=0" << endl;
    cout << "Enter a Value for a: ";
    cin >> a;
    cout << endl;
    cout << "Enter a value for b: ";
    cin >> b;
    cout << endl;
    cout << "Enter a value for c: ";
    cin >> c;
    cout << endl;
    d =((b*b)-(4*a*c));
    if (d > 0)
    {
    cout << "There are 2 roots" << endl;
    x =(-b + sqrt(d))/(2*a);
    y =(-b - sqrt(d))/(2*a);
    cout <<endl <<"Root 1: "<<x;
    cout <<endl <<"Root 2: "<<y <<"\n";
    }
    else if (d == 0)
    {
    cout << "There is 1 root" << endl;
    x = (-b)/(2*a);
    cout <<"Root: "<<x << "\n";
    }
    else if (d < 0)
    {
    cout << "There are no real roots" << endl;
    }


    system( "PAUSE");
    return 0;
    }

Similar Threads

  1. Replies: 8
    Last Post: 19-04-2012, 08:12 PM
  2. Replies: 3
    Last Post: 31-01-2011, 11:57 PM
  3. Replies: 7
    Last Post: 08-12-2010, 02:20 AM
  4. Vista ultimate Language Pack for Greek language
    By ph_oratis in forum Windows Update
    Replies: 3
    Last Post: 29-11-2009, 03:15 PM
  5. Replace calculator with Command Line Calculator
    By Eric B in forum Windows Software
    Replies: 3
    Last Post: 07-05-2009, 04:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,449,274.87065 seconds with 17 queries