C语言中怎么实现多项式相加
本篇文章为大家展示了C语言中怎么实现多项式相加,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
成都创新互联公司主要从事网站制作、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务新河,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
具体内容如下
//多项式的相加和相乘 #include#include #pragma warning(disable:4996)//兼容scanf typedef struct node { int coef; int expon; struct node* link; }Polynode,*Polynomial; Polynomial InsertPolyLinklist(Polynomial in,Polynomial Pread) { Pread->link = in; Pread = in; in->link = NULL; return Pread; } Polynomial ReadPoly(void) { Polynomial Pread = (Polynomial)malloc(sizeof(Polynode)); Pread->link = NULL; Polynomial H = Pread; int N; scanf("%d ", &N); while (N--) { Polynomial p = (Polynomial)malloc(sizeof(Polynode)); scanf("%d %d", &p->coef, &p->expon); Pread= InsertPolyLinklist(p,Pread); } Polynomial F; F = H->link; free(H); return F; } void PrintPoly(Polynomial F) { while(F != NULL) { printf("%d %d ", F->coef, F->expon); F = F->link; } printf("\n"); } Polynomial Add(Polynomial p1, Polynomial p2) { Polynomial t1=p1,t2=p2; Polynomial p=(Polynomial)malloc(sizeof(Polynode)); p->link = NULL; Polynomial q = p; Polynomial read; while (t1&&t2) { if (t1->expon == t2->expon) { if (t1->coef + t2->coef) { t1->coef = t1->coef + t2->coef; t1->expon = t1->expon; read = t1; q->link = read; q = read; t1 = t1->link; t2 = t2->link; } } else { if (t1->expon > t2->expon){ read = t1; q->link = read; q = read; t1 = t1->link; } else { if (t1->expon < t2->expon) { read = t2; q->link = read; q = read; t2 = t2->link; } } } } if (t1) { q->link = t1; } if (t2) { q->link = t2; } Polynomial F = p->link; free(p); return F; } int main(void) { Polynomial p1, p2, pp, ps; p1 = ReadPoly(); PrintPoly(p1); p2 = ReadPoly(); PrintPoly(p2); pp = Add(p1, p2); PrintPoly(pp); // ps = Mult(p1, p2); // PrintPoly(ps); return 0; }
上述内容就是C语言中怎么实现多项式相加,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
本文标题:C语言中怎么实现多项式相加
网站链接:http://cdiso.cn/article/iecgsd.html