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

📄 4a.c

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

#define MAX 100
typedef int DataType;

#include "SLNode.h"

int len;
char stra[MAX],strb[MAX];
int main()
{
	void Print( SLNode *head);
	void Creat( SLNode *head, char s[]);
	void Combination(SLNode *heada, SLNode *headb, SLNode *headc);
	SLNode *heada, *headb, *headc;
	while(scanf("%s%s",stra,strb)!=EOF)//输入的表达式必须是按指数递增 
	{
		ListInitiate(&heada);
		ListInitiate(&headb);
		ListInitiate(&headc);
		
		Creat( heada, stra );
		Creat( headb, strb );
	
		//ListPrint( heada );
		//ListPrint( headb );
	
		Combination(heada, headb, headc);
	
		//ListPrint( headc );
		Print(headc);
	}
	return 0;
}

void Creat( SLNode *head , char s[] )
{
	int i, j, k, len, flag, f, t;
	char str[MAX];
	SLNode *p;
	
	j=0;
	len=strlen(s);
	
	if(s[1]=='x')k=0;
	else
	{
		if(s[1]=='-')k=2;
		else k=1;
		while(s[k]>='0' && s[k]<='9'){k++;}
		if(s[k]!='x')
		{
			for(i=0; i<k; i++)
			str[j++]=s[i];
			
			str[j++]='x';
			str[j++]='0';
			str[j++]=s[k];
			k++;
		}
		else k=0;
	}
	for(i=k;i<len;i++)
	{
		if(s[i]=='x')
		{
			if(s[i-1]<'0' || s[i-1]>'9')str[j++]='1';
			str[j++]=s[i];
			if(s[i+1]<'0' || s[i+1]>'9')str[j++]='1';
		}
		else str[j++]=s[i];
	}
	str[j]='\0';
	//printf("%s\n",str);
		
	p=head;
	flag=0;
	f=0;
	
	for(i=0;i<j;i++)
	{
		if(str[i]=='-')  
		{//判断输入的系数或指数的正负 
			flag=1;
			continue;
		}
		else if(str[i]>='0'&&str[i]<='9')  
		{//把输入的数插入响应的链表中 
			t=0;
			while(str[i]>='0' && str[i]<='9')
			{
				t=10*t+(str[i]-'0');
				i++;
			}
			if(flag==1)
			{
				t=-t;
				flag=0;
			}
			if(f==0)  //是系数 
			{
				if(ListInsert( head, 0, t)==0)
				{
					printf("error!\n");
					return ;
				}
				f=1;  //判断数是系数还是指数 
			}
			else if(f==1)  //是指数 
			{
				head->next->d=t;
				f=0;
			}
			i--;
		}
	}
}

void Combination(SLNode *heada, SLNode *headb, SLNode *headc)
{
	SLNode *p, *q;
	p=heada->next;
	q=headb->next;
	
	while(p!=NULL && q!=NULL)
	{
		if(p->d > q->d )
		{
			if(ListInsert( headc, 0, p->data)==0)
			{
				printf("error!\n");
				return ;
			}
			headc->next->d = p->d;
			p=p->next;
		}
		else if(p->d < q->d)
		{
			if(ListInsert( headc, 0, q->data)==0)
			{
				printf("error!\n");
				return ;
			}
			headc->next->d = q->d;
		    q=q->next;
		}
		else if(p->d == q->d && p->data+q->data!=0)
		{
			if(ListInsert( headc, 0, p->data+q->data)==0)
			{
				printf("error!\n");
				return ;
			}
			headc->next->d = p->d;
		    p=p->next;
			q=q->next;
		}
		else if(p->d == q->d && p->data+q->data==0)
		{
			p=p->next;
			q=q->next;
		}
	}
	while(p!=NULL)
	{
		if(ListInsert( headc, 0, p->data)==0)
		{
			printf("error!\n");
			return ;
		}
		headc->next->d = p->d;
		p=p->next;
	}
	while(q!=NULL)
	{
		if(ListInsert( headc, 0, q->data)==0)
		{
			printf("error!\n");
			return ;
		}
		headc->next->d = q->d;
		q=q->next;
	}
}

void Print( SLNode *head)
{
	printf("CASE :");
	SLNode *p;
	p=head;
	if(p->next==NULL)printf("0\n\n");
	else 
	{
		p=p->next;
		while(p!=NULL)
		{
			if(p->d == 0)printf("%d",abs(p->data));
			else if(p->d == 1)
			{
				if(p->data==1)printf("x");
				else printf("%dx",abs(p->data));
			}
			else
			{
				if(p->data == 1)printf("x%d",abs(p->d));
				else printf("%dx%d",abs(p->data),abs(p->d));
			}
			p=p->next;
			if(p!=NULL)
			{
				if(p->data < 0)printf("-");
				else printf("+");
			}
		}
		printf("\n\n");
	}
}

⌨️ 快捷键说明

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