//sin , cos , tan , cot , exp , pow , fabs & fibonachi simulation
//programming by : Erfan Nasoori
//Mail : ketn68@yahoo.com
//Date of send : 2009/2/7


#include <iostream>
#include <math> /*<cmath>*/
#include <conio>

class mymath{
		public:
			 double mysin(double);
			 double mycos(double);
                         double mytan(double);
                         double mycot(double);
			 double myexp(double);
			 double mypow(double,double);
                         double myfabs(double);
			 int    fibonachi1(int);
                         void   fibonachi2(long[],int);
	    };

/////////////////////////////////////////////////
double mymath :: mysin(double x)
{
 double f=1 , p=x , sum=0 , item=x;
 int i;
 for(i=1; fabs(item)>=1e-10 ; ++i)
 {
    sum += item;
    f *= (2*i+1)*(2*i);
    p *= (-x*x);
    item = p/f;
 }
 return sum;
}
/////////////////////////////////////////////////
double mymath :: mycos(double x)
{
 double f=1 , p=1 , sum=1 , item=1;
 int i;
 for(i=1; fabs(item)>=1e-10 ; ++i)
 {
    f *= (2*i-1)*(2*i);
    p *= (-x*x);
    item = p/f;
    sum += item;
 }
 return sum;
}
/////////////////////////////////////////////////
double mymath :: mytan(double x)
{
 if( mycos(x) != 0)
    return mysin(x)/mycos(x);
}
/////////////////////////////////////////////////
double mymath :: mycot(double x)
{
 if( mysin(x) != 0)
    return mycos(x)/mysin(x);
}
////////////////////////////////////////////////
double mymath :: myexp(double x)
{
 long double f=1 , sum=1 , p=1 , item;
 for(int i=1; i<1000 ; ++i)
 {
    f *= i;
    p *= x;
    item = p/f;
    sum += item;
 }
 return sum;
}
////////////////////////////////////////////////
double mymath :: myfabs(double x)
{
 return( x<0 ?  -x :  x );
}
////////////////////////////////////////////////
double mymath :: mypow(double x,double y)
{
 double z , p=1;
 //y<0 ? z=-y : z=y ;
 if(y<0)
    z = myfabs(y);
 else
    z = y;
 for(int i=0; i<z ; ++i)
 {
    p *= x;
 }
 if(y<0)
   return 1/p;
 else
   return p;
}
//////////////////////////////////////////////
int mymath :: fibonachi1(int x)
{
 int f1=1 , f2=1 , f3;
 f3 = f1;
 for(int i=3 ;i<=x;++i)
 {
    f3 = f1+f2;
    f1 = f2;
    f2 = f3;
 }
 return f3;
}
//////////////////////////////////////////////
void mymath :: fibonachi2(long fibo[],int x)
{
 long f1=1 , f2=1 , f3;
 for(int i=3 ;i<=x;++i)
 {
    fibo[i] = f1+f2;
    f1 = f2;
    f2 = fibo[i];
 }
}
/////////////////////////////////////////////////
int main()
{
 double si,co,tg,cotg,po,ex,fs,x,y,z;
 int i,j,num,fib1,fib2;
 long f3[50];
 char key;
 mymath m;
 do{
     clrscr();
     cout<<"which program do you want?\n"
    	 <<"1-sin\n"
  	 <<"2-cos\n"
         <<"3-tan\n"
  	 <<"4-cot\n"
  	 <<"5-exp\n"
  	 <<"6-pow\n"
         <<"7-fabs\n"
  	 <<"8-fibonachi(1)\n"
  	 <<"9-fibonachi(2)\n"
  	 <<"enter a number of one of them:\n";
     cin>>num;
     cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;

                while(num<1 || num>9)
		{
		 cout<<"Please enter one of 1-2-3-4-5-6-7-8-9"
                     <<" inorder to starting program!\n";
                 cin>>num;
                 cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
		 ++i;
		}

                if(num == 1)
 		{
 		 cout<<"Please enter x for sin function = ";   cin>>x;

 		 si = m.mysin(x);
 		 cout<<"sin("<<x<<") = "<<si<<"\n"
       	             <<"sin("<<x<<") of math.h headerfile = "<< sin(x)<<endl;
 		}

 		else if(num == 2)
 		{
 		 cout<<"Please enter x for cos function = ";   cin>>x;
 		 co = m.mycos(x);
 		 cout<<"cos("<<x<<") = "<<co<<"\n"
   		     <<"cos("<<x<<") of math.h headerfile = "<<cos(x)<<endl;
 		}

                else if(num == 3)
 		{
 		 cout<<"Please enter x for tan function = ";   cin>>x;
 		 tg = m.mytan(x);
 		 cout<<"tan("<<x<<") = "<<tg<<"\n"
   		     <<"tan("<<x<<") of math.h headerfile = "<<tan(x)<<endl;
 		}

                else if(num == 4)
 		{
 		 cout<<"Please enter x for cot function = ";   cin>>x;
 		 cotg = m.mycot(x);
 		 cout<<"cot("<<x<<") = "<<cotg<<"\n"
   		     <<"cot("<<x<<") of math.h headerfile = "<<(1/tan(x))<<endl;
 		}

  		else if(num == 5)
 		{
 		 cout<<"Please enter x for exponent function = ";   cin>>x;
 		 ex = m.myexp(x);
 		 cout<<"exp("<<x<<") = "<<ex<<"\n"
   		     <<"exp("<<x<<") of math.h headerfile = "<<exp(x)<<endl;
 		}

 		else if(num == 6)
 		{
 		 cout<<"Please enter x for power function = ";   cin>>x;
 		 cout<<"Please enter y for power function = ";   cin>>y;
 		 po = m.mypow(x,y);
 		 cout<<"pow("<<x<<","<<y<<") = "<<po<<"\n"
   		     <<"pow("<<x<<","<<y<<") of math.h headerfile = "
                     <<pow(x,y)<<endl;
 		}

                else if(num == 7)
 		{
 		 cout<<"Please enter a number = ";   cin>>x;
 		 fs = m.myfabs(x);
 		 cout<<"fabs("<<x<<") = "<<fs<<"\n"
   		     <<"fabs("<<x<<") of math.h headerfile = "
                     <<fabs(x)<<endl;
 		}

 		else if(num == 8)
                {
 		 cout<<"which number do you want to show ?"<<endl;
 		 cin>>x;
   		 while(x <= 0)
                 {
     		   cout<<"Try again : ";
     		   cin>>x;
                   ++i;
                 }
 		 fib1 = m.fibonachi1(x);
 		 cout<<x<<"th number of fibonachi seri = "<<fib1<<endl;
 		}

		else
                {
   		 cout<<"How many numbers of fibonachi "
                     <<"seri do you want to show?\n";
   		 cin>>x;
   		 while(x <= 0)
                 {
        	   cout<<"Try again : ";
        	   cin>>x;
        	   ++i;
                 }
   		 m.fibonachi2(f3,x);

   		 cout<<x<<" numbers of fibonachi : "<<endl;
   		 if(x == 1)
   		   cout<<"1"<<endl;
   		 else
                 {
   		   cout<<"1 1 ";
   		   for(i=3 ; i<=x ; ++i)
   		      cout<<f3[i]<<" ";
     		 }
   		 cout<<endl;
                }
 
		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
		cout<<"do you want to continue (y/n) ? "<<endl;
                cin.ignore();
		key = getch();

   }while(key=='y'|| key=='Y');
 return 0;
}
2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by subtercosm
0

Thanks for making this posting.

In mytan and mycot, not all paths return a value. What do you expect the function's behavior to be then? I would return NaN. Also those functions unnecessarily recalculate mycos and mysin once.

Your lines

if(y<0)
    z = myfabs(y);
else
    z = y;

are equivalent to just saying z = myfabs(y); .

0

OK
thats right
z = myfabs(y);

but i don't undrestand , is there any problem in mytan ? logical or mathematics etc.
its return value haven't problem , we can declare a double cell and transfer mysin/mycos to it and finally return this cell.
OK?

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.