//高精度计算PI
#include <stdio.h>
#include <stdlib.h>
#define max 500
typedef struct Node{
int data;
struct Node *next,*pre;
}LHead,*LHeadPtr,Node,*NodePtr;
void List_Init(LHeadPtr L){
L->data = 2;
L->next = L;
L->pre = L;
NodePtr p,q;
q = L;
for(int i = 1;i < max;i++)
{
p = (NodePtr)malloc(sizeof(Node));
p->data = 0;
p->next = q->next;
p->pre = q;
q->next->pre = p;
q->next = p;
}
}
void M_D(LHeadPtr L,int zi,int mu){
NodePtr p;
p = L->pre;
//乘法
int c,d=0;
for(;p != L;p=p->pre)
{
c = p->data * zi + d;
p->data = c % 10;
d = c / 10;
}
p->data +=d;
//除法
d = 0;
p = L;
while(1)
{
c = p->data + d * 10;
p->data = c / mu;
d = c % mu;
p = p->next;
if(p == L)
break;
}
}
void List_Add(LHeadPtr L1,LHeadPtr L2){//加法
NodePtr p,q;
p = L1->pre;
q = L2->pre;
int c,d=0;
while(1)
{
c = p->data + q->data + d;
p->data = c%10;
d = c/10;
p=p->pre;
q=q->pre;
if(p == L1->pre)
break;
}
}
int main(){
LHeadPtr s,m;
s = (LHeadPtr)malloc(sizeof(LHead));
m = (LHeadPtr)malloc(sizeof(LHead));
List_Init(s);
List_Init(m);
int i = 1,j=0;
while(i<2000)
{
j = i*2+1;
M_D(m,i,j);
List_Add(s,m);
i++;
}
int n;
scanf("%d",&n);
printf("%d.",s->data);
NodePtr p = s->next;
for(int i = 0;i < n;i++)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
return 0;
}
数据结构实验1.2:高精度计算PI值
最新推荐文章于 2022-03-31 22:42:50 发布