现在的位置: 首页 > 综合 > 正文

实现一些数学功能的函数(转载)

2013年03月25日 ⁄ 综合 ⁄ 共 3871字 ⁄ 字号 评论关闭


天在网上和大家做练习的时候,觉得希望能实现常用的数学公式不但可以练习,还可以回顾一下常用的数学公式,所以我在群里和大家打招呼,看看有谁愿意一起写
这个练习,终于找到愿意一起练习的朋友,今天就先发一下我们第一天的练习吧,如果谁也想参加进来可以联系我, 我的qq号962874841

下面是练习内容:

//arith.h

/*
作者:sheryl
类型名:arith_point
功能:用来标识坐标
限制:必须是双精度小数成员
*/
typedef struct pt{
 double x;
 double y;
}arith_point;

#define POINT pt


#ifndef PI
#define PI 3.14
#endif

/*
作者:sheryl
函数名:arith_sin
功能:求转换角度后的sin值
限制:必须是双精度小数
*/

double arith_sin(double &x);

/*
作者:sheryl
函数名:arith_cos
功能:求转换角度后的cos值
限制:必须是双精度小数
*/

double arith_cos(double &x);


#define SIN(x) arith_sin(x)
#define COS(x) arith_cos(x)


/*
作者:sheryl
函数名:standard_round
功能:根据角度求出圆任意轨迹点坐标
限制:参数都是双精度小数
*/

POINT standard_round(double a,double b,double r,double t);

#define STROUD(a,b,r,t) standard_round(a,b,r,t)


/*
作者:清风
函数名:ellipse_girth
功能:根据长半轴长和短半轴长计算椭圆周长
限制:参数都是双精度小数
*/

double ellipse_girth(double a,double b);

#define ELLGIR(a,b) ellipse_girth(a,b)


/*
作者:sheryl
模版函数名:arith_abs
功能:取绝对值函数
限制:仅适合于小数,整数,字符
*/

template<class T> T arith_abs(T &x){
 if(x!=0){
  if(x>=0){
   return x;
  }else{
   return -x;
  }
 }
}

#define ABS(x) arith_abs(x)

/*
作者:sheryl
模版函数名:arith_swap
功能:对两个变量进行交换
限制:仅适合于小数,整数,字符
*/

template<class T> void arith_swap(T &x,T &y){
 T temp;
 temp=x;
 x=y;
 y=temp;
}

#define SWAP(x,y) arith_swap(x,y)

/*
作者:sheryl
模版函数名:arith_max
功能:计算两个数中的最大数
限制:仅适合于小数和整数
*/

template<class T> T arith_max(T x,T y){
    if(x>y){
        return x;
    }else{
        return y;
    }
}

/*
作者:sheryl
模版函数名:arith_min
功能:计算两个数中的最小数
限制:仅适合于小数和整数
*/

template<class T> T arith_min(T x,T y){
    if(x<y){
        return x;
    }else{
        return y;
    }
}

#define MAX(x,y) arith_max(x,y)
#define MIN(x,y) arith_min(x,y)


/*
作者:sheryl
模版函数名:array_min
功能:计算数组中最小数
限制:仅适合于小数和整数
*/

template<class T>T array_min(T arr[],int size){
 T *pval=new T(0);
 for(int i=0;i<size;i++){
  if((i+1)<=size){
   if(*pval==0){
    *pval=MIN(arr[i],arr[i+1]);
   }else{
    *pval=MIN(arr[i],*pval);
   }
  }
 }
 return *pval;
}

/*
作者:sheryl
模版函数名:array_max
功能:计算数组中最大数
限制:仅适合于小数和整数
*/

template<class T>T array_max(T arr[],int size){
 T *pval=new T(0);
 for(int i=0;i<size;i++){
  if((i+1)<=size){
   if(*pval==0){
    *pval=MAX(arr[i],arr[i+1]);
   }else{
    *pval=MAX(arr[i],*pval);
   }
  }
 }
 return *pval;
}

#define ARRAYMIN(arr,size) array_min(arr,size)
#define ARRAYMAX(arr,size) array_max(arr,size)

#define ASC 1
#define DESC 0

/*
作者:sheryl
模版函数名:sortA
功能:对数组进行升序排列
限制:仅适合于小数和整数
*/

template<class T> void sortA(T arr[],T size){
 T i,j;
 T temp;
 for(i=1;i<size;i++){
  j=i;
  temp=arr[i];
  while(j>0&&temp<arr[j-1]){
   arr[j]=arr[j-1];
   j--;
  }
  arr[j]=temp;
 }

}

/*
作者:sheryl
模版函数名:sortB
功能:对数组进行将序排列
限制:仅适合于小数和整数
*/

template<class T> void sortB(T arr[],T size){
 T i,j;
 T temp;
 for(i=1;i<size;i++){
  j=i;
  temp=arr[i];
  while(j>0&&temp>arr[j-1]){
   arr[j]=arr[j-1];
   j--;
  }
  arr[j]=temp;
 }
}


#define SORTA(x,y) sortA(x,y)
#define SORTB(x,y) sortB(x,y)

/*
作者:sheryl
模版函数名:isort
功能:排序函数
限制:仅适合于小数和整数
*/

template<class T> void isort(T arr[],T size,T type){
 if(type==ASC){
  SORTA(arr,size);
 }else if(type==DESC){
  SORTB(arr,size);
 }
}

#define SORT(x,y,z) isort(x,y,z)


/*
作者:冯树勋
模版函数名:greatest_common_divisorA
功能:求两个数的最大公约数
限制:仅适合于整数
*/

template <class T> T greatest_common_divisor(T a, T b){
 if (0 == a) return b;
 else
  if (0 == b) return a;
   a=ABS(a);
   b=ABS(b);
   T max =MAX(a,b);
   T min =MIN(a,b);
   while (0 != max % min){
    T temp = max;
    max = min;
    min = temp % min;
   }
 return min;
}

/*
作者:冯树勋
模版函数名:greatest_common_divisorA
功能:求多个数的最大公约数
限制:仅适合于整数
*/

template <class T> T greatest_common_divisor(T *d, int n){
 if (0 == d || 1 > n)
  return T(0);
 else
  if (1 == n)
   return *d;
  else
  if (2 == n)
   return
   greatest_common_divisor(*d, *(d + 1));
  else
   return
   greatest_common_divisor(*d,
    greatest_common_divisor(d + 1, n - 1));
}

#define GCDD(x,y) greatest_common_divisor(x,y)
#define GCDS(x,y) greatest_common_divisor(x,y)

 

//arith.cpp

#include <iostream>
#include <math.h>
#include "arith.h"

using namespace std;

 

double arith_sin(double &x){
 x=sin(x * PI / 180);
 return x;
}

double arith_cos(double &x){
 x=cos(x * PI / 180);
 return x;
}

 

POINT standard_round(double a,double b,double r,double t){
 POINT *ppt=new POINT;
 ppt->x=a + r * COS(t);
 ppt->y=b + r * SIN(t);
 return *ppt;
}

 

double ellipse_girth(double a,double b){
 double *pgirth=new double;
 *pgirth=2*PI*b+4*(a-b);
 return *pgirth;
}

抱歉!评论已关闭.