该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
本人大一,,微积分刚学到泰勒展开式,,然后一时兴起,用手机编译器做了个科学计算器。。我真觉得泰勒是个天才啊啊!!这里顺便和大家分享一下代码,,有一个小问题就是计算较大的数时误差较大,〈因为是在0处展开〉希望能有大佬提出改进方法。。
#include
#include
#include
main(){
float E();
float SIN();
float COS();
float FP();
float LN();
int choose;
printf("enter 1 for e^x\n"
" 2 for sin(x)\n"
" 3 for cos(x)\n"
" 4 for x^a\n"
" 5 for ln(x)\n");
mark:
scanf("%d",&choose);
switch (choose){
case 1:E();break;
case 2:SIN();break;
case 3:COS();break;
case 4:FP();break;
case 5:LN();break;
default :break;
}
printf("Continue:\n");
goto mark;
}
float powfac(float x,int n){
float trans=1;
for (int i=1;i<=n;i++){
trans=x*trans/i;
}
return trans;
}
float power(float x,int n){
int i=abs(n);
float result;
float trans=1;
if (n==0) result=1;
else for (i;i>=1;i--){
trans=x*trans;
}
result=n>0?trans:(1/trans);
return result;
}
/*
float factorial(int n){
float result=1;
for (n;n>=1;n--)
result=n*result;
return result;
}
*/
float E(){
float powfac(float x,int n);
float power(float x,int n);
float result=1;
float x;
printf("e^");
scanf("%f",&x);
for (int n=1;n<=100;n++){
result=result+powfac(x,n);
}
printf("result=%.3f\n",result);
}
float SIN(){
float powfac(float x,int n);
float power(float x,int n);
float result=0;
float minT(float angle);
float x;
float rad;
printf("sin");
scanf("%f",&x);
x=minT(x);
rad=x/180*3.141592;
for (int n=1;n<=100;n=n+2){
result=result+
power(-1,(n-1)/2)*powfac(rad,n);
}
printf("result=%.3f\n",result);
}
float COS(){
float power(float x,int n);
float minT(float angle);
float SIN();
float result;
float x;
float rad;
printf("cos");
scanf("%f",&x);
x=minT(x);
rad=x/180*3.141592;
for (int n=1;n<=100;n=n+2)
result=result+
power(-1,(n-1)/2)*powfac(rad,n);
result=sqrt(1-power(result,2));
printf("result=%.3f\n",result);
}
/*
float FP(){
float x,a,x_;
float result=1;
printf("X^a:");
scanf("%f^%f",&x,&a);
x_=x-1;
for (int n=1;n<=10;n++){
float trans=1;
for (int i=1;i<=n;i++){
trans=(a+1-i)*x_*trans/i;
}
result=result+trans;
}
printf("result=%.4f\n",result);
}
float LN(){
float powfac(float x,int n);
float power(float x,int n);
float result=0;
float x,x_;
printf("ln");
scanf("%f",&x);
x_=x-1;
for (int n=1;n<=10;n++){
result=result+
power(-1,n+1)*power(x_,n)/n;
}
printf("result=%.4f\n",result);
}
//展开式发散无法求极限
//无效算法
*/
float LN(){
float powfac(float x,int n);
float power(float x,int n);
float result=1;
float x,x_;
printf("ln");
scanf("%f",&x);
if (x<=0){printf("Error\n");goto loop_;}
x_=(x-1)/(x+1);
for (int n=2;n<=1000;n=n+2){
result=result+power(x_,n)/(n+1);
}
result=2*x_*result;
printf("result=%.3f\n",result);
loop_:
}
float FP(){
float x,a,x_;
float trans=1;
float result=1;
printf("X^a:");
scanf("%f^%f",&x,&a);
if (x==0){ result=0;goto loop; }
x_=(x-1)/(x+1);
for (int n=2;n<=100;n=n+2){
trans=trans+power(x_,n)/(n+1);
}
trans=a*2*x_*trans;
for (int n=1;n<=100;n++){
result=result+powfac(trans,n);
}
loop:
printf("result=%.3f\n",result);
}
float minT(float angle){
float delta=angle-(int)angle;
float basic=(int)angle%360+delta;
return basic;
}