Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 14-03-2009
Member
 
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
Reply With Quote
  #2  
Old 14-03-2009
Member
 
Join Date: Apr 2008
Posts: 4,086
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 << "
Reply With Quote
  #3  
Old 14-03-2009
Member
 
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);
}
}
}
Reply With Quote
  #4  
Old 14-03-2009
Member
 
Join Date: Jan 2008
Posts: 3,751
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;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Calculator in C++ Language"
Thread Thread Starter Forum Replies Last Post
How to change ?Microsoft office 2011 for Mac? language as I bought the wrong language package Naq Windows Software 8 19-04-2012 08:12 PM
No Chinese language even after language pack upgrade in windows 7 Cadence Operating Systems 3 31-01-2011 11:57 PM
What are the advantages of a object oriented language over the procedural language? Naotau Software Development 7 08-12-2010 02:20 AM
Vista ultimate Language Pack for Greek language ph_oratis Windows Update 3 29-11-2009 03:15 PM
Replace calculator with Command Line Calculator Eric B Windows Software 3 07-05-2009 04:32 PM


All times are GMT +5.5. The time now is 06:44 AM.