/*
*File:calculate PI.c
*------------------------
*This program is calculate the PI
*/
#include<stdio.h>
#include"simpio.h"
#include"genlib.h"
#include<math.h>
#define r 2
/ * Function program */
double calculate_PI (void)
/* Main prorgram */
int main()
{
printf(" This program is answer the PI");
printf("The PI is %g\n",calculate_PI ());
return 0;
}
/*
*Function: To calculate PI;
* Usage:Tocalculate ()
*---------------------------------
*This program calculate the PI
*/
double calculate_PI (void)
{
int n = 100;
double w;
w = r / n; // 纠错: int 型除以int 型 ,如果是浮点数的话会强行转换,此处是 2 / 100; 所以会是0;
double s,x,h; // 把 w = r /n 改成 w = (double)r / n;强制转化成double型;以后写代码时需要注意小细节
double S =0;
for (int i = 1;i <= n;i++) {
x = (w / 2) + (i - 1) * w;
h = sqrt (r*r - x*x);
s = h *w;
S = S + s;
}
return (S);
}
/*
* 欢迎各位朋友对我的代码进行改造和优化,本人不胜感激!
* 若有相关专业的朋友可以私信我,一起修炼,哈哈哈哈
*/