十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要介绍“C语言如何实现多项式相加”,在日常操作中,相信很多人在C语言如何实现多项式相加问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C语言如何实现多项式相加”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
在察隅等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、做网站 网站设计制作按需网站制作,公司网站建设,企业网站建设,高端网站设计,营销型网站建设,成都外贸网站建设,察隅网站建设费用合理。
具体代码如下:
//多项式的相加和相乘 #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语言如何实现多项式相加”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!