my own calculator - WHITE PANDA - 05-24-2010
Hi guys i have made a scientific calculator and just felt like sharing it with my mates.So here it goes.Code: #include <iostream.h>
#include<math.h>
#include <stdio.h>
#include<conio.h>
# define sqr(x) (x)*(x)
void main()
{
float a,b,c;
int choice, i;
clrscr();
do
{
cout<<"\n\n\nScientific calculator\n\n\n";
cout<<"\n\nEnter \n\n1.ADDITION\n\n 2.SUBTRACTION\n\n 3.MULTIPLICATION\n\n 4.DIVISION\n\n 5.SIN\n\n 6.COSINE \n\n 7.TAN\n\n 8.LOG\n\n 9.QUADRATIC EQUATUION SOLVER\n\n 10.POWER FUNCTION \n\n 11.EXPONENTIAL FUNCTION \n\n 12.PERCENTAGE \n\n 13.FACTORIAL";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<"\n\nADDITION";
cout<<"\n\nENTER TWO NUMBERS";
cin>>a>>b;
c=a+b;
cout<<"\n\nANSWER IS "<<c;
cout<<"\n\nTHANK YOU COME AGAIN";
break;
}
case 2:
{
cout<<"\n\nSUBTRACTION";
cout<<"\n\nENTER TWO NUMBERS";
cin>>a>>b;
c=a-b;
cout<<"\n\nANSWER IS "<<c;
cout<<"\n\nTHANK YOU COME AGAIN";
break;
}
case 3:
{
cout<<"\n\nMULTIPLICATION";
cout<<"\n\nENTER TWO NUMBERS";
cin>>a>>b;
c=a*b;
cout<<"\n\nANSWER IS "<<c;
cout<<"\n\nTHANK YOU COME AGAIN";
break;
}
case 4:
{
cout<<"\n\nDIVISION";
cout<<"\n\nENTER TWO NUMBERS";
cin>>a>>b;
c=a/b;
cout<<"\n\nANSWER IS "<<c;
cout<<"\n\nTHANK YOU COME AGAIN";
break;
}
case 5:
{
double a,b,ans;
int ch;
clrscr();
cout<<"\n\nSIN FUNCTION";
cout<<"\n\n PRESS 1 IF THE ANGLE IS IN DEGREES";
cout<<"\n\n PRESS 2 IF THE ANGLE IS IN RADIANS";
cin>>ch;
if (ch==1)
{
cout<<"\n\nENTER THE ANGLE IN DEGREES";
cin>>a;
b=(a*22)/(7*180);
}
else if (ch==2)
{
cout<<"\n\nENTER THE ANGLE IN RADIANS";
cin>>a;
b=a;
}
else
{
cout<<"\n\nERROR";
break;
}
ans=sin(b);
cout<<"\n\nTHE SIN OF "<<a<<"IS "<<ans;
cout<<"\n\nTHANK YOU COME AGAIN";
break;
}
case 6:
{
double a,b,ans;
int ch;
clrscr();
cout<<"\n\nCOSINE FUNCTION";
cout<<"\n\n PRESS 1 IF THE ANGLE IS IN DEGREES";
cout<<"\n\n PRESS 2 IF THE ANGLE IS IN RADIANS";
cin>>ch;
if (ch==1)
{
cout<<"\n\nENTER THE ANGLE IN DEGREES";
cin>>a;
b=(a*22)/(7*180);
}
else if (ch==2)
{
cout<<"\n\nENTER THE ANGLE IN RADIANS";
cin>>a;
b=a;
}
else
{
cout<<"\n\nERROR";
break;
}
ans=cos(b);
cout<<"\n\nTHE COSINE OF "<<a<<"IS "<<ans;
cout<<"\n\nTHANK YOU COME AGAIN";
break;
}
case 7:
{
double a,b,ans;
int ch;
clrscr();
cout<<"\n\nTAN FUNCTION";
cout<<"\n\n PRESS 1 IF THE ANGLE IS IN DEGREES";
cout<<"\n\n PRESS 2 IF THE ANGLE IS IN RADIANS";
cin>>ch;
if (ch==1)
{
cout<<"\n\nENTER THE ANGLE IN DEGREES";
cin>>a;
b=(a*22)/(7*180);
}
else if (ch==2)
{
cout<<"\n\nENTER THE ANGLE IN RADIANS";
cin>>a;
b=a;
}
else
{
cout<<"\n\nERROR";
break;
}
ans=tan(b);
cout<<"\n\nTHE TAN OF "<<a<<"IS "<<ans;
cout<<"\n\nTHANK YOU COME AGAIN";
break;
}
case 9:
{
float A,B,C,delta;
clrscr();
cout<<"\nQUADRATIC EQUATION SOLVER";
cout<<"\nENTER COEFFICIENTS";
cout<<"A ";
cin>>A;
cout<<"B ";
cin>>B;
cout<<"C ";
cin>>C;
if (A==0 && B!=0)
{
cout<<"\n\nTHIS IS A FIRST DEGREE EQUATION";
cout<<"\n\nTHE SOLUTION IS "<<-C/B;
}
else if(A==0&&B==0)
{
cout<<"\n\nTHIS EQUATION HAS NO SOLUTION";
}
else
{
delta=sqr(B)-4*A*C;
if (delta<0)
{
cout<<"\n\nTHIS EQUATION HAS NO REAL ROOTS";
}
else if(delta==0)
{
cout<<"\n\nTHIS EQUATION HAS DOUBLE ROOTS ";
cout<<"\n\nAND THE ROOTS ARE : "<<-B/2*A;
}
else
{
cout<<"\n\nTHIS EQUATION HAS TWO REAL ROOTS";
float X1 =(-B+sqrt(delta))/(2*A);
float X2 =(-B-sqrt(delta))/(2*A);
cout<<" X1 ="<<X1;
cout<<" X2 ="<<X2;
}
}
cout<<"\n\nTHANK YOU COME AGAIN";
break;
}
case 8:
{
cout<<"\n\nLOG";
double a,ans;
int ch;
cout<<"\n1.NATURAL LOG \n2.LOG TO THE BASE 10";
cin>>ch;
if(ch==1)
{
cout<<"\n\nENTER VALUE ";
cin>>a;
ans=log(a);
cout<<"\n\nTHE VALUE OF THE LOG IS "<<ans;
}
if (ch==2)
{
cout<<"\n\nENTER VALUE ";
cin>>a;
ans=log10(a);
cout<<"\n\nTHE VALUE OF THE LOG TO THE BASE 10 IS "<<ans;
}
else
{
cout<<"\n\nERROR!!";
}
break;
}
case 10:
{
float m,n,p;
clrscr();
cout<<"\n\nPOWER FUNCTION";
cout<<"\n\nENTER THE VALUES OF m AND n W.R.T THE EQUATION m^n ";
cin>>m>>n;
p=pow(m,n);
cout<<"\n\nTHE ANSWER IS "<<p;
cout<<"\n\nTHANK YOU COME AGAIN";
break;
}
case 11:
{
float r,s;
clrscr();
cout<<"\n\nEXPONENTIAL FUNCTION";
cout<<"\n\nENTER VALUE";
cin>>r;
s=exp(r);
cout<<"\n\nTHE ANSWER IS "<<s;
cout<<"\n\nTHANK YOU COME AGAIN";
break;
}
case 12:
{
float x,y,z;
cout<<"\n\nPERCENTAGE";
cout<<"\n\nPLEASE ENTER THE VALUES OF X AND Y W.R.T THE EQUATION (X/Y)*100 ";
cin>>x>>y;
z=x/y*100;
cout<<"\n\nTHE ANSWER IS "<<z;
cout<<"THANK YOU COME AGAIN";
break;
}
case 13:
{
int u,v,fact;
cout<<"\n\nFACTORIAL";
cout<<"\n\nENTER THE NUMBER ";
cin>>u;
fact=1;
for (v=u;v>=1;v--)
fact=fact*v;
cout<<"\n\nTHE ANSWER IS "<<fact;
break;
}
default:
{
cout<<"\n\nERROR!!";
break;
}
}cout<<"\n\nENTER 1 TO CONTINUE.0 TO EXIT";
cin>>i;
}
while (i==1);
getch();
}
Its really simple programming and I have not used the concept of classes etc.Please let me know if you like it
RE: my own calculator - delme - 05-24-2010
Lol, waaaay better than my calculator that I made when I started C++, all it had was addition, subtraction, multiplication and division. I havn't tried running the code but it looks good, well done.
RE: my own calculator - WHITE PANDA - 05-28-2010
(05-24-2010, 03:38 AM)Detest Wrote: Lol, waaaay better than my calculator that I made when I started C++, all it had was addition, subtraction, multiplication and division. I havn't tried running the code but it looks good, well done.
Yeah i spent too much time and energy in making it.But i sort of enjoyed it
RE: my own calculator - Psycho - 06-02-2010
Cool, now try making a calculator where the user is able to enter an expression and your program will evaluate it. Hint: Use a stack. infix notation, prefix notation.
RE: my own calculator - MrD. - 06-02-2010
(06-02-2010, 01:17 PM)Psycho Wrote: Cool, now try making a calculator where the user is able to enter an expression and your program will evaluate it. Hint: Use a stack. infix notation, prefix notation.
A lexer and a parser would also help. The kind of calculator described here is actually a good example of a very basic stack based virtual machine.
RE: my own calculator - Psycho - 06-02-2010
They could be very helpful. But this is still possible by removing all the whitespace and going through the infix expression one character at a time. If you find an operand, you throw it at the end of your prefix expression string. If it's an open parenthesis, you push it on the stack. When you find an operator, you push it on the stack if the stack is empty...if it's not empty, you pop every operator of greater or equal precedence (stop when an operator of lower precedence is found, another open parenthesis is found, or the stack is empty) and throw them onto the end of your prefix string...and THEN you can push your operator on the stack. If you find the close parenthesis, you just pop the operators off the stack until you find its matching open parenthesis. Finally, if you hit the end of the infix expression, you just pop everything off the stack and throw it at the end of your prefix string. But that's just converting infix notation to prefix notation using a stack. The fun part comes when you evaluate the expression. Now that your expression is in prefix notation, it should be easier to evaluate it using whatever algorithm you come up with. One last thing, you don't necessarily have to use stacks, there are quite a few ways of accomplishing this...and it may have been a mouth full, so I could try to explain this by writing pseudocode if needed. There are a lot of resources on this topic, too.
RE: my own calculator - Marikā¢ - 06-18-2010
Thanks for the code dude, I will try it soon.
RE: my own calculator - iHood - 06-18-2010
Nice going to try soon. Good job anyway.
RE: my own calculator - Kondry - 07-14-2010
Nice code, could use some work though.
|