问题描述
- 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("求两个任意长整数的和 "); printf("输入时每四位用逗号隔开 如:123456789 "); printf("输入格式为:1,2345,6789 "); 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(" "); }//控制输出的格式 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不知道是哪种情况
解决方案
这个应该就是两个长整数的加法啊,通过链表实现每一位分别相加的
解决方案二:
代码粘贴太乱,标出来哪里不懂。
解决方案三:
[程序猿感悟] Understanding your own code(看不懂自己代码的程序猿不可原谅)
解决方案四:
建议你看下链表的定义,知道如何建立链表,然后结合代码看
时间: 2024-12-09 23:32:25