| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 701 人关注过本帖
标题:我的求圆周率的程序怎么不能运行?帮个忙!!
只看楼主 加入收藏
liupengchang
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-7-6
收藏
 问题点数:0 回复次数:1 
我的求圆周率的程序怎么不能运行?帮个忙!!
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define USEFPU //条件编译,两种情况,一种是用浮点数,时间较短,另一种用整数,时间较长
#define ARC5 1
#if defined USEFPU //如果这样定义,则用浮点数
#define BASE 1000000000L
#define BASEDIGITS 9
typedef long int SHORT;
typedef double LONG;
#else //否则,用整型数
#define BASE 100
#define BASEDIGITS 2
typedef unsigned char SHORT;
typedef long int LONG;
#endif
typedef long int INDEXER;
class PI
{
private:
SHORT *pi, *powers, *term;
INDEXER size;
public:
PI(){size=0; pi= powers= term= NULL;}
PI(INDEXER nsize)
{ size= nsize;
size = ((size+ BASEDIGITS -1)/ BASEDIGITS) +1; //将位数变成为输出类型的元素个数
pi = new SHORT [size];
powers = new SHORT [size];
term =new SHORT [size];
}
~PI()
{ if(pi) delete []pi;
if(powers) delete []powers;
if(term) delete []term;
}
void SetSize(INDEXER nsize)
{ size= nsize;
size = ((size+ BASEDIGITS -1)/ BASEDIGITS) +1; //将位数变成为输出类型的元素个数
if(pi) delete []pi;
pi = new SHORT [size];
if(powers) delete powers;
powers = new SHORT [size];
if(term) delete term;
term =new SHORT [size];
}
void Show();
void OutDig( int dig);
void PrintShort(SHORT num);
void Print(SHORT *num);
void arctan(int multiplier, int denom, int sign); //multiplier为系数、denom为分母、sign为分子
};
void PI::Show( )
{ for (int x = 0; x < size; x++)
pi[x] = 0;
#if defined ARC3
arctan(8,3,1); //8是系数,1/3是自变量
arctan(4,7,1);
#elif defined ARC5
arctan(16,5,1);
arctan(4,70,-1);
arctan(4,99,1); //系数中已包含了要运行的公式
#elif defined ARC4
arctan(12,4,1);
arctan(4,20,1);
arctan(4,1985,1);
#elif defined ARC10
arctan(32,10,1);
arctan(4,239,-1);
arctan(16,515,-1);
#else
arctan(16,5,1);
arctan(4,239,-1);
#endif
Print(pi);
}
void PI::OutDig(int dig) //按位输出数字,将每位数字变成字符型
{ static int printed = 0;
putchar( dig +'0');
printed ++;
if ((printed%1000) ==0)
{ printed = 0;
printf("\n\n\n");
}
if(( printed%50) ==0)
printf("\n");
else if ((printed%10) ==0)
putchar(' ');
}
void PI::PrintShort(SHORT num) //将整型数变成字符型的数,放入数组digits[]并显示
{ int x;
int digits[BASEDIGITS + 1];
for( x =0; x< BASEDIGITS; x++)
{ digits[x] = num % 10;
num /=10;
}
for (x = BASEDIGITS -1; x >= 0; x--)
OutDig( digits[x]);
}
void PI::Print(SHORT * num)
{ INDEXER x;
printf("\nPI = 3. \n");
for (x =1; x < size; x++)
PrintShort(num[x]);
printf("\n");
}
void PI::arctan(int multiplier, int denom, int sign) //multiplier为系数、denom为分母、sign为分子
//本函数是程序的难点,将小数边成数据数组,放至数组中进行计算
{ INDEXER x;
LONG remain, temp, divisor,denom2;
SHORT NotZero = 1;
INDEXER adv;
for (x = 0; x < size; x++)
powers[x] =0;
divisor =1;
denom2 =(LONG)denom;
denom2 *=denom2;
adv = 0;
remain = (LONG)multiplier * denom; //第一项
while (NotZero)
{ for(x = adv;x < size; x++)
{ temp = (LONG)powers[x] + remain;
powers[x] = (SHORT)(temp/denom2);
remain = (temp - ( denom2 * (LONG)powers[x])) * BASE; //将运算结果变为整数
}
remain = 0;
for (x =adv;x < size;x++)
{ temp = (LONG)powers[x] + remain;
term[x] = (SHORT)(temp/divisor);
remain = (temp - (divisor * (LONG)term[x]))*BASE;
}
remain =0;
if (sign >0)
{ LONG carry,sum;
carry =0;
for (x =size -1;x >=0;x--) //从低位循环
{ sum = (LONG)pi[x] + (LONG)term[x] +carry;
carry = 0;
if (sum >= BASE) //如果有进位
{ carry = 1;
sum -= BASE;
}
PI[x] =(SHORT)sum;
}
}
else
{ LONG borrow, sum;
borrow = 0;
for (x =size -1; x >= 0;x--)
{ sum = (LONG)pi[x] - (LONG)term[x] - borrow;
borrow =0;
if (sum<0) //如果有错位
{ borrow =1;
sum +=BASE;
}
pi[x] = (SHORT)sum;
}
}
sign = -sign; //符号取反
divisor +=2; //分母加2
NotZero =0;
for (x = adv;x < size; x++)
{ if(powers[x])
{ break;
} NotZero = 1;

}
if (NotZero)
{ while (powers[adv] == 0) //跳过全为0的项
adv++;
}
}
}
int main(void)
{ PI pi(23);
pi.Show();
PI p1;
p1.SetSize(200);
p1.Show();
return 0;
}
搜索更多相关主题的帖子: 圆周率 运行 
2006-07-06 15:01
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 
比较复杂,注释过少。
有空才能帮你看

http://kongfuziandlife. http://codeanddesign.
2006-07-06 18:55
快速回复:我的求圆周率的程序怎么不能运行?帮个忙!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039761 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved