1、对于两个一元多项式中所有指数不相同的项,则分别复抄到“和”多项式中去。实验步骤:1、2、3、六、调试过程及实验结果七、总结多项式系数小的八、附录#pragma once#include struct node int exp;/指数 double coef;/系数 node *next;/指针域;class polyprivate: node * head;public: poly(); void in1poly(); void in2poly(int,int,double); void delpoly(); void prtpoly(); poly operator +(poly &); /
2、*poly(void);*/#include StdAfx.hpoly.hiostreamusing namespace std;poly:poly() head = NULL; return ;void poly:in1poly() node *p,*k; int e; double c; k=NULL; coutce; while(e=0) p=new node; p-exp=e;coef=c;next=NULL; if(k=NULL) head=p; else k-next = p; k=p; in2poly (int n,int e,double c) int k; node *p;
3、for(k=n-1;k=0;k-)coef=ck;p-exp=ek;next = head; head = p; return;delpoly() node *p,*q; q=head; while(q!=NULL) p=q-next; delete q; q=p;prtpoly() node *k; if(head=NULL) coutcoef,exppoly poly:operator+(poly &p2) poly p; node *k,*q,*m,*n; m=head; n=p2.head ; while (m!=NULL)&(n!=NULL) if(m-exp=n-exp) c=m-
4、coef +n-coef; e=m-exp; m=m- n=n- else if(m-expn- c=n- e=n- c=m- if(c! q=new node; q- if(k=NULL) p.head=q; else k-next=q; k=q; while(m! e=m- m=m- q- p.head=q;next =q; k=q; while(n! c=n- e=n- else return (p);/poly:poly(void)/ delete /stdafx.hcmathint _tmain(int argc, _TCHAR* argv) poly p1,p2,add_p; in
5、t pe14=0,1,8,17; double pc13=7,3,9; int pe23=1,7,8; double pc23=8,22,-9; p1.in2poly(3,pe1,pc1); p2.in2poly(3,pe2,pc2); add_p=p1+p2;欢迎使用多项式相加程序! 多项式1为: p1.prtpoly();多项式2为: p2.prtpoly();两多项式相加和为: add_p.prtpoly(); return 0;邢台学院数学与信息技术学院二栈及其应用1、熟悉栈的原理和实现方式;2、理解使用栈消除函数递归调用的原理;3、掌握后缀表达式计算的方法。1、栈实现阶乘函数;2、栈
6、实现后缀表达式的计算。函数调用栈的结构如下:第n层调用(当前函数局部变量空间)第n-1层调用(当前函数的主调函数变量空间)第1层调用主函数局部变量空间函数调用的栈空间使用栈消去递归的算法框架如下:初始化栈;将完整问题参数压栈;while(栈非空)取出栈顶元素所描述的问题如果可以直接解决,则直接解决否则分解为子问题压栈后缀表达式是运算符在运算数之后的一种表达式存储的数据结构,不需要比较运算符优先级别,只需要每次读到运算数时压栈,读到运算符时将运算数出栈,将结果压栈即可。最后的运算结果存放在栈底。实验步骤#define SElemType intclass stack SElemType *bas
7、e; SElemType *top; stack() base = new SElemType100; top = base; void Push(SElemType n) *top = n; top+; SElemType Pop() SElemType e ; e = *(-top); return e;/实现阶乘运算int JC(int n) stack s; int sum = 1; /将?数簓据Y循-环压1入?栈?中D while(n) s.Push(n); n-; while(s.top != s.base) int b = s.Pop(); sum *= b; return su
8、m;/用栈实现后缀表达式int Count(char b) stack s1; int sum = 0; int i=0; while(bi != 0) if(bi!=+bi!*-/ s1.Push(bi-0 if(bi= int a = s1.Pop(); int b = s1.Pop(); sum = a+b; else if(bi=) sum = a*b; sum = b-a; sum = b/a; s1.Push(sum); i+;int main(int argc, char* argv) int m = 6; int a = JC(m); printf(%d的阶乘计算结果为a:阰,m);%dn,a);nn后缀表达式计算: char c10 = 5, int b = Count(c);%d,b);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1