纯C语言单链表实现求解一元多项式求值的算法(书上代码)

/*Polynomial.h*/
#ifndef POLYNOMIAL_H_INCLUDED
#define POLYNOMIAL_H_INCLUDED
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdbool.h>

typedef struct node
{			//多项式结点的定义
    double coef;					//系数
    int exp;						//指数
    struct node *link;			//链接指针
} Term, *Polynomial;			//一元多项式的定义



#endif // POLYNOMIAL_H_INCLUDED

/*test.c*/
#include "Polynomial.h"
#define maxSize 30

void Input ( Polynomial PL, double C[ ], int E[ ], int n )
{
//从系数数组C[n]和指数数组E[n]输入一元多项式的各项,建立一个按降幂方式排列
//的一元多项式PL。要求调用此函数前PL已存在且已置空
    Polynomial newTerm, p, pre;
	int i;
	for ( i = 0; i < n; i++ )
    {						//输入各项的系数和指数
		p = PL->link;
		pre = PL;				//按降幂寻找新项插入位置
		while ( p != NULL && p->exp > E[i] )
		{
		    pre = p;
            p = p->link;
        }
		if ( p != NULL && p->exp == E[i] ) 		//已有指数相等的项,不插入
 			printf ( "已有与指数%d相等的项,输入作废\n", E[i] );
		else
        {
			newTerm = ( Term* ) malloc ( sizeof ( Term ) );	//创建新结点
			newTerm->coef = C[i];
            newTerm->exp = E[i];
			newTerm->link = p;
			pre->link = newTerm;  	//链入并保持项指数降序
		}
	}
}

void Output ( Polynomial PL )
{
//输出带头结点的一元多项式链表PL。
    Polynomial p = PL->link;
    printf ( "The polynomal is:\n" );
    bool h = 1;									//最初不输出‘+’号的标识
    while ( p != NULL )
    {
        if ( h == 1 )
        {
            if ( p->coef < 0 )
                printf ( "-" );	 	//第一项系数小于零输出"-"
            h = 0;
        }
        else
        {									//非第一项输出系数符号
            if ( p->coef > 0 )
                printf ( "+" );
            else
                printf ( "-" );
        }
        if ( p->exp == 0 || fabs(p->coef) != 1 )
            printf ( "%g", fabs(p->coef) );		//输出项的系数
        switch ( p->exp )
        {						//输出项的指数
            case 0: break;							//常数项不输出指数
            case 1: printf ( "X" );  break;		//一次项仅输出“X”
            default: printf ( "X^%d", p->exp ); 		//高次项输出“X^指数”
        }
        p = p->link;								//下一项
    }
    printf ( "\n" );
}

void reverse ( Polynomial A )
{
//将按照幂指数降序链接的一元多项式A就地逆置,转换为按幂指数升序链接。
    Polynomial p, q;
    p = A->link;  A->link = NULL;				//新链表初始化
    while ( p != NULL )
    {
        q = p;  p = p->link;						//在原链表中摘下*q
        q->link = A->link;  A->link = q;			//将*q插入新链表表头
    }
}

double calcValue_1 ( Polynomial A, double x )
{
//求多项式的值。pow(x, y)是求x的y次幂的函数, 其原型在“math.h”中
    Polynomial p = A->link;  double value = 0.0;
    while ( p != NULL )
    {
        value = value + pow(x, p->exp) * p->coef;
        p = p->link;
    }
    return value;
}

double calcValue ( Polynomial PL, double x )
{
//求降幂排列的一元稀疏多项式的值。pow(x, y)是求x的y次幂的函数, 其原型在"math.h"中
    Polynomial p = PL->link;
    double value = p->coef;
    int e = p->exp;  		//存放系数am和指数em的值
    p = p->link;								//p指向下一项 (am-1, em-1)
    while ( p != NULL )
    {
        value = value*pow ( x, e-p->exp )+p->coef;	//计算
        e = p->exp;
        p = p->link;				//e保存ei-1的值
    }
    return value*pow ( x, e );
}

int main()
{
	double C[maxSize] = {3, -2, 1, 4, -6, -1 };
	int E[maxSize] = {6, 5, 3, 2, 1, 0 };
	int n = 6;
	Polynomial Poly;  double x;
	Poly = ( Term* ) malloc ( sizeof ( Term ) );
	Poly->link = NULL;
	Input ( Poly, C, E, n );
	Output ( Poly );

	printf ( "求值\n" );
	x = 10;
	printf ( "x=%g时求值结果为%g\n", x, calcValue ( Poly, x ) );
	printf ( "x=%g时求值结果为%g\n", x, calcValue_1 ( Poly, x ) );

	reverse ( Poly );
	Output ( Poly );
}

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页