Untitled
unknown
plain_text
2 months ago
5.1 kB
3
Indexable
/** * * Solution to course project # 4 * Introduction to programming course * Faculty of Mathematics and Informatics of Sofia University * Winter semester 2024/2025 * * @author Niya Neykova * @idnumber 5MI0600488 * @compiler GCC * * <file with helper functions › * */ #include <iostream> #include <vector> using namespace std; //getting polynomial's coefficients vector<double> getCoefficient() { int degree; cout << "Enter the degree of P(x): " << endl; cin >> degree; vector<double> coefficients(degree + 1); for (int i = degree; i >= 0; i--) { cout << "Enter the coefficient before x^" << i << ": "; cin >> coefficients[i]; } return coefficients; } //adding two polynomials together vector<double> addPolynomials(vector<double> &polynomial1, vector<double> &polynomial2) { int newDegree, degree1, degree2; degree1 = polynomial1.size() - 1; degree2 = polynomial2.size() - 1; if (degree1 >= degree2) newDegree = degree1; else newDegree = degree2; vector<double> newPolynomial(newDegree + 1); for (int i = 0; i <= newDegree; i++) { double polynomial1Coeff = 0; double polynomial2Coeff = 0; if (i < polynomial1.size()) polynomial1Coeff = polynomial1[i]; else polynomial1Coeff = 0; if (i < polynomial2.size()) polynomial2Coeff = polynomial2[i]; else polynomial2Coeff = 0; newPolynomial[i] = polynomial1Coeff + polynomial2Coeff; } return newPolynomial; } //subtracting two polynomials vector<double> subtractPolynomials(vector<double> &polynomial1, vector<double> &polynomial2) { int newDegree, degree1, degree2; degree1 = polynomial1.size() - 1; degree2 = polynomial2.size() - 1; if (degree1 >= degree2) newDegree = degree1; else newDegree = degree2; vector<double> newPolynomial(newDegree + 1); for (int i = 0; i <= newDegree; i++) { double polynomial1Coeff = 0; double polynomial2Coeff = 0; if (i < polynomial1.size()) polynomial1Coeff = polynomial1[i]; else polynomial1Coeff = 0; if (i < polynomial2.size()) polynomial2Coeff = polynomial2[i]; else polynomial2Coeff = 0; newPolynomial[i] = polynomial1Coeff - polynomial2Coeff; } return newPolynomial; } //printing a polynomial void printPolynomial(vector<double> &coefficients) { for (int i = coefficients.size() - 1; i >= 0; i--) { if (coefficients[i] != 0) { cout << coefficients[i]; if (i > 0) { cout << "x^" << i; } if (i > 0 && coefficients[i - 1] > 0) { cout << " + "; } } } cout << endl; } int main() { int numOfFunc; cout << "Welcome to Polynomial Calculator - "; cout << "a mini project intended to work with polynomials with rational coefficients." << endl; while (true) { cout << "Choose one of the following functionalities:" << endl; cout << "1) Add polynomials" << endl; cout << "2) Subtract polynomials" << endl; cout << "3) Multiply polynomials" << endl; cout << "4) Divide polynomials" << endl; cout << "5) Multiply polynomial by scalar" << endl; cout << "6) Find value of polynomial at a given number" << endl; cout << "7) Find GCD of two polynomials" << endl; cout << "8) Display Vieta's formulas for a given polynomial" << endl; cout << "9) Represent a polynomial in powers of (x+a)" << endl; cout << "10) Factor polynomial and find its rational roots" << endl; cout << "11) Quit program" << endl; cout << "Enter your option: " << endl; cin >> numOfFunc; if (numOfFunc == 11) { cout << "Thank you for using me! Goodbye!" << endl; break; } switch (numOfFunc) { case 1: { cout << "You chose the first functionality!" << endl; cout << "Enter Polynomial P(x)" << endl; vector<double> polynomial1 = getCoefficient(); cout << "Enter Polynomial Q(x)" << endl; vector<double> polynomial2 = getCoefficient(); vector<double> sum = addPolynomials(polynomial1, polynomial2); cout << "The sum of the polynomials is:" << endl; printPolynomial(sum); } break; case 2: { cout << "You chose the second functionality!" << endl; cout << "Enter Polynomial P(x)" << endl; vector<double> polynomial1 = getCoefficient(); cout << "Enter Polynomial Q(x)" << endl; vector<double> polynomial2 = getCoefficient(); vector<double> subtract = subtractPolynomials(polynomial1, polynomial2); cout << "The sum of the polynomials is:" << endl; printPolynomial(subtract); } break; default: cout << "Invalid input. Try again!" << endl; } } return 0; }
Editor is loading...
Leave a Comment