程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 注釋-C++下面一段代碼中的if的情況感覺不懂

注釋-C++下面一段代碼中的if的情況感覺不懂

編輯:編程綜合問答
C++下面一段代碼中的if的情況感覺不懂
#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如:123456789\n\n");
    printf("輸入格式為:1,2345,6789\n\n");
    printf("輸入整數1:");
    num1=create(num1);//建立第一個新結點 
    printf("輸入整數2:"); 
    num2=create(num2);//建立第二個新結點 
    printf("\n兩整數的和為:");
    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;//如果長整數大於0則把符號保存在頭結點中 
  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)
{
  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);
 }

我自己理解加了注釋但不知道對不對,if之後的分情況討論感覺不能讀懂
就是data*e1 e1*e2==1不知道是哪種情況

最佳回答:


這個應該就是兩個長整數的加法啊,通過鏈表實現每一位分別相加的

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