题目:输入n,输出PI精确到小数点后n位的PI值。
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int data;
struct node*next;
struct node*pre;
}node,*list;
int n;
void init(list &l)
{
list tail = l, p;
int i = 0;
p=(list)malloc(sizeof(node));
tail->next = p;
p->pre = tail;
tail = p;
p->data = 2;
tail->next = NULL;
}
void destorylist(list &l)
{
list p=l->next;
list q;
while (p->next)
{
q = p->next;
free(p);
p = q;
}
free(l);
l = NULL;
}
void insert(list &l, int data)
{
list p = l;
if(p == NULL)
{
return;
}
else
{
while (p->next)
p = p->next;
list temp = (list)malloc(sizeof(node));
p->next = temp;
temp->data = data;
temp->pre = p;
temp->next = NULL;
}
}
void tran(list l)
{
printf("%d.",l->next->data);
l=l->next;
list p;
p=l->next;
while(p!=NULL&&n)
{
printf("%d",p->data);
p=p->next;
n--;
}
printf("\n"