⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 polymain.cpp

📁 多项式与常数和多项式之间的加减乘除等运算
💻 CPP
字号:
/////////////////////////////////////////////////////
//	polymain.cpp  - testing classes
/////////////////////////////////////////////////////
#include <iostream>
#include "poly.h"
#include "quad.h"
#include "complex.h"
#include <string>

using namespace std;

int main() {

/////////////////////////////////////////////////////
//	enter double for operations with polynomials
/////////////////////////////////////////////////////
	double d;
	cout<<"Demonstrating and Testing"<<endl<<"Enter number(double) : ";
	cin>>d;
/////////////////////////////////////////////////////
//	enter size and array for first poly
/////////////////////////////////////////////////////
//  following alternative format can be used for poly's
//	or omit the int for quadratic
//
//	double set1[] = {2,3,4,8};
//	poly p4(int, set1);
/////////////////////////////////////////////////////
	int i;
	int in;
	double incoeff;
	double *cArray;
	cout<<"Enter Polynomial p1 details "<<endl;
	cout<<"How many coefficients ? ";
	cin>>in;
	cArray = new double[in];
	for (i=0;i<in;i++)
	{
		cout<<"Input Coefficient of x^"<<in-1-i<<": ";
		cin>>incoeff;
		cArray[i]=incoeff;
	}
/////////////////////////////////////////////////////
	poly p1(in, cArray);	//set up poly with input 
							//size and coefficient array
	delete cArray;
///////////////////////////////////////////////////////////
//	another method to enter coefficients - second poly
//	using setCoeff(int, double)
///////////////////////////////////////////////////////////
	cout<<"Enter Polynomial p2 details "<<endl;
	cout<<"How many coefficients ? ";
	cin>>in;
	poly p2(in);//set up second poly using size only
	for (i=0;i<in;i++)
	{
		cout<<"Input Coefficient of x^"<<in-1-i<<": ";
		cin>>incoeff;
		p2.setCoef((in-1-i),incoeff);//demonstrates access(write)	
									//assign in reverse order from
									//input size (in), minus one to zero
	}
/////////////////////////////////////////////////////
//	demonstrates polynomial printing
/////////////////////////////////////////////////////
	cout<<"Poly p1: ";	//poly p1	
	p1.print();			//print
	cout<<endl;
	cout<<"Poly p2: ";	//poly p2
	p2.print();			//print 
	cout<<endl;

//////////////////////////////////////////////////////
//	demonstrates read access using p[]
//////////////////////////////////////////////////////
	cout <<"access p1[0]";	
	cout<<": answer: ";
	double c = p1[0];			// will be used later
	cout<<c<<endl;				//to restore contents

//////////////////////////////////////////////////////
//	demonstrates read access using getCoef(int)
//////////////////////////////////////////////////////
	cout <<"access p1.getCoeff(0)";	
	cout<<": answer: ";
	cout<<p1.getCoef(0)<<endl;

//////////////////////////////////////////////////////
//	demonstrates write access using setCoef(int)
//////////////////////////////////////////////////////
	cout <<"access p1.setCoeff(0, 2)";//temporarily set coef	
	p1.setCoef(0,2);
	cout<<": answer: ";
	cout<<p1.getCoef(0);
	cout<<" (restoring) "<<endl;
	p1.setCoef(0, c);		//restore original contents 
							
//////////////////////////////////////////////////////
//	demonstrates identity function
//////////////////////////////////////////////////////
	cout <<"identity function +p1";	
	cout<<": answer: ";
	+p1;
	p1.print();
	cout<<endl;

//////////////////////////////////////////////////////
//	demonstrates inverse function
//////////////////////////////////////////////////////
	cout <<"inverse function -p1";	
	cout<<": answer: ";
	-p1;			//also works as p1 = -p1
	p1.print();
	cout<<" (restoring) "<<endl;
	-p1;	//restore
	
////////////////////////////////////////////////////
//	demonstrates addition of a polynomial
////////////////////////////////////////////////////
	poly p3 = p1 + p2;	//create poly p3 and do add
	cout <<"p1 + p2";	
	cout<<": answer: ";
	p3.print();
	cout<<endl;
////////////////////////////////////////////////////
//	demonstrates subtraction of a polynomial	
////////////////////////////////////////////////////
	p3 = p1 - p2;
	cout <<"p1 - p2";	
	cout<<": answer: ";
	p3.print();
	cout<<endl;

////////////////////////////////////////////////////
//	demonstrates addition of a double
////////////////////////////////////////////////////
	p3 = p1 + d;	//create poly p3 and do add
	cout <<"p1 + d";	
	cout<<": answer: ";
	p3.print();
	cout<<endl;
////////////////////////////////////////////////////
//	demonstrates subtraction of a double
////////////////////////////////////////////////////
	p3 = p1 - d;
	cout <<"p1 - d";	
	cout<<": answer: ";
	p3.print();
	cout<<endl;

////////////////////////////////////////////////////
//	demonstrates division by a double
////////////////////////////////////////////////////
	p3 = p1 / d;
	cout <<"p1 / d";	
	cout<<": answer: ";
	p3.print();
	cout<<endl;


////////////////////////////////////////////////////
//demonstrate multiplication by a poly 
////////////////////////////////////////////////////
	p3 = p1 * p2;
	cout <<"p1 * p2";	
	cout<<": answer: ";
	p3.print();
	cout<<endl;

////////////////////////////////////////////////////
//demonstrate division by a poly 
////////////////////////////////////////////////////
	p3 = p1 / p2;
	cout <<"p1 / p2";	
	cout<<": answer: ";
	p3.print();
	cout<<endl;

////////////////////////////////////////////////////
//demonstrate normalise poly 
////////////////////////////////////////////////////
	p1.normalise();
	cout <<"p1.normalise()";	
	cout<<": answer: ";
	p1.print();
	cout<<endl;

////////////////////////////////////////////////////
//demonstrate differenciate or derive poly 
////////////////////////////////////////////////////
	p1.differenciate();
	cout <<"p1.differenciate()";	
	cout<<": answer: ";
	p1.print();
	cout<<endl;
	

////////////////////////////////////////////////////
//demonstrate integrate poly 
////////////////////////////////////////////////////
	p1.integrate();
	cout <<"p1.integrate()";	
	cout<<": answer: ";
	p1.print();
	cout<<endl;

/////////////////////////////////////////////////////
//	demonstrating chaining
/////////////////////////////////////////////////////
	p3 = p2 + p2 + p2;
	cout <<"p3 = p2 + p2 + p2";	
	cout<<": answer: ";
	p3.print();
	cout<<endl;

/////////////////////////////////////////////////////
//	demonstrating symmetry on built-in types
/////////////////////////////////////////////////////
	p3 = d + p2;
	cout <<"p3 = d (double) + p2";	
	cout<<": answer: ";
	p3.print();
	cout<<endl;


/////////////////////////////////////////////////////
//	demonstrate quadratic
/////////////////////////////////////////////////////

/////////////////////////////////////////////////////
//	code to enter array for quadratic
/////////////////////////////////////////////////////
	cout<<"Quadratic coefficiants: "<<endl;
	in=3;					//only used for input loop 
	quad q2;				//create quadratic 
	for (i=0;i<3;i++)
	{
		cout<<"Input Coefficient of x^"<<in-1-i<<": ";
		cin>>incoeff;
		q2.setCoef((in-1-i),incoeff);	//assign in reverse order 
	}
/////////////////////////////////////////////////////
	root rObject;			//create root object
	rObject = q2.getRoots();//calculate roots and 
							//encapsulate in root object
/////////////////////////////////////////////////////
//	returns number of roots
/////////////////////////////////////////////////////
	cout<<rObject.getNumRoots()<<" ";//number of roots
/////////////////////////////////////////////////////
//	returns roots type message
/////////////////////////////////////////////////////
	cout<<rObject.getMsg()<<" at"<<endl;//root message string
/////////////////////////////////////////////////////
//	prints roots
/////////////////////////////////////////////////////
	rObject.print();	//uses roots.print to display	
/////////////////////////////////////////////////////
//	returns actual root
/////////////////////////////////////////////////////
	complex cpx;
	cpx = rObject.getRoot(0);
	cout<<"Repeating first root ";
	cpx.print();	//can also use complex print
	cout<<endl;
	system("PAUSE");
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -