调试示例
/*****x/2=1+1!/3+2!/3*5+3!/3*5*7+4!/3*5*7*9+...+i!/3*5*...(2*i+1)*****/ /***求x得值***/ #include<stdio.h> double fact(double n); double multi(double n); int main(void) { double i; double sum,item,eps; eps=1E-6; sum=1; item=1; for(i=1;item<=eps;i++){ item=fact(i)/multi(2*i+1); sum=sum+item; } printf("PI=%0.5lf\n",sum*2); return 0; } double fact(double n) { double i; double res; res=1; for(i=1 ;i<=n;i++) res=res*i; return res; } double multi(double n) { double i; double res; res=1; for(i=3;i<=n;i=i+2) res=res*i; return res; }
/*******调试示例******/ /*输入三角形的三条边a,b,c,判断是否能构成一个三角形,若能,求其面积和周长*/ #include<stdio.h> #include<math.h> int main(void) { double a,b,c; double area,perimeter,s; printf("Enter a,b,c:"); scanf("%lf%lf%lf",&a,&b,&c); if(a+b>c&&a+c>b&&b+c>a){ s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); perimeter=a+b+c; printf("area=%.2lf;perimeter=%.2lf\n",area,perimeter); } else { printf("These sides do not correspond to a valid triangle\n"); } return 0; }}
/******调试示例******/
/*输入a,b,c,求一元二次方程ax^2+bx+c=0的根*/
#include<stdio.h>
#include<math.h>
int main(void)
{
double a,b,c,d;
printf("Enter a,b,c:");
scanf("%lf%lf%lf",&a,&b,&c);
d=b*b-4*a*c; /*调试时设断点*/
if(a==0)
if(b==0)
if(c==0)
printf("参数都为0,方程无意义!\n");
else{
printf("a和b都为0,c不为0,方成不成立\n");
}
else{
printf("x=%0.2f\n",-c/b);
}
else
if(d>=0) /*调试时设断点*/
printf("x1=%0.2f\n",(-b+sqrt(d))/(2*a));
printf("x2=%0.2f\n",(-b-sqrt(d))/(2*a));
if(d<0)
printf("x1=%0.2f+%0.2fi\n",-b/(2*a),sqrt(-d)/(2*a));
printf("x2=%0.2f-%0.2fi\n",-b/(2*a),sqrt(-d)/(2*a));
return 0; /*调试时设断点*/
}