程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 鏈表-關於長整數的加法,怎麼修改下面的代碼

鏈表-關於長整數的加法,怎麼修改下面的代碼

編輯:編程綜合問答
關於長整數的加法,怎麼修改下面的代碼
 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node{
    int data;
    struct node *prior;
    struct node *next;
} node;//雙向鏈表結構體定義
int main()
{
    node *num1,*num2,*sum;
    node *create(node *n);
    int output(node *n);
    node *add_Num(node *n1,node *n2);
    num1=(node *)malloc(sizeof(node));
    num2=(node *)malloc(sizeof(node));
    printf("********************************\n\n");
    printf("*                              *\n\n");
    printf("*     求兩個任意長整數的和     *\n\n"); 
    printf("*                              *\n\n");
    printf("********************************\n\n");
    printf("* 注意:輸入時每四位用逗號隔開 *\n\n");
    printf("*        如:123456789         *\n\n");
    printf("*    輸入格式為:1,2345,6789   *\n\n");  
    printf("請輸入整數1:");
    num1=create(num1);//建立第一個新結點 
    printf("請輸入整數2:"); 
    num2=create(num2);//建立第二個新結點 
    printf("兩整數的和為:");
    sum=add_Num(num1,num2);
    output(sum);
}//定義主函數 
int output(node *n)
{
  node *p;
  if(n->data<0) //判斷輸出結果的正負 
  printf("-");
  printf("%d,",n->next->data);
  p=n->next->next;
  while(p!=n->next)
  {
    if(p->data<1000) printf("0");
    if(p->data<100) printf("0");
    if(p->data<10) printf("0");
    printf("%d,",p->data);
    p=p->next;
   }
  printf("\b \b\n"); 
 }//控制輸出的格式 
node *create(node *n)
{//創建一個雙向鏈表 
  node *p,*t;
  int data,i;
  t=(node *)malloc(sizeof(node));//第一個結點 
  i=scanf("%d",&data);
  if(data<0)
  n->data=-1;//如果長整數小於0則把符號保存在頭結點中 
  else 
  n->data=1;
  t->data=n->data*data;
  n->next=t;
  t->next=t;
  t->prior=t;
  n->prior=NULL;
  p=n->next;
  while(i!=0)//以後結點 
  {
    if(getchar()==',')//如果輸入時“,”的話則執行if語句 
    i=scanf("%d",&data);
    else break;
    t=(node *)malloc(sizeof(node));
    t->data=data;
    p->next=t;
    t->prior=p;
    t->next=n->next;
    n->next->prior=t;
    p=p->next;
   }
  return(n);
 }
node *add_Num(node *n1,node *n2)  //完成加法操作返回指向*n1+*n2結果的指針
{
  node *p1,*p2,*t,*s,*sum;
  int e1,e2,carry;
  p1=n1->next->prior;
  p2=n2->next->prior;
  e1=n1->data;
  e2=n2->data;
  sum=(node *)malloc(sizeof(node));
  sum->data=1;
  sum->prior=NULL;
  s=(node *)malloc(sizeof(node));
  s->data=p1->data*e1+p2->data*e2;
  p1=p1->prior;
  p2=p2->prior;
  if(e1*e2==1)
  {
    sum->data=e1;
    s->data=abs(s->data);
    if(s->data>=10000)//相加大於等於10000則進位 
    {
      s->data-=10000;
      carry=1;
     }
    else carry=0;//否則不進位 
   }
  else
    if(s->data>=0) 
    carry=0;
    else
    {
      s->data+=10000;
      carry=-1;
     }
  t=s;
  s->next=s;
  s->prior=s;
  sum->next=s;
  while(p1!=n1->next->prior&&p2!=n2->next->prior)
  {
    s=(node *)malloc(sizeof(node));
    s->data=p1->data*e1+p2->data*e2+sum->data*carry;
    p1=p1->prior;
    p2=p2->prior;
    if(e1*e2==1)
    {
      sum->data=e1;
      s->data=abs(s->data);
      if(s->data>=10000)
      {
        s->data-=10000;
        carry=1;
       }
      else carry=0;
     }
    else
      if(s->data>=0) carry=0;
      else
      {
        s->data+=10000;
        carry=-1;
       }
    s->next=t;
    t->prior=s;
    s->prior=sum->next;
    sum->next->next=s;
    t=t->prior;
   }

  if(p1==n1->next->prior)
    while(p2!=n2->next->prior)
    {
      s=(node *)malloc(sizeof(node));
      s->data=p2->data*e2+sum->data*carry;
      p2=p2->prior;
      if(e1*e2==1)
      {
        sum->data=e1;
        s->data=abs(s->data);
        if(s->data>=10000)
        {
          s->data-=10000;
          carry=1;
         }
        else carry=0;
       }
      else
        if(s->data>=0) carry=0;
        else
        {
          s->data+=10000;
          carry=-1;
         }
      s->next=t;
      t->prior=s;
      s->prior=sum->next;
      sum->next->next=s;
      t=t->prior;
     }
  if(p2==n2->next->prior)
    while(p1!=n1->next->prior)
    {
      s=(node *)malloc(sizeof(node));
      s->data=p1->data*e1+sum->data*carry;
      p1=p1->prior;
      if(e1*e2==1)
      {
        sum->data=e1;
        s->data=abs(s->data);
        if(s->data>=10000)
        {
          s->data-=10000;
          carry=1;
         }
        else carry=0;
       }
      else
        if(s->data>=0) carry=0;
        else
        {
          s->data+=10000;
          carry=-1;
         }
      s->next=t;
      t->prior=s;
      s->prior=sum->next;
      sum->next->next=s;
      t=t->prior;
     }
  if(carry!=0)//如果有進位 
  {
    s=(node *)malloc(sizeof(node));
    s->data=1;
    carry=0;
    s->next=t;
    t->prior=s;
    s->prior=sum->next;
    sum->next->next=s;
    t=t->prior;
   }
  sum->next=t;
  do
  {
    if(t->data==0)
    {
      sum->next=t->next;
      t->next->prior=t->prior;
      t->prior->next=t->next;
      free(t);
     }
    else break;
    t=sum->next;
   }
   while(t!=sum->next->prior);
  return(sum);
 }

這個代碼裡
如果兩個異號的長整數相加結果是負數的話輸出結果就不對了
比如-2和1 的輸出結果就是-1,9999
而且0和0的相加也不對
請問應該怎麼修改

最佳回答:


參考:http://blog.csdn.net/hanangellove/article/details/42486723
http://blog.sina.com.cn/s/blog_62bc9a330100gix5.html
http://wenku.baidu.com/link?url=t0TobqGl2GCvX0PbxuNAqxkP-QKAuZWXf_2NIB9LArH6TwO5T5Kp4ZkJ_sPJvwzDubXLVL0DH6Yx2TfjJY94lKDFql9a2RWcNtza1ZKTezC

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved