《2022年一元稀疏多项式计算器源代 .pdf》由会员分享,可在线阅读,更多相关《2022年一元稀疏多项式计算器源代 .pdf(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、#include #include typedef struct node float coef; int exp; struct node *next; Lnode, *polynmial; void create(polynmial &L); /输入并建立多项式L void display(polynmial L); /显示,输出多项式L void sort(polynmial &L); /多项式 L 按指数排序void reverse(polynmial &L); /逆置void select(); /用户选择加减操作void add(polynmial La, polynmial Lb
2、, polynmial &Lc); /多项式 La,Lb 相加void subtract(polynmial La, polynmial Lb, polynmial &Ld); /多项式 La 减去 Lb,结果给Ld void create(polynmial &L) /输入并建立多项式L int i, n; static struct node *p; scanf(%d, &n); L = (struct node *)malloc (sizeof(struct node); L-next = NULL; for(i = 0; i coef, &p-exp); p-next = L-next
3、; L-next = p; void display(polynmial L)/显示,输出多项式L struct node *p, *q; int flag = 0; int k = 0; q = L-next; while(q) if(q-coef != 0) k+; q = q-next; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 7 页 - - - - - - - - - printf(%d, , k); p = L-next; if(p-coef != 0)
4、 printf(%.1fX%d, , p-coef, p-exp); flag+; for(p = p-next; p; p = p-next) if(p-coef != 0) printf(%.1fX%d, , p-coef, p-exp); flag+; if(flag = 0) printf(%dn, flag); else printf(n); void sort(polynmial &L)/多项式 L 按指数排序 polynmial p, q, r, u; p = L-next; L-next = NULL; while(p != NULL) r = L; q = L-next; w
5、hile(q != NULL) & (q-exp exp) r = q; q = q-next; u = p-next; r-next = p; p-next = q; p = u; void reverse(polynmial &L)/逆置名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 7 页 - - - - - - - - - polynmial H; static struct node *p, *q, *s; H = (struct node*)malloc(si
6、zeof(struct node); H-next = NULL; p = (struct node*)malloc(sizeof(struct node); s = L-next; p-coef = s-coef; p-exp = s-exp; p-next = s-next; while(s) p-coef = s-coef; p-exp = s-exp; p-next = s-next; q = H-next; H-next = p; p-next = q; p = (struct node*)malloc(sizeof(struct node); s = s-next; p = H-n
7、ext; q = L-next; while(p) q-coef = p-coef; q-exp = p-exp; q = q-next; p = p-next; void select() / 用户选择加减操作 printf( 请选择加减操作n); printf(1. 两个一元多项式相加n); printf(2. 两个一元多项式相减n); void add(polynmial La, polynmial Lb, polynmial &Lc)/多项式 La,Lb 相加 struct node *pa, *pb; static struct node *pc; Lc = (struct node
8、*)malloc(sizeof(struct node); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 7 页 - - - - - - - - - pa = La-next; pb = Lb-next; Lc-next = NULL; while(pa & pb) pc = (struct node*)malloc(sizeof(struct node); if(pa-exp exp) pc-next = Lc-next; Lc-next = pc; pc-coef
9、= pa-coef; pc-exp = pa-exp; pa = pa-next; else if(pa-exp = pb-exp) pc-next = Lc-next; Lc-next = pc; pc-exp = pa-exp; pc-coef = pa-coef + pb-coef; pa = pa-next; pb = pb-next; else pc-next = Lc-next; Lc-next = pc; pc-coef = pb-coef; pc-exp = pb-exp; pb = pb-next; while(pa) pc = (struct node*)malloc(si
10、zeof(struct node); pc-next = Lc-next; Lc-next = pc; pc-coef = pa-coef; pc-exp = pa-exp; pa = pa-next; while(pb) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 7 页 - - - - - - - - - pc = (struct node*)malloc(sizeof(struct node); pc-next = Lc-next; Lc-next = pc;
11、pc-coef = pb-coef; pc-exp = pb-exp; pb = pb-next; void subtract(polynmial La, polynmial Lb, polynmial &Ld)/多项式 La 减去 Lb,结果给Ld struct node *pa, *pb; static struct node *pd; Ld = (struct node*)malloc(sizeof(struct node); pa = La-next; pb = Lb-next; Ld-next = NULL; while(pa & pb) pd = (struct node*)mal
12、loc(sizeof(struct node); if(pa-exp exp) pd-next = Ld-next; Ld-next = pd; pd-coef = pa-coef; pd-exp = pa-exp; pa = pa-next; else if(pa-exp = pb-exp) pd-next = Ld-next; Ld-next = pd; pd-exp = pa-exp; pd-coef = pa-coef - pb-coef; pa = pa-next; pb = pb-next; else pd-next = Ld-next; Ld-next = pd; pd-coef
13、 = pb-coef; pd-exp = pb-exp; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 7 页 - - - - - - - - - pb = pb-next; while(pa) pd = (struct node*)malloc(sizeof(struct node); pd-next = Ld-next; Ld-next = pd; pd-coef = pa-coef; pd-exp = pa-exp; pa = pa-next; while(pb)
14、 pd = (struct node*)malloc(sizeof(struct node); pd-next = Ld-next; Ld-next = pd; pd-coef = -pb-coef; pd-exp = pb-exp; pb = pb-next; int main() int sign; polynmial La, Lb, Lc, Ld; printf( 请输入第一个多项式:n); create(La); sort(La); printf( 请输入第二个多项式:n); create(Lb); sort(Lb); select(); scanf(%d, &sign); switc
15、h(sign) case 1: printf( 多项式之和为:n); add(La, Lb, Lc); sort(Lc); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 7 页 - - - - - - - - - reverse(Lc); display(Lc); break; default: printf( 多项式之差为:n); subtract(La, Lb, Ld); sort(Ld); reverse(Ld); display(Ld); break; int a; printf( 是否继续进行运算?1,是; 2,否 ); scanf(%d,&a); if(a=1) main(); else if (a=0) return 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 7 页 - - - - - - - - -
限制150内