实验2 一元符号多项式

第1关:一元符号多项式的建立

任务描述:用带头结点的单链表存储一元符号多项式各项的系数和指数,定义一元符号多项式类型Polynomial,实现从键盘(测试集)依次读入项数与各项的系数和指数而建立一个一元符号多项式,实现对建立的一元符号多项式进行输出,具体格式见下面的测试说明。

#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int Status;

typedef struct {
	float coef;
	int expn;
}ElemType;

typedef struct LNode {
	ElemType data;
	struct LNode* next;
}LNode, * LinkList;

typedef LinkList Polynomial;

Status InitPolyn(Polynomial* P);	// 初始化一元符号多项式P,创建只有头结点的多项式P(表示零多项式)
void ClearPolyn(Polynomial P);		// 清空一元符号多项式P
Status CreatPolyn(Polynomial P);	// 从键盘(测试集)读入数据建立一元符号多项式,P 已初始化,若P 非空,则先清空
									// 在建立过程中,若申请结点失败,则清空一元符号多项式P 且返回ERROR
									// 需要自行保证数据的合法性(按升幂有序且指数非负)
void DestroyPolyn(Polynomial* P);	// 销毁一元符号多项式P
void PrintPolyn(Polynomial P);		// 输出一元符号多项式P


int main() {
	Polynomial pa = NULL;

	InitPolyn(&pa);
	if (!CreatPolyn(pa)) { DestroyPolyn(&pa); exit(OVERFLOW); }
	printf("一元符号多项式: ");
	PrintPolyn(pa);
	printf("\n");

	DestroyPolyn(&pa);

	return 0;
}

// 给出上述5个函数的定义,注意不要修改主函数!

给出5个函数的定义

// 初始化一元符号多项式P,创建只有头结点的多项式P(表示零多项式)
Status InitPolyn(Polynomial* P) 
{
    *P = (LNode*)malloc(sizeof(LNode));
	if (*P == NULL) 
	{
        return OVERFLOW;
    }
    (*P)->next = NULL;
    return OK;
}

// 清空一元符号多项式P
void ClearPolyn(Polynomial P) 
{
    LNode* current = P->next;
	while (current) 
	{
        LNode* temp = current;
        current = current->next;
        free(temp);
    }
    P->next = NULL;
}

// 从键盘(测试集)读入数据建立一元符号多项式
Status CreatPolyn(Polynomial P) 
{
    int n;
	if (scanf("%d", &n) != 1) 
	{
        return ERROR; // 在建立过程中,若申请结点失败,则清空一元符号多项式P 且返回ERROR
    }

    ClearPolyn(P);

    for (int i = 0; i < n; i++) 
    {
        float coef;
        int expn;
        if (scanf("%f %d", &coef, &expn) != 2) 
		{
            return ERROR;
        }

        LNode* prev = P;
        LNode* current = P->next;

        // 按指数递增顺序插入
        while (current && current->data.expn < expn) 
		{
            prev = current;
            current = current->next;
        }

        LNode* newNode = (LNode*)malloc(sizeof(LNode));
        if (newNode == NULL) 
		{
            ClearPolyn(P);
            return ERROR;
        }
        newNode->data.coef = coef;
        newNode->data.expn = expn;
        newNode->next = current;
        prev->next = newNode;
    }
    return OK;
}

// 销毁一元符号多项式P
void DestroyPolyn(Polynomial* P) 
{
    ClearPolyn(*P);
    free(*P);
    *P = NULL;
}

// 输出一元符号多项式P
void PrintPolyn(Polynomial P) 
{
    LNode* current = P->next;
	if (!current) 
	{
        printf("0");
        return;
    }

    int firstTerm = 1;
	while (current) 
	{
        if (!firstTerm && current->data.coef > 0) 
		{
            printf("+");
        }
        printf("%.1fx^%d", current->data.coef, current->data.expn);
        firstTerm = 0;
        current = current->next;
    }
}

第2关:一元符号多项式的加、减法运算

任务描述:在第1关定义的Polynomial 类型的基础上,添加实现加、减法运算的函数。

#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int Status;

typedef struct 
{
    float coef;
    int expn;
} ElemType;

typedef struct LNode 
{
    ElemType data;
    struct LNode* next;
} LNode, * LinkList;

typedef LinkList Polynomial;

// 初始化一元符号多项式P,创建只有头结点的多项式P(表示零多项式)
Status InitPolyn(Polynomial* P) 
{
    *P = (LNode*)malloc(sizeof(LNode));
	if (*P == NULL) 
	{
        return OVERFLOW;
    }
    (*P)->next = NULL;
    return OK;
}

// 清空一元符号多项式P
void ClearPolyn(Polynomial P) 
{
    LNode* current = P->next;
	while (current) 
	{
        LNode* temp = current;
        current = current->next;
        free(temp);
    }
    P->next = NULL;
}

// 从键盘(测试集)读入数据建立一元符号多项式
Status CreatPolyn(Polynomial P) 
{
    int n;
	if (scanf("%d", &n) != 1) 
	{
        return ERROR;
    }

    ClearPolyn(P);

for (int i = 0; i < n; i++) 
{
        float coef;
        int expn;
        if (scanf("%f %d", &coef, &expn) != 2) 
		{
            return ERROR;
        }

        LNode* prev = P;
        LNode* current = P->next;

        // 按指数递增顺序插入
        while (current && current->data.expn < expn) 
		{
            prev = current;
            current = current->next;
        }

        LNode* newNode = (LNode*)malloc(sizeof(LNode));
        if (newNode == NULL) 
		{
            ClearPolyn(P);
            return ERROR;
        }
        newNode->data.coef = coef;
        newNode->data.expn = expn;
        newNode->next = current;
        prev->next = newNode;
    }
    return OK;
}

// 销毁一元符号多项式P
void DestroyPolyn(Polynomial* P) 
{
    ClearPolyn(*P);
    free(*P);
    *P = NULL;
}

// 输出一元符号多项式P
void PrintPolyn(Polynomial P) 
{
    LNode* current = P->next;
	if (!current) 
	{
        printf("0");
        return;
    }

    int firstTerm = 1;
	while (current) 
	{
        if (!firstTerm && current->data.coef > 0) 
		{
            printf("+");
        }
        printf("%.1fx^%d", current->data.coef, current->data.expn);
        firstTerm = 0;
        current = current->next;
    }
}

// 加法 P = P + Q
void AddPolyn(Polynomial P, Polynomial Q) 
{
    LNode* pa = P;
    LNode* qa = Q->next;
	while (qa) 
	{
        LNode* temp = qa;
        qa = qa->next;

        LNode* current = P->next;
        LNode* prev = P;

        while (current && current->data.expn < temp->data.expn) 
		{
            prev = current;
            current = current->next;
        }

        if (current && current->data.expn == temp->data.expn) 
		{
            current->data.coef += temp->data.coef;
            if (current->data.coef == 0) 
			{
                prev->next = current->next;
                free(current);
            }
        } 
		else 
		{
            temp->next = current;
            prev->next = temp;
        }
    }
    Q->next = NULL;
}

// 减法 P = P - Q
void SubtractPolyn(Polynomial P, Polynomial Q) 
{
    LNode* qa = Q->next;
	while (qa) 
	{
        qa->data.coef = -qa->data.coef;
        qa = qa->next;
    }
    AddPolyn(P, Q);
}

int main() {
    Polynomial pa, pb;

    // Initialize polynomials
    if (InitPolyn(&pa) != OK || InitPolyn(&pb) != OK) {
        fprintf(stderr, "Error initializing polynomials.\n");
        return EXIT_FAILURE;
    }

    // Create and input polynomials
    printf("Enter polynomial pa:\n");
    if (CreatPolyn(pa) != OK) {
        fprintf(stderr, "Error creating polynomial pa.\n");
        DestroyPolyn(&pa);
        DestroyPolyn(&pb);
        return EXIT_FAILURE;
    }
    printf("Enter polynomial pb:\n");
    if (CreatPolyn(pb) != OK) {
        fprintf(stderr, "Error creating polynomial pb.\n");
        DestroyPolyn(&pa);
        DestroyPolyn(&pb);
        return EXIT_FAILURE;
    }

    // Print initial polynomials
    printf("一元符号多项式pa: ");
    PrintPolyn(pa);
    printf("\n");
    printf("一元符号多项式pb: ");
    PrintPolyn(pb);
    printf("\n");

    // Add pb to pa and print result
    printf("after AddPolyn(pa, pb) ...\n");
    AddPolyn(pa, pb);
    printf("一元符号多项式pa: ");
    PrintPolyn(pa);
    printf("\n");
    printf("一元符号多项式pb: ");
    PrintPolyn(pb);
    printf("\n");

    // Reset polynomials for subtraction test
    // Recreate polynomials because pb was modified during addition
    DestroyPolyn(&pa);
    DestroyPolyn(&pb);
    if (InitPolyn(&pa) != OK || InitPolyn(&pb) != OK) {
        fprintf(stderr, "Error initializing polynomials.\n");
        return EXIT_FAILURE;
    }
    printf("Enter polynomial pa again:\n");
    CreatPolyn(pa);
    printf("Enter polynomial pb again:\n");
    CreatPolyn(pb);

    // Print initial polynomials again
    printf("一元符号多项式pa: ");
    PrintPolyn(pa);
    printf("\n");
    printf("一元符号多项式pb: ");
    PrintPolyn(pb);
    printf("\n");

    // Subtract pb from pa and print result
    printf("after SubtractPolyn(pa, pb) ...\n");
    SubtractPolyn(pa, pb);
    printf("一元符号多项式pa: ");
    PrintPolyn(pa);
    printf("\n");
    printf("一元符号多项式pb: ");
    PrintPolyn(pb);
    printf("\n");

    // Clean up
    DestroyPolyn(&pa);
    DestroyPolyn(&pb);

    return EXIT_SUCCESS;
}

第3关:一元符号多项式的乘法运算

任务描述:在第2关定义的Polynomial 类型的基础上,添加实现乘法运算的函数。

#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int Status;

typedef struct 
{
    float coef;
    int expn;
} ElemType;

typedef struct LNode 
{
    ElemType data;
    struct LNode* next;
} LNode, *LinkList;

typedef LinkList Polynomial;

// 初始化一元符号多项式P,创建只有头结点的多项式P(表示零多项式)
Status InitPolyn(Polynomial* P) 
{
    *P = (LNode*)malloc(sizeof(LNode));
    if (*P == NULL) 
    {
        return OVERFLOW;
    }
    (*P)->next = NULL;
    return OK;
}

// 清空一元符号多项式P
void ClearPolyn(Polynomial P) 
{
    LNode* current = P->next;
    while (current) 
    {
        LNode* temp = current;
        current = current->next;
        free(temp);
    }
    P->next = NULL;
}

// 从键盘(测试集)读入数据建立一元符号多项式
Status CreatPolyn(Polynomial P) 
{
    int n;
    if (scanf("%d", &n) != 1) 
    {
        return ERROR;
    }

    ClearPolyn(P);

    for (int i = 0; i < n; i++) 
    {
        float coef;
        int expn;
        if (scanf("%f %d", &coef, &expn) != 2) 
        {
            return ERROR;
        }

        LNode* prev = P;
        LNode* current = P->next;

        // 按指数递增顺序插入
        while (current && current->data.expn < expn) 
        {
            prev = current;
            current = current->next;
        }

        LNode* newNode = (LNode*)malloc(sizeof(LNode));
        if (newNode == NULL) 
        {
            ClearPolyn(P);
            return ERROR;
        }
        newNode->data.coef = coef;
        newNode->data.expn = expn;
        newNode->next = current;
        prev->next = newNode;
    }
    return OK;
}

// 销毁一元符号多项式P
void DestroyPolyn(Polynomial* P) 
{
    ClearPolyn(*P);
    free(*P);
    *P = NULL;
}

// 输出一元符号多项式P
void PrintPolyn(Polynomial P) 
{
    LNode* current = P->next;
    if (!current) 
    {
        printf("0");
        return;
    }

    int firstTerm = 1;
    while (current) 
    {
        if (!firstTerm && current->data.coef > 0) 
        {
            printf("+");
        }
        printf("%.1fx^%d", current->data.coef, current->data.expn);
        firstTerm = 0;
        current = current->next;
    }
}

// 添加或更新项到结果多项式
Status AddTerm(Polynomial R, float coef, int expn) 
{
    LNode* prev = R;
    LNode* current = R->next;

    while (current && current->data.expn < expn) 
    {
        prev = current;
        current = current->next;
    }

    if (current && current->data.expn == expn) 
    {
        current->data.coef += coef;
        if (current->data.coef == 0) 
        {
            prev->next = current->next;
            free(current);
        }
    } else 
    {
        LNode* newNode = (LNode*)malloc(sizeof(LNode));
        if (newNode == NULL) 
        {
            return ERROR;
        }
        newNode->data.coef = coef;
        newNode->data.expn = expn;
        newNode->next = current;
        prev->next = newNode;
    }
    return OK;
}

// 乘法 P = P * Q
Status MultiplyPoly(Polynomial P, Polynomial Q) 
{
    Polynomial R;
    if (InitPolyn(&R) != OK) 
    {
        return ERROR;
    }

    for (LNode* pa = P->next; pa != NULL; pa = pa->next) 
    {
        for (LNode* qa = Q->next; qa != NULL; qa = qa->next) 
        {
            if (AddTerm(R, pa->data.coef * qa->data.coef, pa->data.expn + qa->data.expn) != OK) 
            {
                ClearPolyn(R);
                return ERROR;
            }
        }
    }

    ClearPolyn(P);
    P->next = R->next;
    free(R);

    return OK;
}

int main() {
    Polynomial pa = NULL, pb = NULL;

    InitPolyn(&pa);
    InitPolyn(&pb);

    if (!CreatPolyn(pa)) {
        DestroyPolyn(&pa);
        exit(EXIT_FAILURE);
    }
    if (!CreatPolyn(pb)) {
        DestroyPolyn(&pa);
        DestroyPolyn(&pb);
        exit(EXIT_FAILURE);
    }

    printf("一元符号多项式pa: ");
    PrintPolyn(pa);
    printf("\n");
    printf("一元符号多项式pb: ");
    PrintPolyn(pb);
    printf("\n");

    printf("after MultiplyPolyn(pa, pb) ...\n");
    if (!MultiplyPoly(pa, pb)) {
        printf("乘法操作失败!\n");
    } else {
        printf("一元符号多项式pa: ");
        PrintPolyn(pa);
        printf("\n");
    }

    printf("一元符号多项式pb: ");
    PrintPolyn(pb);
    printf("\n");

    DestroyPolyn(&pa);
    DestroyPolyn(&pb);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值