一元多项式的计算实验报告_第1页
一元多项式的计算实验报告_第2页
一元多项式的计算实验报告_第3页
一元多项式的计算实验报告_第4页
一元多项式的计算实验报告_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、计算机学院工程实践一元多项式的计算 总 报 告小组序号:编 撰 人:年 级 班 级:指导教师:提交日期:1. 项目组成员分工表 1 项目组成员分工2. 程序功能(程序实现的功能,功能结构图)实现功能:一元多项式的加、减、乘运算功能结构图:3. 程序设计简介(包括:类及其属性和方法、类之间关系、关键代码等的说明)1.class Nodepublic:Node();Node(float c, int e, Node* next);Node();float coef; /系数int exp; /指数Node* Next; /指向下一项的指针friend class Polynominal;节点类,储

2、存一元多项式每一项的信息。该内含有两个构造函数,一个析构函数及存储系数、指数和Next指针等成员变量。与Polynominal是友元关系,允许Polynominal的访问。具体成员函数如下:1)Node:Node() 默认构造函数。2)Node:Node(float c, int e, Node* next)coef = c;exp = e;Next = next;重载的自定义构造函数,用于给成员变量coef、exp和Next存入数据,Next指向传参来的next指针指向的地址,用于构造链表。2.class Polynominalpublic:Polynominal();Polynominal

3、(Polynominal &a); /拷贝构造函数void GetMSG(CString TempPloy); /获取由对话框输入的字符串并处理CString Output_Node(); /输出最后结果void PolyAdd(Polynominal &a, Polynominal &b); /加法void PolySubtract(Polynominal &a, Polynominal &b); /减法void PolyMultiply(Polynominal &a, Polynominal &b); /乘法void PolySort

4、(); /排序函数,用于乘法之后的按指数排序void OutFile(); /文本输出函数void OutFile(Polynominal &a, Polynominal &b, string ch); /重载文本输出函数void OpposeCoef(Polynominal &a, Node *temp); /系数取反,用于减法运算Node theList; /头节点 ofstream FOut; /输出流;此类用于构造一元多项式。包括加、减、乘各各功能函数,还有两个帮助运算的辅助函数,以及界面输入输出和文本输出函数。成员变量有头节点和输出流。具体成员函数如下:1)P

5、olynominal:Polynominal()默认构造函数2)Polynominal:Polynominal(Polynominal &a)Node* temp = a.theList.Next;Node* tempNode = &theList;for (; temp != NULL;)tempNode->Next = new Node(temp->coef, temp->exp, NULL);temp = temp->Next;tempNode = tempNode->Next;拷贝构造函数,用于计算时对象间的赋值,防止误改数据。3)void

6、 Polynominal:GetMSG(CString TempPoly)TempPoly.Remove(_T('');Node* TempNode = &theList;int i = 0;for (i;i<TempPoly.GetLength(); )CString temp1 ;CString temp2 ;int j = 1;if (TempPoly.GetAt(i) =_T('-')temp1 = temp1 + TempPoly.GetAt(i);i+;if (TempPoly.GetAt(i) = _T('+')i+;

7、if (TempPoly.GetAt(i) = _T('x') )temp1 = temp1 +_T('1');while(j=1)if (TempPoly.GetAt(i) != _T('x')temp1 = temp1 + TempPoly.GetAt(i);i+;else j=0;i+;if (i <(TempPoly.GetLength()-1) && TempPoly.GetAt(i) = _T('-')temp2 = temp2 + TempPoly.GetAt(i);i+;if (i = (Te

8、mpPoly.GetLength()-1) && TempPoly.GetAt(i) = _T('x')temp2 = temp2 + _T('1');elseif (i = TempPoly.GetLength() && TempPoly.GetAt(i-1) = _T('x')temp2 = temp2 + _T('1');elsewhile (i < TempPoly.GetLength() && TempPoly.GetAt(i) != _T('+')

9、&& TempPoly.GetAt(i) != _T('-')temp2 = temp2 + TempPoly.GetAt(i);i+;TempNode->Next = new Node(_ttof(temp1), _ttoi(temp2), NULL); /链表构建TempNode = TempNode->Next;此函数用于把从对话框获取的字符串进行处理,截取出多项式各项系数和指数,并转换为float型和int型,构造一元多项式链表。4)CString Polynominal:Output_Node()int first = 0;CString

10、temp;CString temp_coef;CString temp_exp;Node* TempNode = theList.Next;for (; TempNode != NULL; TempNode = TempNode->Next)if (first = 1 && TempNode->coef > 0) temp = temp + _T("+") ;temp_coef.Format(_T("%g"), TempNode->coef);temp_exp.Format(_T("%d"),T

11、empNode->exp);switch (TempNode->exp)case 0:temp = temp + temp_coef; break; /指数为0则推出case 1:if (TempNode->coef=1)/指数为1则直接输出Xtemp = temp + _T("x"); else temp = temp + temp_coef + _T("x"); break; default:if (TempNode->coef = 1) /其余指数输出“X”形式 temp = temp + _T("x")

12、+temp_exp;else temp = temp + temp_coef + _T("x")+temp_exp; break; first = 1;return temp;输出函数,将计算所得结果输出到对话框。5)void Polynominal:PolyAdd(Polynominal &a, Polynominal &b)Node *p, *q, *temp;temp = &theList;p = a.theList.Next;q = b.theList.Next;while (p != NULL && q != NULL)if

13、 (p->exp) > (q->exp)temp->Next = new Node(p->coef, p->exp, NULL);temp = temp->Next;p = p->Next;else if (p->exp) = (q->exp)temp->Next = new Node(p->coef + q->coef, p->exp, NULL);temp = temp->Next;p = p->Next;q = q->Next;else if (p->exp) < (q-&g

14、t;exp)temp->Next = new Node(q->coef, q->exp, NULL);temp = temp->Next;q = q->Next;if (q != NULL)while (q != NULL)temp->Next = new Node(q->coef, q->exp, NULL);temp = temp->Next;q = q->Next;if (p != NULL)while (p != NULL)temp->Next = new Node(p->coef, p->exp, NULL

15、);temp = temp->Next;p = p->Next;加法函数,实现多项式的相加。在其中需要判断各项指数的关系,已实现按指数大小关系构建链表。6)void Polynominal:OpposeCoef(Polynominal &a, Node *temp)temp = a.theList.Next;while (temp != NULL)temp->coef = -(temp->coef);temp = temp->Next;在执行减法操作时,用此函数将被减多项式各位系数取反,之后可直接调用加法函数进行运算;7)void Polynominal:

16、PolySubtract(Polynominal &a, Polynominal &b)Node* temp = NULL;Polynominal aa = b;OpposeCoef(aa, temp);PolyAdd(a, aa);减法函数。先调用系数取反函数,再调用加法函数,可直接实现减法功能。其中为了不改变被减多项式的各项值,在此调用拷贝构造函数创建临时对象用于计算。8)void Polynominal:PolyMultiply(Polynominal &a, Polynominal &b)Node *p, *q, *temp;temp = &th

17、eList;p = a.theList.Next;q = b.theList.Next;while (p != NULL)while (q != NULL)temp->Next = new Node(p->coef*q->coef), (p->exp + q->exp), NULL);q = q->Next;temp = temp->Next;p = p->Next;q = b.theList.Next;PolySort();乘法函数。按项相乘,运算完之后,再调用排序函数,实现同指数项系数相加,并按照指数大小输出。9)void Polynomin

18、al:PolySort()Polynominal temp;int i = 1;float temp_coef = 0;int temp_exp = 0;Node* tempNode = theList.Next;Node* tempNode2 = &theList;Node* tempNode3 = &temp.theList;while (i = 1)tempNode = theList.Next;tempNode2 = &theList;for (; tempNode != NULL;)if (tempNode->exp > temp_exp)temp

19、_exp = tempNode->exp;tempNode = tempNode->Next;tempNode = theList.Next;if (tempNode != NULL)for (; tempNode != NULL;)if (tempNode->exp = temp_exp)temp_coef = temp_coef + tempNode->coef;tempNode2->Next = tempNode->Next;tempNode = tempNode->Next;elsetempNode = tempNode->Next;te

20、mpNode2 = tempNode2->Next;tempNode3->Next = new Node(temp_coef, temp_exp, NULL);tempNode3 = tempNode3->Next;temp_coef = 0;temp_exp = 0;elsei = 0;Node* temp2 = &theList;Node* temp3 = temp.theList.Next;for (; temp3 != NULL;)temp2->Next = new Node(temp3->coef, temp3->exp, NULL);te

21、mp2 = temp2->Next;temp3 = temp3->Next;实现相乘之后的排序,是最后的链表按照指数大小排列,并且每个指数只有一项。10)void Polynominal:OutFile()FOut.open("PolynominalList.txt", ofstream:app);int first = 1;Node* temp = theList.Next;FOut << " ( "for (; temp != NULL; temp = temp->Next)if (!first&&tem

22、p->coef > 0)FOut << "+"FOut << temp->coef;switch (temp->exp)case 0:break; /指数为0则推出case 1:FOut << "x" break; /指数为1则直接输出Xdefault:FOut << "x" << temp->exp; break; /其余指数输出“X”形式first = 0;FOut << " ) "FOut.close();文

23、本输出函数,用于两个多项式的文本输出。11)void Polynominal:OutFile(Polynominal &a, Polynominal &b, string ch)FOut.open("PolynominalList.txt", ofstream:app);int first = 1;Node* temp = theList.Next;a.OutFile();FOut << ch;b.OutFile();FOut << " = ( "for (; temp != NULL; temp = temp-&

24、gt;Next)if (!first&&temp->coef > 0)FOut << "+"FOut << temp->coef;switch (temp->exp)case 0:break; /指数为0则推出case 1:FOut << "x" break; /指数为1则直接输出Xdefault:FOut << "x" << temp->exp; break; /其余指数输出“X”形式first = 0;FOut <<

25、; " ) "FOut << "nn"FOut.close();重载文本输出函数,用于最后结果的一次性文本输出。3.1. 类设计表 2 类表序号类名属性方法1Nodefloat coef; int exp; Node* Next; Node();Node(float c, int e, Node* next);2PolynominalNode theList; ofstream FOut; Polynominal();Polynominal(Polynominal &a); void GetMSG(CString TempPloy);

26、 CString Output_Node(); void PolyAdd(Polynominal &a, Polynominal &b); void PolySubtract(Polynominal &a, Polynominal &b); void PolyMultiply(Polynominal &a, Polynominal &b); void PolySort(); void OutFile(); void OutFile(Polynominal &a, Polynominal &b, string ch); void O

27、pposeCoef(Polynominal &a, Node *temp); 3.2. 类关系图3.3. 数据结构设计数据成员:1.Class Node:float coef; /系数int exp; /指数Node* Next; /指向下一项的指针2.class PolynominalNode theList; /头节点 ofstream FOut; /输出流此类中theList是头节点,其成员变量Next指向新建节点,构造成链表。3.4. 关键代码与运行界面关键代码:#ifndef POLYNOMINA_H#define POLYNOMINA_H#include "Nod

28、e.h"#include <string>#include <afxstr.h>#include <fstream>#include <iostream>using namespace std;class Polynominalpublic:Polynominal();Polynominal(Polynominal &a); /拷贝构造函数void GetMSG(CString TempPloy); /获取由对话框输入的字符串并处理CString Output_Node(); /输出最后结果void PolyAdd(Polyno

29、minal &a, Polynominal &b); /加法void PolySubtract(Polynominal &a, Polynominal &b); /减法void PolyMultiply(Polynominal &a, Polynominal &b); /乘法void PolySort(); /排序函数,用于乘法之后的按指数排序void OutFile(); /文本输出函数void OutFile(Polynominal &a, Polynominal &b, string ch); /重载文本输出函数void O

30、pposeCoef(Polynominal &a, Node *temp); /系数取反,用于减法运算Node theList; /头节点 ofstream FOut; /输出流;#endif函数实现:#include "stdafx.h"#include "Polynominal.h"#include "conio.h"Polynominal:Polynominal()Polynominal:Polynominal(Polynominal &a)Node* temp = a.theList.Next;Node* te

31、mpNode = &theList;for (; temp != NULL;)tempNode->Next = new Node(temp->coef, temp->exp, NULL);temp = temp->Next;tempNode = tempNode->Next;void Polynominal:GetMSG(CString TempPoly)TempPoly.Remove(_T('');Node* TempNode = &theList;int i = 0;for (i;i<TempPoly.GetLength(

32、); )CString temp1 ;CString temp2 ;int j = 1;if (TempPoly.GetAt(i) =_T('-')temp1 = temp1 + TempPoly.GetAt(i);i+;if (TempPoly.GetAt(i) = _T('+')i+;if (TempPoly.GetAt(i) = _T('x') )temp1 = temp1 +_T('1');while(j=1)if (TempPoly.GetAt(i) != _T('x')temp1 = temp1 + T

33、empPoly.GetAt(i);i+;else j=0;i+;if (i <(TempPoly.GetLength()-1) && TempPoly.GetAt(i) = _T('-')temp2 = temp2 + TempPoly.GetAt(i);i+;if (i = (TempPoly.GetLength()-1) && TempPoly.GetAt(i) = _T('x')temp2 = temp2 + _T('1');elseif (i = TempPoly.GetLength() &&

34、amp; TempPoly.GetAt(i-1) = _T('x')temp2 = temp2 + _T('1');elsewhile (i < TempPoly.GetLength() && TempPoly.GetAt(i) != _T('+') && TempPoly.GetAt(i) != _T('-')temp2 = temp2 + TempPoly.GetAt(i);i+;TempNode->Next = new Node(_ttof(temp1), _ttoi(temp2)

35、, NULL);TempNode = TempNode->Next;CString Polynominal:Output_Node()int first = 0;CString temp;CString temp_coef;CString temp_exp;Node* TempNode = theList.Next;for (; TempNode != NULL; TempNode = TempNode->Next)if (first = 1 && TempNode->coef > 0) temp = temp + _T("+") ;

36、temp_coef.Format(_T("%g"), TempNode->coef);temp_exp.Format(_T("%d"),TempNode->exp);switch (TempNode->exp)case 0:temp = temp + temp_coef; break; /指数为0则推出case 1:if (TempNode->coef=1)/指数为1则直接输出Xtemp = temp + _T("x"); else temp = temp + temp_coef + _T("x&qu

37、ot;); break; default:if (TempNode->coef = 1) /其余指数输出“X”形式 temp = temp + _T("x")+temp_exp;else temp = temp + temp_coef + _T("x")+temp_exp; break; first = 1;return temp;void Polynominal:PolyAdd(Polynominal &a, Polynominal &b)Node *p, *q, *temp;temp = &theList;p = a.t

38、heList.Next;q = b.theList.Next;while (p != NULL && q != NULL)if (p->exp) > (q->exp)temp->Next = new Node(p->coef, p->exp, NULL);temp = temp->Next;p = p->Next;else if (p->exp) = (q->exp)temp->Next = new Node(p->coef + q->coef, p->exp, NULL);temp = tem

39、p->Next;p = p->Next;q = q->Next;else if (p->exp) < (q->exp)temp->Next = new Node(q->coef, q->exp, NULL);temp = temp->Next;q = q->Next;if (q != NULL)while (q != NULL)temp->Next = new Node(q->coef, q->exp, NULL);temp = temp->Next;q = q->Next;if (p != NUL

40、L)while (p != NULL)temp->Next = new Node(p->coef, p->exp, NULL);temp = temp->Next;p = p->Next;void Polynominal:OpposeCoef(Polynominal &a, Node *temp)temp = a.theList.Next;while (temp != NULL)temp->coef = -(temp->coef);temp = temp->Next;void Polynominal:PolySubtract(Polyno

41、minal &a, Polynominal &b)Node* temp = NULL;Polynominal aa = b;OpposeCoef(aa, temp);PolyAdd(a, aa);void Polynominal:PolyMultiply(Polynominal &a, Polynominal &b)Node *p, *q, *temp;temp = &theList;p = a.theList.Next;q = b.theList.Next;while (p != NULL)while (q != NULL)temp->Next

42、= new Node(p->coef*q->coef), (p->exp + q->exp), NULL);q = q->Next;temp = temp->Next;p = p->Next;q = b.theList.Next;PolySort();void Polynominal:PolySort()Polynominal temp;int i = 1;float temp_coef = 0;int temp_exp = 0;Node* tempNode = theList.Next;Node* tempNode2 = &theList;N

43、ode* tempNode3 = &temp.theList;while (i = 1)tempNode = theList.Next;tempNode2 = &theList;for (; tempNode != NULL;)if (tempNode->exp > temp_exp)temp_exp = tempNode->exp;tempNode = tempNode->Next;tempNode = theList.Next;if (tempNode != NULL)for (; tempNode != NULL;)if (tempNode->

44、;exp = temp_exp)temp_coef = temp_coef + tempNode->coef;tempNode2->Next = tempNode->Next;tempNode = tempNode->Next;elsetempNode = tempNode->Next;tempNode2 = tempNode2->Next;tempNode3->Next = new Node(temp_coef, temp_exp, NULL);tempNode3 = tempNode3->Next;temp_coef = 0;temp_exp = 0;elsei = 0;Node* temp2 = &theList;Node* temp3 = temp.theList.Next;for (; temp3 != NULL;)temp2->Next = new Node(t

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论