C语言实现一个多项式操作

本文档介绍如何使用C语言来实现对多项式的操作。涵盖了多项式的基本运算,如加法、减法、乘法等。


#ifndef QUANTIC_H
#define QUANTIC_H

 


#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "malloc.h"

typedef struct Quantic
{
    double factor;//系数
    int order; //阶数
    struct Quantic *next;
}Quantic;

Quantic *CreateList(int num)  //创建多项式
{
    Quantic *head1,*head2,*rear1,*rear2,*p;
    double a[3]={1,2,3};
    int b[3]={0,1,2};
    if(num==1)
    {
        head1=NULL;
        for(int i=0;i<3;i++)
        {
            p=(Quantic *)malloc(sizeof(Quantic));
            p->factor=a[i];p->order=b[i];
            if(head1==NULL) head1=p;
            else rear1->next=p;
            rear1=p;
        }
        rear1->next=NULL;
        return head1;
    }
    else if(num==2)
    {
        head2=NULL;
        for(int i=0;i<3;i++)
        {
            p=(Quantic *)malloc(sizeof(Quantic));
            p->factor=a[i]+1;p->order=b[i]+1;
            if(head2==NULL) head2=p;
            else rear2->next=p;
            rear2=p;
        }
        rear2->next=NULL;
        return head2;
    }
}

void PrintList(Quantic *head)  //打印多项式
{
    Quantic *p=head;
    printf("\nf(x)=");
    while(p!=NULL)
    {
        if(p->factor!=0)
        {
        printf("(%.3f)*x(%d)+",p->factor,p->order);        
        }
        p=p->next;
    }
    printf("\n");
}

Quantic *SumList(Quantic *head1,Quantic *head2,int mark) //对两个多项式求和
{
    Quantic *p1=head1,*p2=head2;
    Quantic *p,*head,*rear;
    head=NULL;
    while(p1 && p2)  //head1和head2都不为空
    {
        p=(Quantic *)malloc(sizeof(Quantic));
        if(p1->order > p2->order)
        {            
            p=p2;
            if(m
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值