社区
数据结构与算法
帖子详情
请问谁有高精度除高精度的C程序
TESTMYACM
2005-03-23 11:05:06
加精
^-^
如果能象java那样能集成所有的高精度运算
只需要用户提供输入
是最完美的了
...全文
246
4
打赏
收藏
请问谁有高精度除高精度的C程序
^-^ 如果能象java那样能集成所有的高精度运算 只需要用户提供输入 是最完美的了
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
rickone
2005-04-08
打赏
举报
回复
不知道你的高精度是用的二进制还是十进制?
TESTMYACM
2005-03-28
打赏
举报
回复
谢谢javablind的帮助:)
javablind
2005-03-23
打赏
举报
回复
这是我以前写过的字符串操作实现大整数的程序,希望有点用
#include<stdio.h>
#include<string.h>
#include<memory.h>
#define MAX 1000
void reverse(char *p)
{//逆序存放
int i=(int)strlen(p),j;
char temp;
for(j=0;j<i/2;j++)
{
temp=p[j];
p[j]=p[i-j-1];
p[i-j-1]=temp;
}
}
int compare(char *a,char *b)
{//比较两正数大小
if(strlen(a)>strlen(b)) return 1;
if(strlen(a)<strlen(b)) return -1;
if(strcmp(a,b)>0) return 1;
if(strcmp(a,b)<0) return -1;
return 0;
}
void cutzero(char *temp)
{//去首零
int i=0;
int flag=0;
if(temp[0]=='+'||temp[0]=='-')
{
i++;
flag=1;
}
while(temp[i]=='0') i++;
int j=0;
if(flag) temp[j++]=temp[0];
int len=(int)strlen(temp);
for(;i<=len;i++,j++)
{
temp[j]=temp[i];
}
if(temp[0]=='\0')
{
temp[0]='0';
temp[1]='\0';
}
}
void add(char *c,char *a,char *b)
{//a加b结果放入c中 a,b都为正数
char A[MAX],B[MAX];
if(strlen(a)<strlen(b))
{
strcpy(A,b); strcpy(B,a);
}
else
{
strcpy(A,a); strcpy(B,b);
}
reverse(A);
reverse(B);
int carry=0;//进位
int i=0,temp;
int flag=1;
while(A[i])
{
if(flag) temp=A[i]+B[i]-96;
else temp=A[i]-48;
c[i]=(temp+carry)%10+'0';
carry=(temp+carry)/10;
i++;
if(flag&&!B[i]) flag=0;
}
if(carry==0) c[i]='\0';
else
{
c[i]=carry+'0';
c[i+1]='\0';
}
reverse(c);
}
void mul(char *c,char *a,char *b)
{//a乘b结果放入c中 a,b都为正数
char A[MAX],B[MAX];
if(a[0]=='0'||b[0]=='0')
{
c[0]='0'; c[1]='\0'; return;
}
memset(c,'0',MAX);
strcpy(A,a);
strcpy(B,b);
reverse(A);
reverse(B);
int carry;//进位
int i=0,j,temp1,temp2;
while(B[i])
{
j=0;
carry=0;
while(A[j])
{
temp1=(A[j]-48)*(B[i]-48);
temp2=c[i+j]-'0';
c[i+j]=(temp1+temp2+carry)%10+'0';
carry=(temp1+temp2+carry)/10;
j++;
}
if(carry!=0) c[i+j]=carry+'0';
i++;
}
if(c[i+j-1]=='0') c[i+j-1]='\0';
else c[i+j]='\0';
reverse(c);
}
void sub(char *c,char *a,char *b)
{//a减b结果放入c中 a,b都为正数
char A[MAX],B[MAX];
int flag=1;
if(compare(a,b)<0) flag=0;
if(flag==0)
{
strcpy(A,b); strcpy(B,a);
}
else
{
strcpy(A,a); strcpy(B,b);
}
reverse(A);
reverse(B);
int carry=0;//借位
int i=0,temp;
int mark=1;
while(A[i])
{
if(mark) temp=A[i]-B[i]-carry;
else temp=A[i]-carry-48;
if(temp<0)
{
carry=1; temp+=10;
}
else carry=0;
c[i++]=temp%10+'0';
if(mark&&!B[i]) mark=0;
}
if(c[i-1]=='0')
{
if(flag==0)
{
c[i-1]='-'; c[i]='\0';
}
else c[i-1]='\0';
}
else
{
if(flag==0)
{
c[i]='-'; c[i+1]='\0';
}
else c[i]='\0';
}
reverse(c);
cutzero(c);
}
void div(char *d,char *m,char *a,char *b)
{// a/b 商d 余数m
//调用add sub mul
if(compare(a,b)<0)
{
d[0]='0'; d[1]='\0';
strcpy(m,a); return;
}
char A[MAX],B[MAX];
strcpy(A,a); strcpy(B,b);
char temp[MAX+1],havetry[MAX],tryd[2];
memset(temp,'\0',MAX+1);
int ina=strlen(B),ind=0,i,flag;
int len_A=(int)strlen(A);
strncpy(temp,A,ina);
while(ina<len_A+1)
{
flag=0;
while(compare(temp,B)<0)
{
if(ina==len_A)
{
flag=1;
d[ind++]='0';
break;
}
i=(int)strlen(temp);
temp[i++]=A[ina++];
temp[i]='\0';
cutzero(temp);
d[ind++]='0';
}
if(flag==1) break;
if(strlen(temp)==strlen(B)) i=(temp[0]-'0')/(B[0]-'0'+1);
else i=((temp[0]-'0')*10+temp[1]-'0')/(B[0]-'0'+1);
if(i<=1) i=2;
while(1)
{
tryd[0]=i+'0';
tryd[1]='\0';
mul(havetry,B,tryd);
if(compare(havetry,temp)>0) break;
i++;
}
i--;
d[ind++]=i+'0';
add(temp,temp,B);
sub(temp,temp,havetry);
i=strlen(temp);
temp[i++]=A[ina++];
temp[i]='\0';
cutzero(temp);
}
cutzero(temp);
strcpy(m,temp);
if(m[0]=='\0')
{
m[0]='0'; m[1]='\0';
}
d[ind]='\0';
cutzero(d);
}
void transfer(char result[MAX],int n)
{//整数n->字符串reuslt
if(n==0)
{
result[0]='0';
result[1]='\0';
return;
}
int i=0,temp=n;
while(temp>0)
{
result[i++]=temp%10+'0';
temp=temp/10;
}
result[i--]='\0';
int length=(i+1)/2;
int j;
for(j=0;j<length;j++,i--)
{
temp=result[i];
result[i]=result[j];
result[j]=temp;
}
}
int re_transfer(char result[MAX])
{//字符串result->整数n
int count=0;
int i;
for(i=0;result[i];i++)
count=count*10+result[i]-'0';
return count;
}
void JieC(char result[MAX],int m)
{//计算m! ->result
strcpy(result,"1");
int i=2;
char temp1[MAX],temp2[MAX];
while(i<=m)
{
transfer(temp1,i);
mul(temp2,result,temp1);
strcpy(result,temp2);
i++;
}
}
void Cmn(char result[MAX],int m,int n)
{//计算C(m,n) (m>=n)
if(n>m/2) n=m-n;
strcpy(result,"1");
int i,j;
char temp1[MAX],temp2[MAX],temp3[MAX];
for(i=m,j=1;i>=m-n+1;i--,j++)
{
transfer(temp1,i);
mul(temp2,result,temp1);
transfer(temp1,j);
div(result,temp3,temp2,temp1);
}
}
int main()
{
char a[MAX],b[MAX],c[MAX],d[MAX];
while(1)
{
scanf("%s%s",a,b);
div(c,d,a,b);
printf("%s %s\n",c,d);
}
return 1;
}
mathe
2005-03-23
打赏
举报
回复
呵呵,不如你安装一台Linux,只要在上面使用bc命令就可以了。
高精度
加减乘除阶乘 C语言描述
C语言描述的
高精度
算法,为单独可编译运行的
程序
。 包含:
高精度
加法
高精度
减法
高精度
乘法
高精度
除以低精度
高精度
除以
高精度
高精度
阶乘
C语言-
高精度
加法
C语言-
高精度
加法,C语言
程序
设计实验之
高精度
加法
电子罗盘 C
程序
高精度
电子罗盘 C
程序
高精度
高精度
电子罗盘 HMR5883
用C语言实现
高精度
加法
用户输入两个任意长度的数字,
程序
能够算出二者之和,利用c语言实现了
高精度
。
TM7711厨房秤0.1克
高精度
滤波算法(c语言).zip
TM7711厨房秤
高精度
滤波算法,0.1g时对应15个内码,内码跳动小于5
数据结构与算法
33,008
社区成员
35,326
社区内容
发帖
与我相关
我的任务
数据结构与算法
数据结构与算法相关内容讨论专区
复制链接
扫一扫
分享
社区描述
数据结构与算法相关内容讨论专区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章