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

📄 arthur.c

📁 程序功能: 实现一元多项式的四则运算
💻 C
字号:
/////////////////////////////////////////////////
//   程序功能: 实现一元多项式的四则运算        //
/////////////////////////////////////////////////


#include <stdio.h>
#include <stdlib.h>
//定义结构体 ;
struct polynode                                       
{
	float coef ;
	int exp  ;
	struct polynode *next ;
};
typedef struct polynode pointer ;

void Output(pointer *a) ;

//指数比较函数 ;
char Compare(int x, int y)                            
{
     if(x > y)
		return ('>');
   	 if(x < y)
		return ('<');
	 else
        return ('=');
}

pointer * Attch(float c, int e, pointer *d)
{
	pointer *x ;
	x = (pointer *)malloc( sizeof(pointer) ) ;
	x->next = NULL ; 
	x->coef = c ; x->exp = e ; d->next = x ;
	return x ;
}

//一元多项式的加法运算 ;
pointer * Poly_ADD(pointer *a, pointer *b)             
{
	pointer *p , *q , *d , *c ;
	float x=0 ;
	p = a ; q = b ;
	c = (pointer *)malloc( sizeof(pointer) ) ;
	c->next = NULL ;
	d = c ;
    while((p!=NULL)&&(q!=NULL))
		switch(Compare(p->exp, q->exp))
		{
			case '=' : x = p->coef + q->coef ;
					   if(x)  d = Attch(x, p->exp, d) ;
					   p = p->next ; q = q->next ;
					   break ;
			case '>' : d = Attch(p->coef, p->exp, d) ;
					   p = p->next ;
					   break ;
			case '<' : d = Attch(q->coef, q->exp, d) ;
					   q = q->next ;
					   break ;
			default  : break ;
		}
	while(p != NULL)
	{
		d = Attch(p->coef, p->exp, d) ;
        p = p->next ;
	}
	while(q != NULL)
	{
		d = Attch(q->coef, q->exp, d) ;
        q = q->next ;
	}
	d->next = NULL ; p = c ; c = c->next ;
    printf("运算结果是:");
	
	free(p) ;  
	return c ;
}

//一元多项式的减法运算 ;
pointer * Poly_SUB(pointer *a, pointer *b)               
{
	pointer *p , *q , *d , *c ;
	float x=0 ;
	p = a ; q = b ;
	c = (pointer *)malloc( sizeof(pointer) ) ;
	c->next = NULL ;
	d = c ;
    while((p!=NULL)&&(q!=NULL))
		switch(Compare(p->exp, q->exp))
		{
			case '=' : x = p->coef - q->coef ;
					   if(x)  d = Attch(x, p->exp, d) ;
					   p = p->next ; q = q->next ;
					   break ;
			case '>' : d = Attch(p->coef, p->exp, d) ;
					   p = p->next ;
					   break ;
			case '<' : d = Attch(-q->coef, q->exp, d) ;
					   q = q->next ;
					   break ;
			default  : break ;
		}
	while(p != NULL)
	{
		d = Attch(p->coef, p->exp, d) ;
        p = p->next ;
	}
	while(q != NULL)
	{
		d = Attch(-q->coef, q->exp, d) ;
        q = q->next ;
	}
	d->next = NULL ; p = c ; c = c->next ;
    printf("运算结果是:");
	
	free(p) ;  
	return c ;
}

//一元多项式的乘法运算 ;
pointer * Poly_MUL(pointer *a, pointer *b)                     
{
	pointer *p , *q , *c , *d , *e , *g ;
	float coef=0 ;
	int exp=0 ;
	p = a ; q = b ;
	c = NULL ;
	
	while(p!=NULL)
	{
		q = b ;
		e = (pointer *)malloc( sizeof(pointer) ) ;
	    e->next = NULL ; d = e ;
		while(q!=NULL)
		{
			coef = p->coef * q->coef ;
			exp = p->exp + q->exp ;
			d = Attch(coef, exp, d) ;
			q = q->next ;
		} 
		d->next = NULL ;
		e = e->next ;	
		c = Poly_ADD(c, e) ;
		g = e ;
		free( e );  
	    p = p->next ;
	}
	printf("运算结果是:");
	return c ;
}
	
//一元多项式的除法运算 ;
pointer *Poly_DIV(pointer *p, pointer *q)                    
{
	 pointer *a , *b , *d , *f ;
	 float m ;
	 int   n ;
	 a =  (pointer *)malloc( sizeof(pointer) ) ;
	 a->next = NULL ; b = a ;
	 f = p ;
	 if(q->coef == 0)
	 {
		 printf("\a您输入的多项式B为0,无法进行除法运算。\n");
		 return NULL ;
	 }
	 else
	 {
		 do
		 {
		    d = (pointer *)malloc( sizeof(pointer) ) ;
	        d->next = NULL ;
			m = (float)(f->coef) / (q->coef) ;
			n = (f->exp) - (q->exp) ;
			b = Attch(m, n, b) ;
			b->next = NULL ;
			d = Poly_MUL(q, b) ;
			f = Poly_SUB(f, d) ;
			free(d) ;
		 }while((f->exp) >= (q->exp)) ;
		 printf("余式是:") ;
		 Output(f) ;
		 printf("商式是:") ;
	     a = a->next ;	
	     return a ;
	 }
}	 
		 
//一元多项式的输出 ;
void Output(pointer *a)                                 
{
	while(a != NULL)
	{
  		printf("(%.1f,%d) ", a->coef, a->exp);
		a = a->next ;
	}
	printf("\n\n");
}


int main()
{
	float coef=0 ; 
	int exp=0 ;
	int choice=0 ;
	pointer *poly_A , *poly_B , *oper_A , *oper_B , *link_A , *link_B , *rezult ;
    
	do{
		poly_A = (pointer *)malloc( sizeof(pointer) ) ;
		poly_A->next = NULL ;
		oper_A = link_A = poly_A ;
		poly_B = (pointer *)malloc( sizeof(pointer) ) ;
		poly_B->next = NULL ;
		oper_B = link_B = poly_B ;

		printf("\n*******************************************************************************\n");
		printf(" *执行多项式“加法”运算,请按 1 **********************************************\n");
		printf(" *执行多项式“减法”运算,请按 2 **********************************************\n");
		printf(" *执行多项式“乘法”运算,请按 3 **********************************************\n");
		printf(" *执行多项式“除法”运算,请按 4 **********************************************\n");
		printf(" *退出,请按 0 ****************************************************************\n");
		printf("*******************************************************************************\n");
		scanf("%d", &choice);
		if(choice==0)
			return 0 ;
		else if(choice<=4 && choice>=0)
		{	
			printf("请输入多项式 A <以 0 0 结尾> :\n");
			while( scanf("%f%d", &coef, &exp) != EOF )
			{ 
				if( coef==0 && exp==0 )
				{
					link_A->next = NULL ;
					break ;
				}
				else
				{
					oper_A->coef = coef ;
					oper_A->exp = exp ;
					link_A->next = oper_A ;
					link_A = link_A->next ;
					oper_A = (pointer *)malloc(sizeof(pointer)) ;
				}
			}
    
			printf("请输入多项式 B <以 0 0 结尾> :\n");
			while( scanf("%f%d", &coef, &exp) != EOF )
			{
				if( coef==0 && exp==0 )
				{
					link_B->next = NULL ;
					break ;
				}
				else
				{
					oper_B->coef = coef ;
					oper_B->exp = exp ;
					link_B->next = oper_B ;
					link_B = link_B->next ;
					oper_B = (pointer *)malloc(sizeof(pointer)) ;
				}
			}

			switch(choice)
			{
				case 1 : rezult = Poly_ADD( poly_A, poly_B );
						 break ;
				case 2 : rezult = Poly_SUB( poly_A, poly_B );
						 break ;
				case 3 : rezult = Poly_MUL( poly_A, poly_B );
			   		     break ;
				case 4 : rezult = Poly_DIV( poly_A, poly_B );
			 		     break ;
				default : break ;
			}
			Output( rezult );
			free( poly_A );
			free( poly_B );
			}
		else
			printf("对不起,您的选择超出范围。\n");
	}while(choice != 0) ;

	return 0 ;
}
	

⌨️ 快捷键说明

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