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

📄 1aaaa.c

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

typedef int DataType;
#define MAX 100

#include"DLNode.h"

int flag, lena, lenb;
char stra[MAX],strb[MAX];
int main()
{
	DLNode *heada, *headb, *headc, *p, *q;
	
	void Input(DLNode *head, char str[]);
	void Judge(DLNode *heada, DLNode *headb, DLNode *headc);
	void Add(DLNode *la, DLNode *lb, DLNode *lc);
	
	int i, j, t;
	printf("直接输入加数和被加数就可以了,两数间用空格隔开\n"); 
	
	
	while(1)
	{
		flag=0;
		ListInitiate(&heada);  //初始化 
		ListInitiate(&headb);
		ListInitiate(&headc);
		heada->data=0;
		headb->data=0;
		headc->data=0;
		
		scanf("%s",stra);  //输入加数 
		lena=strlen(stra);
		Input(heada,stra);  
		
		scanf("%s",strb); //输入被加数 
		lenb=strlen(strb);
		Input(headb,strb);  
		
		//ListPrintNext(heada);  //打印链表中的数据,高位在前 
		//ListPrintNext(headb); 
		
		Judge(heada,headb,headc);
		
		//ListPrintNext(headc);
		
		printf("Case :");
		if(headc->data==1)printf("-"); 
		
		p=headc->next;
		while(p->next!=headc && p->data==0)  //去掉高位多余的 0 
		{
			p=p->next;
		}
		printf("%d ",p->data);
		p=p->next;
		while(p!=headc)
		{
			j=0;
			t=p->data;
			while(t!=0)    //中间不足4位数的用0补充 
			{
				t/=10;
				j++;
			}
			for(i=0;i<4-j;i++)printf("0");
			if(j!=0)printf("%d ",p->data);
			else printf(" ");   //结果每四位用空格分开 
			p=p->next;
		}
		printf("\n\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->prior;
	q=lb->prior;
		
	while(p!=la && q!=lb)
	{
		if(flag==0)t=p->data + q->data;
		else t=p->data - q->data;
		if(ListInsert(lc, 0, t)==0)
		{
			printf("error!");
			return ;
		}
		p=p->prior;
		q=q->prior;
	}
	if(p!=la)
	{
		while(p!=la)
		{
			t=p->data;
			if(ListInsert(lc, 0, t)==0)
			{
				printf("error!");
				return ;
			}
			p=p->prior;
		}
	}
	if(q!=lb)
	{
		while(q!=lb)
		{
			t=q->data;
			if(ListInsert(lc, 0, t)==0)
			{
				printf("error!");
				return ;
			}
			q=q->prior;
		}
	}
	
	//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 ;
				}
				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 ;
				}
				return ;
			}
		}
		s=s->prior;
	}
}

void Input(DLNode *head, char str[])
{
	int i, j, t, len;
		
	if(str[0]=='-')head->data=1;  //判别数的正负 
	else head->data=0;
	len=strlen(str);
	
	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)+0.5)*(str[i]-'0');
			j++;
			i--;
		}
		//if(flag)t=-t;
		if(ListInsert(head, 0, t)==0)  //插入到双向链表中  高位在后面的节点中 
		{
			printf("error!");
			return ;
		}
		i++;
	}
}

void Judge(DLNode *heada, DLNode *headb, DLNode *headc)
{
	int i,j,t;
	if(stra[0]!='-' && strb[0]!='-')
	{
		flag=0;
		headc->data=0;
		Add(heada,headb,headc);
	}
	else if(stra[0]=='-' && strb[0]=='-')
	{
		flag=0;
		headc->data=1;
		Add(heada,headb,headc);
	}
	else if( stra[0]=='-' )
	{
		flag=1;
		if(lena-1==lenb)
		{
			t=strcmp(stra+1,strb);
			if(t>=0)
			{	
				headc->data=1;
				Add(heada,headb,headc);
			}
			else if(t<0)
			{
				headc->data=0;
				Add(headb,heada,headc);
			}
		}
		else if(lena-1>lenb)
		{
			headc->data=1;
			Add(heada,headb,headc);
		}
		else if(lena-1<lenb)
		{
			headc->data=0;
			Add(headb,heada,headc);
		}
	}
	else if(strb[0]=='-')
	{
		flag=1;
		if(lena==lenb-1)
		{
			t=strcmp(stra,strb+1);
			if(t>=0)
			{	
				headc->data=0;
				Add(heada,headb,headc);
			}
			else if(t<0)
			{
				headc->data=1;
				Add(headb,heada,headc);
			}
		}
		else if(lena>lenb-1)
		{
			headc->data=0;
			Add(heada,headb,headc);
		}
		else if(lena<lenb-1)
		{
			headc->data=1;
			Add(headb,heada,headc);
		}
	}
}
		

⌨️ 快捷键说明

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