예제 #1
0
//Read a polynomial from an inputstream
void Helper::readpoly(std::ifstream& ifs, Polynomial& poly)
{
  char c;
  while(ifs>>c)
  {
    int iCoefficient = 0, iExponent = 0;
    ifs>>iCoefficient>>c>>iExponent;
    ifs>>c;
        
    if(iExponent == 0 && iCoefficient == 0)
      return;
    else
      poly.insert(iCoefficient, iExponent);
  }

}
	/* Multiplication: multiplies two polynomials 
	   For each term in second polynomial, copies
	   the first polynomial and multiplies each of 
	   its terms by the second's term and inserts
	   the results into the output polynomial.
	*/
	Polynomial operator*(const Polynomial p){
		 
		Polynomial output;
		Term* currentA = head;

		/* for each term in A */
		while (currentA->next)
		{
			/* for each term in B, multiply against
			   A's term and insert into output*/
			Term* currentB = p.head;
			while (currentB->next)
			{
				output.insert(*currentA->next * *currentB->next);
				currentB = currentB->next;
			}
			
			currentA = currentA->next;
		}

		return output;
	}
예제 #3
0
파일: main.cpp 프로젝트: haydenx83/School
int main()
{
    ifstream myFile("dataPoly.txt");
	string currC;
	string currE;
	string currL;
	double currCoeff;
	int currExpon;
	int x = 0,y,z,pNum;
	string numPoly;

   	Polynomial* polyArray[12];

	for(int i = 0; i < 12; i++)
	{
		Polynomial* polyList = new Polynomial;
		polyArray[i] = polyList;
	}
///////////////////////////////////////////////////////////////////////////////////////////
	if (myFile.is_open())
	{
		myFile >> numPoly;
		pNum = atoi(numPoly.c_str());

		for(int i = 0; i < pNum;i++)
		{
			Polynomial* polyList = polyArray[i];

			myFile >> currC;
			myFile >> currE;
			currCoeff = atof(currC.c_str());
			currExpon = atoi(currE.c_str());
			polyList->insert(currCoeff,currExpon,polyArray[10]);

			while(currExpon != 0)
			{
				myFile >> currC;
				myFile >> currE;
				currCoeff = atof(currC.c_str());
				currExpon = atoi(currE.c_str());
				polyList->insert(currCoeff,currExpon,polyArray[10]);
			}

			polyArray[x] = polyList;
			x++;
		}
/////////////////////////////////////////////////////////////////////////////////////////////////////
		while(currL.compare("Q") !=  0 && myFile >> currL)
		{
			if (currL.compare("R") == 0)
			{
				myFile >> currL;
				int x = atoi(currL.c_str()) - 1;
				polyArray[10]->remove(polyArray[x]);
				Polynomial* polyList = polyArray[x];
				myFile >> currC;
				myFile >> currE;
				currCoeff = atof(currC.c_str());
				currExpon = atoi(currE.c_str());
				polyList->insert(currCoeff,currExpon,polyArray[10]);

				while(currExpon != 0)
				{
					myFile >> currC;
					myFile >> currE;
					currCoeff = atof(currC.c_str());
					currExpon = atoi(currE.c_str());
					polyList->insert(currCoeff,currExpon,polyArray[10]);
				}
			}
			else if(currL.compare("A") == 0)