/* 
    File name: project1.cpp
    Created by: Spencer Hughes
    Created on: 23 Sept. 2021
    Synopsis: (synopsis here)
*/ 



#include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;

// Default size of our dynamic coefficient array
const int DEFAULTPOLY = 10;    

//Default spacing for our Punnett square
const int SQUARESPACE = 10;

// Do NOT modify the class header.
class Poly
{
private:
  
    // Data members
    int arraySize;    // size of array
    int *coeff;       // dynamic array

public:

    // Default Class constructor
    // Allocate an array of DEFAULTPOLY elements and initialize it to the constant 0
    // post: Class object is initialized to degree-0 polynomial of 0  
    Poly();

  
    // Non-default (alternate) Class constructor
    // Allocate an array of 'size' elements and initializes it to the constant 0
    // post: Class object is initialized to degree-0 polynomial of 0  
    Poly(int size);

  
    // Copy constructor
    // Construct a new Poly that is a copy of an existing Poly
    // post: Class object is initialized to be a copy of the argument Poly
    Poly(const Poly& aPoly);

  
    // Destructor
    // Destroy a poly object by freeing the dynamically allocated array
    ~Poly();

  
    // Assignment operator
    // Assign 'aPoly' Poly object to 'this' Poly object
    // Note: This function is provided, please do not modify it
    const Poly& operator=(const Poly& aPoly);

    
    // grow
    // This method will allow us to increase the size of the dynamically allocated
    // array by allocating a new array of the desired size, copying the data from
    // the old array to the new array, and then releasing the old array.
    // If the newSize is less than or equal to the current size, then no actions 
    // are taken.
    // Note: the maximum degree of a polynomial is one less than the size of the 
    // array. The parameter newSize represents the size of the array.
    void grow(int newSize);

    
    // degree
    // Finds the degree of a polynomial (the highest power with a non-zero
    // coefficient)
    // pre: Class object exists
    // post: Returns the degree of the polynomial object.
    int degree() const;

    
    // setCoeff
    // Sets a term, value*x^i, in a polynomial, growing the array if necessary.
    // pre: Class object has been initialized. i is a non-negative integer.
    // post: In the polynomial, the term with power i has coefficient
    //       value. The polynomical was grown if required.
    void setCoeff(int value, int i);

    
    // getCoeff
    // Finds the coefficient of the x^i term in poly
    // pre: Class object has been initialized. i is a non-negative integer.
    // post: Returns the value of the coefficient of the term with power i
    // note: If the object does not contain a term with power i (e.g., 
    //       i>=arraySize), a coefficient value of zero is returned.
    int getCoeff(int i) const;


    // negate
    // Negate a polynomial
    // pre: The class object has been initialized.
    // post: The polynomial has been changed to represent its
    //       multiplication by -1.
    void negate();

    // punnettSquare
    // Calculates the punnett square of two polynomials
    // pre: Both polynomial objects have been initialized
    // post: Calculates the punnett square of the two polynomials and then
    //		 Invokes outputPunnet(..) function to display result into the screen
    void punnettSquare(const Poly& aPoly);

    // punnettSquare output
    // Displays the punnett square of two polynomials into the screen
    // pre: punnetSquare function generates a punnet Array then calls this function to display it.
    //		Must provide the second polinomial as well as the punnet Array
    // post: Displays the punnett square of the two polynomials into the screen
	void outputPunnett(const Poly& aPoly, int** punnetArray);
	
    // addition operator
    // Add two polynomials together and return a new polynomial that is the result
    // pre: Both class objects have been initialized
    // post: The sum of two polynomials is stored in a new polynomial which is returned.
    //       The parameter polynomials are not changed.
    friend Poly operator+(const Poly& aPoly, const Poly& bPoly);

    
    // subtraction operator
    // Subtracts one polynomial from another and return a new polynomial that is the result
    // pre: Both class objects have been initialized
    // post: The difference of two polynomials is stored in a new polynomial which is returned.
    //       The parameter polynomials are not changed.
    friend Poly operator-(const Poly& aPoly, const Poly& bPoly);

    // multiplication operator
    // Multiplies one polynomial with another and returns a new polynomial that is the result
    // pre: Both class objects have been initialized
    // post: The product of the two polynomials is stored in a new polynomial which is returned
    //       The parameter polynomials are not changed.
    friend Poly operator*(const Poly& aPoly, const Poly& bPoly);

    
    // equality operator
    // Compare two polynomials and return true if they are the same, false otherwise
    // pre: Both class objects have been initialized
    // post: A boolean value indicating whether two polynomials are the same is returned.
    //       The parameter polynomials are not changed.
    friend bool operator==(const Poly& aPoly, const Poly& bPoly);
    

    // insertion operator for output
    // Print polynomials
    // pre: The class object has been initialized
    // post: several values representing the polynomial are inserted into the output stream
    friend ostream& operator<<(ostream& out, const Poly &aPoly);

};



int main(){
    
    Poly poly1, poly2;
    int numCoeff, coeffValue, coeffDegree, x;

    // prompt user for the number of coefficients
    cout << "How many coefficients for polynomial 1?" << endl;
    cin >> numCoeff;
    for (int i=0; i<numCoeff; ++i){
        cout << "Coefficient " << i+1 << " for polynomial 1:";
        cin >> coeffValue >> coeffDegree;
        poly1.setCoeff(coeffValue, coeffDegree);
    }

    cout << endl << "How many coefficients for polynomial 2?" << endl;
    cin >> numCoeff;
    for (int i=0; i<numCoeff; ++i){
        cout << "Coefficient " << i+1 << " for polynomial 2:";
        cin >> coeffValue >> coeffDegree;
        poly2.setCoeff(coeffValue, coeffDegree);
    }

    // Sample test cases for degree() and operator<<
    cout << endl << "Polynomial 1 = " << poly1 << endl;
    cout << "Polynomial 1 has degree " << poly1.degree() << endl;
    cout << "Polynomial 2 = " << poly2 << endl;
    cout << "Polynomial 2 has degree " << poly2.degree() << endl;

    // Sample test cases for operator+, operator- and operator*
    cout << endl << "Polynomial 1 + Polynomial 2 = " << poly1 + poly2 << endl;
    cout << "Polynomial 1 - Polynomial 2 = " << poly1 - poly2 << endl << endl;
    cout << "Punnett square of Polynomial 1 and Polynomial 2: " << endl;
    poly1.punnettSquare(poly2);		//the function uses outputPunnet(..) to display result
    cout << "Polynomial 1 * Polynomial 2 = " << poly1 * poly2 << endl << endl;

    // Sample test cases for operator==
    if (poly1==poly2)
        cout << "Two polynomials are the same." << endl;
    else
        cout << "Two polynomials are different." << endl;

    // Try more test cases to test your class thoroughly


    
    return 0;
}

// Do not modify this function
const Poly& Poly::operator= (const Poly& aPoly){

    if (this == &aPoly)
        return *this;

    if (coeff)
        delete [] coeff;
    
    arraySize = aPoly.arraySize;
    coeff = new int[arraySize];
    for (int i=0; i<arraySize; ++i){
        coeff[i] = aPoly.getCoeff(i);
    }

    return *this;
}



// Do not modify this function
void Poly::outputPunnett(const Poly& aPoly, int** punnettArray){
	int squareSpace = SQUARESPACE;
    
    //Output first row
	for (int power = this->degree(); power >= 0; power--) {
        Poly aTerm(power);
        aTerm.setCoeff(this->getCoeff(power), power);
        cout << setw(squareSpace) << right << aTerm;

    }

    //Move to next row
    cout << endl;

    //Now output rest of the punnett square

    for (int power = aPoly.degree(); power >= 0; power--) {
        //First output first element of the column
        Poly aTerm(power);
        aTerm.setCoeff(aPoly.getCoeff(power), power);

        cout << right << setw(squareSpace) << aTerm;

        //Next output punnett square element
        for (int thisPower = this->degree(); thisPower >= 0; thisPower--) {
            int squarePower = thisPower + power;
            Poly squareTerm(squarePower);

            squareTerm.setCoeff(punnettArray[thisPower][power], squarePower);
            
            cout << right << setw(squareSpace) << squareTerm;

        }

        //Move to next row
        cout << endl;
    }
}

//Class function definitions

Poly::Poly() : arraySize(DEFAULTPOLY)
{
	for (int i = 1; i <= arraySize; i++)
	{
		coeff[i] = 0;
	}
}
	
Poly::Poly(int size) : arraySize(size)
{
	for (int i = 1; i <= arraySize; i++)
	{
		coeff[i] = 0;
	}
}

Poly::Poly(const Poly& aPoly)
{
	coeff = new int[arraySize];
	*coeff = *aPoly.coeff;
}

Poly::~Poly()
{
	delete [] coeff;
}

void Poly::grow(int newSize)
{
	if (newSize > arraySize) {
		int *newArray = new int[newSize];
		
		for (int i = 0; i < arraySize; ++i)
		{
			newArray[i] = coeff[i];
		}
        
        delete [] coeff;

		*coeff = *newArray;
	
		arraySize = newSize;
	}
}

int Poly::degree() const
{
	for (int i = arraySize - 1; i >= 0; --i)
	{
		if (coeff[i] > 0 || coeff[i] < 0) {
			return i;
		}
	}
}

void Poly::setCoeff(int value, int i)
{
	grow(i + 1);

	coeff[i] = value;
}

int Poly::getCoeff(int i) const
{
	if (i < arraySize && i >= 0) {
		return coeff[i];
	}
	else {
		return 0;
	}
}

void Poly::negate()
{
	for (int i = 0; i < arraySize; ++i)
	{
		coeff[i] = coeff[i] * -1;
	}
}

void Poly::punnettSquare(const Poly &aPoly)
{
	int** punnettArray;
	
	for (int i = 0; i < aPoly.arraySize; ++i)
	{
		for (int j = 0; j < arraySize; ++j)
	        {
			    punnettArray[i][j] = aPoly.getCoeff(i) * coeff[j], i+j;
		}
	}
	
	outputPunnett(aPoly, punnettArray);
}

Poly operator +(const Poly& aPoly, const Poly& bPoly)
{
    Poly *newPoly;
    
	if (aPoly.arraySize > bPoly.arraySize) {
		newPoly[aPoly.arraySize];
	}
	else {
		newPoly[bPoly.arraySize];
	}

	for (int i = 0; i < newPoly->arraySize; ++i) 
	{
		newPoly->setCoeff(aPoly.getCoeff(i) + bPoly.getCoeff(i), i);
	}
	
	return *newPoly;
}
		
Poly operator -(const Poly& aPoly, const Poly& bPoly)
{
    Poly *newPoly;
    
	if (aPoly.arraySize > bPoly.arraySize) {
		newPoly[aPoly.arraySize];
	}
	else {
		newPoly[bPoly.arraySize];
	}

	for (int i = 0; i < newPoly->arraySize; ++i) 
	{
		newPoly->setCoeff(aPoly.getCoeff(i) - bPoly.getCoeff(i), i);
	}
	
	return *newPoly;
}

Poly operator *(const Poly &aPoly, const Poly &bPoly)
{
	Poly *newPoly;
	
	for (int i = 0; i < aPoly.arraySize; ++i)
	{
		for (int j = 0; j < bPoly.arraySize; ++j)
		{
			newPoly->setCoeff(aPoly.getCoeff(i) * bPoly.getCoeff(j) + newPoly->getCoeff(i+j), i+j);
		}
	}

	return *newPoly;
}

bool operator ==(const Poly& aPoly, const Poly &bPoly)
{
	if (aPoly.degree() == bPoly.degree()) {
		for (int i = 0; i < bPoly.degree(); ++i)
		{
			if (aPoly.getCoeff(i) != bPoly.getCoeff(i)) {
				return false;
			}
		}
		
		return true;
	}

	return false;
}

ostream& operator <<(ostream& out, const Poly &aPoly)
{
	int totalCoeff(0);
	
	for (int j = 0; j < aPoly.arraySize; ++j)
	{
		totalCoeff = aPoly.getCoeff(j) + totalCoeff;
	}

	if (totalCoeff == 0) {
		out << 0;
		return out;
	}

	for (int i = aPoly.arraySize - 1; i > 0; --i)
	{	
		if (aPoly.getCoeff(i) > 0) {
			if (i = aPoly.arraySize) {
				out << aPoly.getCoeff(i);
			}
			else {
				out << '+' << aPoly.getCoeff(i) << 'x' << '^' << i;
			}
		}
		if (aPoly.getCoeff(i) < 0) {
			out << '-' << aPoly.getCoeff(i) << 'x' << '^' << i;
		}
	}

	if (aPoly.getCoeff(0) > 0) {
		out << '+' << aPoly.getCoeff(0);
	}
	else if (aPoly.getCoeff(0) < 0) {
		out << '-' << aPoly.getCoeff(0);
	}

	return out;
}









 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}