读书人

长整数计算器求修改解决思路

发布时间: 2012-04-13 13:50:24 作者: rapoo

长整数计算器求修改

C/C++ code
#include <iostream>using namespace std;#include "计算器头文件.h"int main(){    char x[100]={0},y[100]={0},a[100]={0},b[100]={0},c[2][100]={0},d[100]={0};    int n,m,len1,len2,i,j,k;    bit *head1,*head2,*head3=NULL;    char z,w;    cout<<"请选择运算类型(+/-/*):";    cin>>z;    cout<<"请输入第一个数:";    cin>>x;    cout<<"请输入第二个数:";    cin>>y;    while(z=='+'||z=='-'||z=='*')    {    if(x[0]=='+'||x[0]=='-')//将两数去符号        strcpy_s(a,x+1);    else strcpy_s(a,x);    if(y[0]=='+'||y[0]=='-')        strcpy_s(b,y+1);    else strcpy_s(b,y);    m=strlen(a);n=strlen(b);    for(i=0,len1=0;i<m;i++)//将两数去逗号    {        if(a[i]!=',')        {a[len1]=a[i];len1++;}    }    for(j=0,len2=0;j<n;j++)    {        if(b[j]!=',')        {b[len2]=b[j];len2++;}    }    if(len1>=len2)k=len1;//k取较大值    else k=len2;    k=k/3*3+3;//将长度统一为最接近的3的倍数    for(i=0;i<k;i++)//将两数前不足的位数补为‘0’    {        if(i<k-len1)c[0][i]='0';        else c[0][i]=a[i-(k-len1)];    }    for(i=0;i<k;i++)    {        if(i<k-len2)c[1][i]='0';        else c[1][i]=b[i-(k-len2)];    }    head1=create(c[0],k);    head2=create(c[1],k);    head1=reserve(head1);    head2=reserve(head2);if(z=='+')    {        if(x[0]!='-'&&y[0]!='-')        {            head1=plus(head1,head2,k);            head1=reserve(head1);            showlist(head1);        }        if(x[0]=='-'&&y[0]=='-')        {            head1=plus(head1,head2,k);            head1=reserve(head1);            cout<<"-";            showlist(head1);        }        if(x[0]!='-'&&y[0]=='-')        {            if(strcmp(c[0],c[1])>=0)            {                head1=minus(head1,head2,k);                head1=reserve(head1);                showlist(head1);            }            else             {                head1=minus(head2,head1,k);                head1=reserve(head1);                cout<<"-";                showlist(head1);            }        }        if(x[0]=='-'&&y[0]!='-')        {            if(strcmp(c[0],c[1])>=0)            {                head1=minus(head1,head2,k);                head1=reserve(head1);                cout<<"-";                showlist(head1);            }            else             {                head1=minus(head2,head1,k);                head1=reserve(head1);                showlist(head1);            }        }    }if(z=='-')    {        if(x[0]=='-'&&y[0]=='-')        {            if(strcmp(c[0],c[1])>=0)            {                head1=minus(head1,head2,k);                head1=reserve(head1);                cout<<"-";                showlist(head1);            }            else             {                head1=minus(head2,head1,k);                head1=reserve(head1);                showlist(head1);            }        }        if(x[0]!='-'&&y[0]!='-')        {            if(strcmp(c[0],c[1])>=0)            {                head1=minus(head1,head2,k);                head1=reserve(head1);                showlist(head1);            }            else             {                head1=minus(head2,head1,k);                head1=reserve(head1);                cout<<"-";                showlist(head1);            }        }        if(x[0]!='-'&&y[0]=='-')        {            head1=plus(head1,head2,k);            head1=reserve(head1);            showlist(head1);        }        if(x[0]=='-'&&y[0]!='-')        {            head1=plus(head1,head2,k);            head1=reserve(head1);            cout<<"-";            showlist(head1);        }    }if(z=='*'){    if((x[0]!='-'&&y[0]!='-')||(x[0]=='-'&&y[0]=='-'))    {        head3=create(d,2*k);        head3=multiply(head1,head2,head3);        head3=deal(head3);        head3=reserve(head3);        showlist(head3);    }    else     {        head3=create(d,2*k);        head3=multiply(head1,head2,head3);        head3=deal(head3);        head3=reserve(head3);        cout<<"-";        showlist(head3);    }}    cout<<"是否继续?(Y/N)";    cin>>w;    if(w=='Y'||w=='y')    {        cout<<"请选择运算类型(+/-/*):";    cin>>z;    cout<<"请输入第一个数:";    cin>>x;    cout<<"请输入第二个数:";    cin>>y;    }    else {cout<<"谢谢使用!"<<endl;break;}    }    }    //创建链表函数    bit *create(char *x,int n)    {        bit *s,*p,*head=NULL;        s=new bit;        s->data=x[0];        for(int i=1;i<=n;i++)        {            if(head==NULL) head=s;            else p->next=s;            p=s;            s=new bit;            s->data=x[i];        }        p->next=NULL;        delete s;        return head;    }    //反序链表    bit *reserve(bit *head)    {        bit *newhead=NULL,*newnext;        while(head)        {            newnext=head->next;            head->next=newhead;            newhead=head;            head=newnext;        }        return newhead;    }    //打印链表    void showlist(bit *head)    {        int i=0;        while(head->data=='0')        {head=head->next;i++;}        while(head)        {            cout<<head->data;            i++;            if(i%3==0&&head->next!=NULL){cout<<",";}            head=head->next;        }        cout<<endl;    }    //加法函数bit *plus(bit *head1,bit *head2,int k){    bit *p,*q,*r;    int i,j=0;    for(i=1,p=head1,q=head2;i<k;p=p->next,q=q->next,i++)    {        if(p->data-'0'+q->data-'0'+j>9)        {p->data=p->data+q->data-'0'+j-10;j=1;}        else {p->data=p->data+q->data+j-'0';j=0;}    if(i==k-1)        if(p->data-'0'+q->data-'0'>9)        {            r=new bit;            r->data='1';            p->next=r;            r->next=NULL;        }        else        {            r=new bit;            r->data='0';            p->next=r;            r->next=NULL;        }    }    return head1;}//减法函数bit *minus(bit *head1,bit *head2,int k){    bit *p,*q;    int i,j=0;    for(i=1,p=head1,q=head2;i<k;p=p->next,q=q->next,i++)    {        if(p->data-j<q->data)        {p->data=(p->data-'0')+10-j-(q->data-'0')+'0';j=1;}        else {p->data=(p->data-'0')-j-(q->data-'0')+'0';j=0;}    }    return head1;}//乘法函数bit *multiply(bit *head1,bit *head2,bit *head3){    bit *p,*q,*r;    int i,j,k;    for(j=0,q=head2;q!=NULL;q=q->next,j++)    {        for(i=0,p=head1;p!=NULL;p=p->next,i++)        {            for(k=0,r=head3;k<=i+j;r=r->next,k++)                if(k==i+j){r->data=r->data+(p->data-'0')*(q->data-'0');}        }    }    return head3;}bit *deal(bit *head3){    bit *p,*s;    int t;    for(p=head3;p->next!=NULL;p=p->next)    {        t=p->data/10;        p->data=p->data%10;        p->next->data=p->next->data+t;    }    if(p->data>=10)    {        s=new bit;        t=p->data/10;        p->data=p->data%10;        s->data=t;        p->next=s;        s->next=NULL;    }    for(p=head3;p!=NULL;p=p->next)    {        p->data=p->data+'0';    }    return head3;} 






请问乘法的部分的代码改怎样修改?
mutiply函数是将被乘数的第i位乘以乘数的第j位所得结果放在结果链表的第i+j位。
deal函数是处理进位的。
这样的思路正确吗?
请大神指教

[解决办法]
一般这么长的代码很少有人会耐心帮你调的,大家的时间都很宝贵。LZ需要学会自己单步调试

读书人网 >C++

热点推荐