两个数相乘,小数点后位数没有限制,请写一个高精度算法 c语言,两个数相乘,小数点后位数没有限制,请写一个高精度算法【转】...

变为整数求就行了.输入的时候记一下,小数点位置..输出再做点文章就行了.

下面的是大整数的运算.

#include

using namespace std;

#define MAX 10000

struct Node{

int data;

Node *next;

};

void output(Node *head)

{

if(!head->next&&!head->data)return;

output(head->next);

cout<data;

}

void Mul(char *a,char *b,int pos)

{

char *ap=a,*bp=b;

Node *head=0;

head=new Node;head->data=0,head->next=0;   //头

Node *p,*q=head,*p1;

int temp=0,temp1,bbit;

while(*bp)                //若乘数不为空 ,继续.

{

p=q->next;p1=q;

bbit=*bp-48;          //把当前位转为整型

while(*ap||temp)            //若被乘数不空,继续

{

if(!p)            //若要操作的结点为空,申请之

{

p=new Node;

p->data=0;

p->next=0;

p1->next=p;

}

if(*ap==0)temp1=temp;

else { temp1=(p1->data)+(*ap-48)*bbit+temp;ap++; }

p1->data=temp1%10;    //留当前位

temp=temp1/10;    //进位以int的形式留下.

p1=p;p=p->next;                 //被乘数到下一位

}

ap=a;bp++;q=q->next;                //q进下一位

}

p=head;

output(p);                   //显示

cout<

while(head)                 //释放空间

{

p=head->next;

delete head;

head=p;

}

}

int main()

{

cout<

char test1[MAX],test2[MAX];

cin.getline(test1,MAX,'\n');

cin.getline(test2,MAX,'\n');

Mul(strrev(test1),strrev(test2));

system("PAUSE");

return 0;

}

上面大整数已经写了.你加几个东西就行了.

#include

using namespace std;

#define MAX 10000

struct Node{

int data;

Node *next;

};

void output(Node *head,int pos)

{

if(!head->next&&!head->data)return;

output(head->next,pos-1);

cout<data;

if(!pos)cout<

}

void Mul(char *a,char *b,int pos)

{

char *ap=a,*bp=b;

Node *head=0;

head=new Node;head->data=0,head->next=0;   //头

Node *p,*q=head,*p1;

int temp=0,temp1,bbit;

while(*bp)                //若乘数不为空 ,继续.

{

p=q->next;p1=q;

bbit=*bp-48;          //把当前位转为整型

while(*ap||temp)            //若被乘数不空,继续

{

if(!p)            //若要操作的结点为空,申请之

{

p=new Node;

p->data=0;

p->next=0;

p1->next=p;

}

if(*ap==0)temp1=temp;

else { temp1=(p1->data)+(*ap-48)*bbit+temp;ap++; }

p1->data=temp1%10;    //留当前位

temp=temp1/10;    //进位以int的形式留下.

p1=p;p=p->next;                 //被乘数到下一位

}

ap=a;bp++;q=q->next;                //q进下一位

}

p=head;

output(p,pos);                   //显示

cout<

while(head)                 //释放空间

{

p=head->next;

delete head;

head=p;

}

}

int main()

{

cout<

char test1[MAX],test2[MAX],*p;

int pos=0;

cin.getline(test1,MAX,'\n');

cin.getline(test2,MAX,'\n');

if(p=strchr(test1,'.'))

{

pos+=strlen(test1)-(p-test1)-1;

do

{

p++;

*(p-1)=*p;

}while(*p);

}

if(p=strchr(test2,'.'))

{

pos+=strlen(test2)-(p-test2)-1;

do

{

p++;

*(p-1)=*p;

}while(*p);

}

Mul(strrev(test1),strrev(test2),pos);

system("PAUSE");

return 0;

}

本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/hanlin1985/archive/2008/10/10/3048605.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值