⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gai.cpp

📁 实现一个超过了计算机所表示范围的长整数的加减法
💻 CPP
字号:
#include <stdio.h>
#include<iostream.h>
#define null 0
#define len sizeof (struct dnode)
struct dnode
{char data1;
int data;
struct dnode *next;
struct dnode *prior;
};

count(struct dnode *head)
{struct dnode *p;
 int i;
 i=1;
for(p=head->next;p!=head;p=p->next)
  i=i+1;
return i;
}


struct dnode *creat()
{
struct dnode *head;
struct dnode *p,*s;
int x;
char c;
head=(struct dnode*)malloc(len);
p=head;
printf("please input mark:\n");
c=getchar();
while(c==10)
	c=getchar();
head->data1=c;
while(c!=48)
{ printf("please input data:\n");
  scanf("%d",&x);
s=(struct dnode *)malloc(len);
s->data=x;
p->next=s;
s->prior=p;
     p=s;
printf("please input mark\n if the data isnot over please input 1\n else please input  0:\n");
c=getchar();
c=getchar();
}
head->prior=p;
p->next=head;
return head;
}



void print(struct dnode *head)
{
struct dnode *w;
w=head->next;
printf("\n%c",head->data1);
while(w!=head)
   {
    if(w->data==0&&w->prior!=head)
     {
       printf("0000");
       if(w->next!=head)
         printf(",");
     }
    if(w->data>0&&w->data<10&&w->prior!=head)
     {
       printf("000%d",w->data);
       if(w->next!=head)
         printf(",");
     }
    if(w->data>=10&&w->data<100&&w->prior!=head)
     {
       printf("00%d",w->data);
       if(w->next!=head)
         printf(",");
     }
    if(w->data>=100&&w->data<1000&&w->prior!=head)
     {
       printf("0%d",w->data);
       if(w->next!=head)
         printf(",");
     }
    if(w->data>=1000&&w->data<10000&&w->prior!=head)
     {
       printf("%d",w->data);
       if(w->next!=head)
	 printf(",");
     }
    if(w->data>=0&&w->data<10000&&w->prior==head)
     {
       printf("%d",w->data);
       if(w->next!=head)
         printf(",");
     }
   w=w->next;
   }
printf("\n");
}


struct dnode *contary(struct dnode*head1,struct dnode*head2)
{
struct dnode *head;
struct dnode *p,*q,*w,*v;
head=(struct dnode*)malloc(len);
w=head;
head->data1=head1->data1;
p=head1->prior;
q=head2->prior;
while(q!=head2)
 {v=(struct dnode*)malloc(len);
  if((p->data)>=(q->data))
     v->data=p->data-q->data;
  else
      {v->data=(p->data)+10000-(q->data);
       p->prior->data=p->prior->data-1;
      }
  w->prior=v;
  v->next=w;
  w=v;
  p=p->prior;
  q=q->prior;
 }
while(p!=head1)
 { v=(struct dnode*)malloc(len);
   if(p->data<0)
     {v->data=p->data+10000;
      p->prior->data=p->prior->data-1;
     }
  else
     v->data=p->data;
  w->prior=v;
  v->next=w;
  w=v;
  p=p->prior;
 }
head->next=w;
w->prior=head;
return head;
}

struct dnode *resultnode(struct dnode*head1,struct dnode*head2)
{struct dnode *head;
struct dnode *p,*q,*w,*v;
int f,i,j;
f=0;
i=0;
j=0;
head=(struct dnode*)malloc(len);
w=head;
if(head1->data1==head2->data1)
 {
   head->data1=head1->data1;
    p=head1->prior;
    q=head2->prior;
while(p!=head1&&q!=head2)
  {
   v=(struct dnode*)malloc(len);
   v->data=p->data+q->data+f;
   if(v->data>=10000)
     {v->data=v->data-10000;
       f=1;
     }
   else f=0;
   w->prior=v;
   v->next=w;
   w=v;
   p=p->prior;
   q=q->prior;
  }
if(p==head1&&q==head2&&f==1)
  { v=(struct dnode*)malloc(len);
    v->data=f;
    w->prior=v;
    v->next=w;
    w=v;
  }
while(p!=head1&&q==head2)
  {
   v=(struct dnode*)malloc(len);
   v->data=p->data+f;
   if(v->data>=10000)
    {
     v->data=v->data-10000;
     f=1;
    }
   else f=0;
   w->prior=v;
   v->next=w;
   w=v;
   p=p->prior;
  }
while(p==head1&&q!=head2)
  {
  v=(struct dnode*)malloc(len);
  v->data=q->data+f;
  if(v->data>=10000)
   {
     v->data=v->data-10000;
     f=1;
   }
  else f=0;
  w->prior=v;
  v->next=w;
  w=v;
  q=q->prior;
  }
head->next=w;
w->prior=head;
 }

if(head1->data1!=head2->data1)
 {
  i=count(head1);
  j=count(head2);
  if(i>j)
  head=contary(head1,head2);
  else
    if(i<j)
   head=contary(head2,head1);
    else
   { p=head1->next;
    q=head2->next;
   if(p->data>q->data)
      head=contary(head1,head2);
   else
     if(p->data<q->data)
      head=contary(head2,head1);
     else
     if(p->data==q->data)
      { int u=0;
        p=p->next;
        q=q->next;
        while(p!=head1&&q!=head2)
         {
           if(p->data>q->data)
             {
             head=contary(head1,head2);
             u=1;
             break;
             }
           else
           if(p->data<q->data)
             {
             head=contary(head2,head1);
             u=1;
             break;
             }
           else
           if(p->data==q->data)
             {
               p=p->next;
               q=q->next;
             }
          }
        if(u==0)
	   {head->data1=0;
            v=(struct dnode*)malloc(len);
            v->data=0;
            w->prior=v;
            v->next=w;
            w->next=v;
            v->prior=w;
             w=v;
            }


      }
  }
}
w=head->next;
v=w;
while(w->data==0&&w->prior==head&&w->next!=head)
        { v=v->next;
          w->prior->next=w->next;
          w->next->prior=w->prior;
          free(w);
          w=v;
        }
 return head;
}

void main()
{
	struct dnode *head,*head1,*head2;
	printf("please input number1:\n");
	head1=creat();
	printf("please input number:\n");
head2=creat();
printf("number 1 is:     ");
print(head1);
printf("\n");
printf("number 2 is:     ");
print(head2);
printf("\n");
head=resultnode(head1,head2);
printf("the result is:     ");
print(head);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -