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

📄 1aaa.cpp

📁 《数据结构-使用C语言》第三版
💻 CPP
字号:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include<math.h>

typedef int DataType;

#include"DLNode.h"

int main()
{
	DLNode *heada, *headb, *headc, *p;
	
	void Input(DLNode *head);
	void Add(DLNode *la, DLNode *lb, DLNode *lc);
	
	int i, j, t;
	
	while(1)
	{
		ListInitiate(&heada);  //初始化 
		ListInitiate(&headb);
		ListInitiate(&headc);
		
		Input(heada);  //输入加数 
		Input(headb);  //输入被加数 
		
		ListPrintPrior(heada);  //打印链表中的数据,高位在前 
		ListPrintPrior(headb);  
		
		Add(heada,headb,headc);
		
		p=headc->prior;
		while(p->prior!=headc && p->data==0)
		{
			p=p->prior;
		}
		printf("%d ",p->data);
		//p=p->prior;
		while(p!=headc)
		{
			j=0;
			t=p->data;
			while(t!=0)
			{
				t/=10;
				j++;
			}
			for(i=0;i<4-j;i++)printf("0");
			if(j!=0)printf("%d ",t);
			else printf(" ");
		}
		printf("\n");
		
		ListDestory(&heada);
		ListDestory(&headb);
		ListDestory(&headc);
	}
	return 0;
}

void Add(DLNode *la, DLNode *lb, DLNode *lc)
{
	DLNode *p, *q, *s;
	int t,len;

	p=la->next;
	q=lb->next;
		
	while(p!=la && q!=lb)
	{
		t=p->data+q->data;
		if(ListInsert(lc, 0, t)==0)
		{
			printf("error!");
			return ;
		}
		p=p->next;
		q=q->next;
	}
	if(p!=la)
	{
		while(p!=la)
		{
			t=p->data;
			if(ListInsert(lc, 0, t)==0)
			{
				printf("error!");
				return ;
			}
			p=p->next;
		}
	}
	if(q!=lb)
	{
		while(q!=lb)
		{
			t=q->data;
			if(ListInsert(lc, 0, t)==0)
			{
				printf("error!");
				return ;
			}
			q=q->next;
		}
	}
	
	ListPrintNext(lc);  //打印链表 lc  

	s=lc->prior;
	while(s!=lc)
	{
		if(s->data>9999)
		{
			s->data-=10000;
			if(s->prior!=lc) s->prior->data++;
			else 
			{
				if(ListInsert(lc, 0, 1)==0)
				{
					printf("error!");
					return ;
				}
			}
		}
		else if(s->data<0)
		{
			s->data+=10000;
			if(s->prior!=lc) s->prior->data--;
			else 
			{
				if(ListInsert(lc, 0, -1)==0)
				{
					printf("error!");
					return ;
				}
			}
		}
		s=s->prior;
	}
}

void Input(DLNode *head)
{
	int i, j, t, len;
	char str[100];
	 
	scanf("%s",str);  //输入
	len=strlen(str);
		
	if(str[0]=='-')head->data=1;  //判别数的正负 
	else head->data=0;
		
	for(i=len-1; i>=head->data; i--)   //从低位开始扫描,每四位一起 
	{//负数到下标为 1 结束,正数到下标为 0 结束 
			
		t=0; 
		j=0; 
		while(j<4 && i>=head->data)
		{
			t+=(int)(pow(10,j))*(str[i]-'0');
			j++;
			i--;
		}
		if(head->data==1)t=-t;
		if(ListInsert(head, 0, t)==0)  //插入到双向链表中  高位在后面的节点中 
		{
			printf("error!");
			return ;
		}
		i++;
	}
}

⌨️ 快捷键说明

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