您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了如何用C语言代码实现多项式相加的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何用C语言代码实现多项式相加文章都会有所收获,下面我们一起来看看吧。
具体代码如下
//多项式的相加和相乘 #include<stdio.h> #include<stdlib.h> #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语言代码实现多项式相加”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。