您的位置:

多项式相乘c语言,c语言相乘函数

本文目录一览:

C语言,多项式相乘

#include stdio.h

#include stdlib.h

typedef struct node {

int coefficient, power;

struct node* next;

}term;

term* new_term(int coefficient, int power) {

term* t = (term*)malloc(sizeof(term));

t-next = NULL;

t-coefficient = coefficient;

t-power = power;

return t;

}

void free_term(term* t) {

free(t);

}

typedef struct list {

term head;

}polynomial;

void init_polynomial(polynomial* p) {

p-head.next = NULL;

}

void clear_polynomial(polynomial* p) {

term* t = p-head.next;

term* del;

while (t != NULL) {

del = t;

t = t-next;

free_term(del);

}

p-head.next = NULL;

}

void insert_polynomial(polynomial* p, term* t) {

t-next = p-head.next;

p-head.next = t;

}

void sort(polynomial* p) {

term* t;

term* next;

int finish = 0, temp;

while (!finish) {

finish = 1;

t = p-head.next;

while (t != NULL) {

next = t-next;

if (next != NULL) {

if (t-power  next-power) {

temp = t-coefficient;

t-coefficient = next-coefficient;

next-coefficient = temp;

temp = t-power;

t-power = next-power;

next-power = temp;

finish = 0;

}

}

t = next;

}

}

}

void combine(polynomial* p) {

term* t = p-head.next;

term* next;

while (t != NULL) {

next = t-next;

if (next != NULL  next-power == t-power) {

t-coefficient += next-coefficient;

t-next = next-next;

free_term(next);

}

else {

t = next;

}

}

}

void multiply(polynomial* p1, polynomial* p2, polynomial* p3) {

term* t1 = p1-head.next;

term* t2;

clear_polynomial(p3);

init_polynomial(p3);

while (t1 != NULL) {

t2 = p2-head.next;

while (t2 != NULL) {

insert_polynomial(p3, new_term(t1-coefficient*t2-coefficient, t1-power + t2-power));

t2 = t2-next;

}

t1 = t1-next;

}

sort(p3);

combine(p3);

}

void input(polynomial* p) {

int coef, power;

char c;

init_polynomial(p);

while (true) {

scanf("%d%d", coef, power);

insert_polynomial(p, new_term(coef, power));

c = getchar();

if (c == '\n') break;

}

sort(p);

combine(p);

}

void output(polynomial* p) {

term* t = p-head.next;

while (t != NULL) {

printf("%d %d ", t-coefficient, t-power);

t = t-next;

}

}

int main() {

int i;

polynomial p[3];

for (i = 0; i  3; i++) {

init_polynomial(p[i]);

}

for (i = 0; i  2; i++) {

input(p[i]);

}

multiply(p[0], p[1], p[2]);

output(p[2]);

}

C语言单链表多项式的乘法

Linklist Multiply_poly(Linklist A,Linklist B) {// 多项式相乘

    PolyNode *p,*q,*C,*t;

    C = t = (PolyNode *)malloc(sizeof(PolyNode)); // 头结点

    C-exp = 0;

    C-coef = 0;

    for(p = A-next; p; p = p-next) {

        for(q = B-next; q; q = q-next) {

            t-next = (PolyNode *)malloc(sizeof(PolyNode));

            t-next-coef = p-coef * q-coef;

            t-next- exp = p-exp + q-exp;

            t = t-next;

        }

    }

    t-next = NULL;

    // 调用合并同类项函数

    // 调用排序函数

    return C;

}

如何用C语言实现多项式的加法和乘法

按题目要求应该是(1+X)*(1+X)=X2+1吧

可以用单链表表示多项的指数,比如1+X可以表示为0,1

X2+1可以表示为2,0,Xn+X(n-1)+...+1即n,n-1,.....0

所有的指数建议按大小排序,可以在单链表插入时进行。

加法,可以新建一个链表C做为结果,把链表A的内容复制到C,然后把另一个链表B中的每一项插入C,如果要插入的项已存在,则不插入并且删除这个结点。

乘法,新建一个链表C作为结果,要用2层循环遍历A和B中的每一项,对AB中的每个项都要算个加法,把和插入C中,如果这个和已存在,则不插入并且删除这个结点。

C语言求多项式乘法

/*

* 文件名: 1_3.c(选做题)

* 实验环境: Turbo C 2.0

* 完成时间: 2003年2月22日

*--------------------------------------------------------------------

* 改进说明: 可以实现多个多项式的加法、减法、乘法,并且比书中算法更加

* 合理. 例如: 连加a+b+c+d,连减a-b-c-d,连乘a*b*c*d.

*/

#include stdio.h

#include conio.h

#include stdlib.h

#include string.h

#define TRUE 1

#define FALSE 0

#define POSITIVE 1

#define NEGATIVE -1

typedef int status;

typedef struct NodeType

{

float fCoeff;

int iExpon;

struct NodeType *next;

} NodeType, *LinkType;

typedef LinkType polynomial;

typedef polynomial *PolyPointer;

status MakePolyBuff(PolyPointer *, const int);

status MakeNode(polynomial *, const float, const int);

void AppNodeToList(polynomial *, polynomial); /* 在链表尾追加结点 */

status CreatePolyn(PolyPointer, int);

status ProcStrError(const char[]); /* 检查输入的数据 */

void SortPolyn(PolyPointer, int); /* 根据iExpon域对链表进行升序排序 */

void DestroyBuff(PolyPointer, const int);

void DestroyPolyn(polynomial);

int PolynLength(const polynomial); /* 求链表的长度 */

void AddProcess(PolyPointer, const int, PolyPointer, const int);

void SubstractProcess(PolyPointer, const int, PolyPointer);

void MultiplyProcess(PolyPointer, const int, PolyPointer);

void PrintPolyn(const polynomial);

void MergePolynCoeff(PolyPointer, int); /* 在有序链表中,合并同类项 */

int main(void)

{

int iCounter,

iPolyNum; /* 多项式链表缓冲区中链表的个数 */

PolyPointer PolyBuff = NULL; /* 用户输入的多项式链表缓冲区 */

polynomial PolyAddRes = NULL, /* 存放连加结果链表 */

PolySubRes = NULL, /* 存放连减结果链表 */

PolyMulRes = NULL; /* 存放连乘结果链表 */

char strNum[10];

do

{

printf("请输入需要构造多项式的个数,至少2个: ");

gets(strNum);

iPolyNum = atoi(strNum);

} while (iPolyNum 2);

MakePolyBuff(PolyBuff, iPolyNum);

CreatePolyn(PolyBuff, iPolyNum);

SortPolyn(PolyBuff, iPolyNum);

MergePolynCoeff(PolyBuff, iPolyNum);

printf("\n打印用户输入并整合后的多项式:\n");

for (iCounter = 0; iCounter iPolyNum; iCounter++)

{

printf("第%d个项式:\n", iCounter + 1);

PrintPolyn(*(PolyBuff + iCounter));

}

AddProcess(PolyBuff, iPolyNum, PolyAddRes, POSITIVE);

printf("\n----------------连加结果-----------------\n");

PrintPolyn(PolyAddRes);

SubstractProcess(PolyBuff, iPolyNum, PolySubRes);

printf("\n----------------连减结果-----------------\n");

PrintPolyn(PolySubRes);

MultiplyProcess(PolyBuff, iPolyNum, PolyMulRes);

printf("\n----------------连乘结果-----------------\n");

PrintPolyn(PolyMulRes);

printf("\n运行完毕!\n");

/* 回收资源 */

DestroyBuff(PolyBuff, iPolyNum);

DestroyPolyn(PolyAddRes);

DestroyPolyn(PolySubRes);

DestroyPolyn(PolyMulRes);

getch();

return 0;

}

status MakePolyBuff(PolyPointer *polyBuffHead, const int iPolyNum)

{

int iCounter;

*polyBuffHead = (PolyPointer)

malloc(sizeof(polynomial) * iPolyNum);

if (!(*polyBuffHead))

{

printf("错误,内存溢出!\n");

return FALSE;

}

for (iCounter = 0; iCounter iPolyNum; iCounter++)

*(*polyBuffHead + iCounter) = NULL;

return TRUE;

}

status CreatePolyn(PolyPointer PolyBuff, int iPolyNum)

{

int iCounter, iExpon;

float fCoeff;

char strNum[100], strTemp[64], *cpCurr, *cpCurrNum;

polynomial pNewNode = NULL, pInsPos = NULL;

printf("\n请输入构造多项式的系数和指数...\n");

printf("输入一个多项式的方式为: 系数, 指数; ... ; 系数, 指数;\n例如: 3, 4; 5, 6; 7, 8;\n");

for (iCounter = 0; iCounter iPolyNum; iCounter++)

{

printf("\n请输入第%d个多项式:\n", iCounter + 1);

gets(strNum);

if(!ProcStrError(strNum)) return FALSE;

cpCurr = cpCurrNum = strNum;

while (*cpCurr != '\0')

{

if (*cpCurr == ',')

{

strncpy(strTemp, cpCurrNum, cpCurr - cpCurrNum);

strTemp[cpCurr - cpCurrNum] = '\0';

fCoeff = (float)atof(strTemp);

cpCurrNum = cpCurr + 1;

}

else if (*cpCurr == ';')

{

strncpy(strTemp, cpCurrNum, cpCurr - cpCurrNum);

strTemp[cpCurr - cpCurrNum] = '\0';

iExpon = atoi(strTemp);

MakeNode(pNewNode, fCoeff, iExpon);

AppNodeToList(PolyBuff + iCounter, pNewNode);

cpCurrNum = cpCurr + 1;

}

cpCurr++;

}

}

return TRUE;

}

status MakeNode(LinkType *pp, const float coeff, const int expon)

{

if (!(*pp = (LinkType)malloc(sizeof(NodeType) * 1)))

{

printf("Error, the memory is overflow!\n");

return FALSE;

}

(*pp)-fCoeff = coeff;

(*pp)-iExpon = expon;

(*pp)-next = NULL;

return TRUE;

}

void AppNodeToList(polynomial *pHead, polynomial pNewNode)

{

static polynomial pCurrNode;

if (!(*pHead))

(*pHead) = pCurrNode = pNewNode;

else

{

pCurrNode-next = pNewNode;

pCurrNode = pCurrNode-next;

}

}

void SortPolyn(PolyPointer PolyBuff, int iPolyNum)

{

int iCounter;

polynomial pTemp, pTempCurrNode, /* 临时链表 */

pPrevMinExp, pCurrMinExp,/* 指向最小iExpon结点的指针 */

pCurrNode, pPrevNode;

for (iCounter = 0; iCounter iPolyNum; iCounter++)

{

pTemp = NULL;

while (*(PolyBuff + iCounter) != NULL)

{

pPrevNode = pPrevMinExp = pCurrMinExp =

*(PolyBuff + iCounter);

pCurrNode = (*(PolyBuff + iCounter))-next;

while (pCurrNode != NULL)

{

if (pCurrMinExp-iExpon pCurrNode-iExpon)

{

pPrevMinExp = pPrevNode;

pCurrMinExp = pCurrNode;

}

pPrevNode = pCurrNode;

pCurrNode = pCurrNode-next;

}

/* 将系数最小的结点从原链表中取出 */

if (pCurrMinExp == *(PolyBuff + iCounter))

*(PolyBuff + iCounter) = pPrevMinExp-next;

else

pPrevMinExp-next = pCurrMinExp-next;

/* 将系数最小的结点插入升序链表 */

pCurrMinExp-next = NULL;

if (!pTemp)

pTemp = pTempCurrNode = pCurrMinExp;

else

{

pTempCurrNode-next = pCurrMinExp;

pTempCurrNode = pTempCurrNode-next;

}

}

*(PolyBuff + iCounter) = pTemp;

}

}

void MergePolynCoeff(PolyPointer PolyBuff, int iPolyNum)

{

int iCounter;

float MergeCoeffRes = 0;

polynomial TempList, ResList = NULL, pCurrNode, pPreNode,

pNewNode = NULL;

for (iCounter = 0; iCounter iPolyNum; iCounter++)

{

pPreNode = TempList= *(PolyBuff + iCounter);

MergeCoeffRes = pPreNode-fCoeff;

pCurrNode = (*(PolyBuff + iCounter))-next;

while (pCurrNode != NULL)

{

while ((pCurrNode != NULL)

(pCurrNode-iExpon == pPreNode-iExpon))

{

MergeCoeffRes += pCurrNode-fCoeff;

pPreNode = pCurrNode;

pCurrNode = pCurrNode-next;

}

/* 在ResList中加入新结点 */

if (MergeCoeffRes != 0)

{

MakeNode(pNewNode, MergeCoeffRes, pPreNode-iExpon);

AppNodeToList(ResList, pNewNode);

MergeCoeffRes = 0;

}

pPreNode = pCurrNode;

}

DestroyPolyn(TempList);

*(PolyBuff + iCounter) = ResList;

ResList = NULL;

}

}

void AddProcess(PolyPointer polyBuff, const int iPolyNum,

PolyPointer pResult, const int iSign)

{

int iCounter;

float fCoeffRes;

polynomial pNewNode, pCurrNode_1, pCurrNode_2,

pDelList = NULL, /* 下次要删除的中间结果链表 */

pResList = NULL; /* 中间结果链表 */

pCurrNode_1 = *(polyBuff);

for (iCounter = 1; iCounter iPolyNum; iCounter++)

{

pCurrNode_2 = *(polyBuff + iCounter);

while (pCurrNode_1 != NULL pCurrNode_2 != NULL)

{

if (pCurrNode_1-iExpon == pCurrNode_2-iExpon)

{

fCoeffRes = 0;

fCoeffRes = pCurrNode_1-fCoeff +

iSign * pCurrNode_2-fCoeff;

if (fCoeffRes != 0)

{

MakeNode(pNewNode, fCoeffRes,

pCurrNode_1-iExpon);

AppNodeToList(pResList, pNewNode);

}

pCurrNode_1 = pCurrNode_1-next;

pCurrNode_2 = pCurrNode_2-next;

}

else if (pCurrNode_1-iExpon pCurrNode_2-iExpon)

{

MakeNode(pNewNode, pCurrNode_1-fCoeff,

pCurrNode_1-iExpon);

AppNodeToList(pResList, pNewNode);

pCurrNode_1 = pCurrNode_1-next;

}

else /* 当pCurrNode_1-iExpon pCurrNode_2-iExpon时候 */

{

MakeNode(pNewNode, iSign * pCurrNode_2-fCoeff,

pCurrNode_2-iExpon);

AppNodeToList(pResList, pNewNode);

pCurrNode_2 = pCurrNode_2-next;

}

}

/* 加入余下的多项式 */

while (pCurrNode_1 != NULL)

{

MakeNode(pNewNode, pCurrNode_1-fCoeff,

pCurrNode_1-iExpon);

AppNodeToList(pResList, pNewNode);

pCurrNode_1 = pCurrNode_1-next;

}

while (pCurrNode_2 != NULL)

{

MakeNode(pNewNode, iSign * pCurrNode_2-fCoeff,

pCurrNode_2-iExpon);

AppNodeToList(pResList, pNewNode);

pCurrNode_2 = pCurrNode_2-next;

}

if (pDelList != NULL) DestroyPolyn(pDelList);

pCurrNode_1 = pResList;

pDelList = pResList;

pResList = NULL;

}

*pResult = pCurrNode_1;

}

void SubstractProcess(PolyPointer polyBuff, const int iPolyNum,

PolyPointer pResult)

{

AddProcess(polyBuff, iPolyNum, pResult , NEGATIVE);

}

void MultiplyProcess(PolyPointer polyBuff, const int iPolyNum,

PolyPointer pResult)

{

int iCounter = 1, jCounter = 0, iLength; /* 缓冲区的长度 */

PolyPointer pTemPBuff = NULL; /* 存放中间结果的缓冲区 */

polynomial pCurrNode_1, pCurrNode_2, pNewNode = NULL;

/* 初始化 */

pCurrNode_1 = polyBuff[0];

iLength = PolynLength(polyBuff[0]);

MakePolyBuff(pTempBuff, iLength);

while (TRUE)

{

while (pCurrNode_1 != NULL)

{

pCurrNode_2 = polyBuff[iCounter];

while (pCurrNode_2 != NULL)

{

MakeNode(pNewNode,

pCurrNode_1-fCoeff * pCurrNode_2-fCoeff,

pCurrNode_1-iExpon + pCurrNode_2-iExpon);

AppNodeToList(pTempBuff[jCounter], pNewNode);

pCurrNode_2 = pCurrNode_2-next;

}

jCounter++;

pCurrNode_1 = pCurrNode_1-next;

}

/* 回收旧的中间结果 */

if (pResult != NULL) DestroyPolyn(*pResult);

/* 获得新的中间结果 */

AddProcess(pTempBuff, iLength, pResult , POSITIVE);

DestroyBuff(pTempBuff, iLength); /* 回收存中间结果的缓冲区 */

jCounter = 0;

if (++iCounter = iPolyNum)

break;

else

{

iLength = PolynLength(*pResult);

MakePolyBuff(pTempBuff, iLength);

pCurrNode_1 = *pResult;

}

}

}

void PrintPolyn(const polynomial polyList)

{

polynomial pCurrNode = polyList;

printf("多项式的长度为: %d\n", PolynLength(polyList));

while (pCurrNode != NULL)

{

printf("%.2fX^%d", pCurrNode-fCoeff, pCurrNode-iExpon);

if (pCurrNode-next != NULL)

if (pCurrNode-next-fCoeff 0 )

printf("+");

pCurrNode = pCurrNode-next;

}

printf("\n");

}

int PolynLength(const polynomial polyList)

{

int iLength = 0;

polynomial pCurrNode = polyList;

while (pCurrNode != NULL)

{

pCurrNode = pCurrNode-next;

iLength++;

}

return iLength;

}

void DestroyBuff(PolyPointer polyBuff, const int iPolyNum)

{

int iCounter;

for (iCounter = 0; iCounter iPolyNum; iCounter++)

DestroyPolyn(polyBuff[iCounter]);

free(polyBuff);

}

void DestroyPolyn(polynomial polyList)

{

polynomial pCurrNode;

while (polyList != NULL)

{

pCurrNode = polyList;

polyList = polyList-next;

free(pCurrNode);

}

}

status ProcStrError(const char str[])

{

const char *cpCurr = str;

if (!strlen(str))

{

printf("你没有输入数据!\n");

return FALSE;

}

while (*cpCurr != '\0')

{

if (!(*cpCurr == ' ' || *cpCurr == ',' || *cpCurr == ';'

|| *cpCurr == '-')

('0' *cpCurr || *cpCurr '9')

|| (*(cpCurr + 1) == '\0' *cpCurr != ';'))

{

printf("输入数据出错,请注意正确的输入方式!\n");

return FALSE;

}

cpCurr++;

}

return TRUE

c语言写俩个多项式相乘

这个是母函数的知识,这一块我没怎么看,楼主可以自己百度一下。大概的意思就是: a[x]:x表示指数,a[x]存系数。如 3x^2+4x+5:可表示为:a[2]=3,a[1]=4,a[0]=5. 多项式加减就是a[x]相加减。多项式相乘就是x相加。也就是下标的运算