Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //USE POINTER NOTATION
- #include <iostream>
- #include <cctype>
- #include <cstring>
- #include <cstdlib>
- //#include <string>
- using namespace std;
- class Polynomial
- {
- private:
- int *coefficient;
- int counter;
- public:
- Polynomial()
- {
- coefficient = new int [1];
- *coefficient = '\0';
- }
- Polynomial(char *);
- //copyconstructor
- Polynomial(const Polynomial &obj)
- {
- counter = obj.counter;
- coefficient = new int[counter];
- for (int i = 0; i < counter; i++)
- *(coefficient + i) = obj.coefficient[i];
- }
- //destructor
- ~Polynomial()
- {
- delete [] coefficient;
- }
- void setPolynomial(char *s);
- void getPolynomial() const
- {
- for (int i = 0; i < counter; i++)
- {
- cout << coefficient[i] << "x^" << i;
- if (i != counter - 1)
- cout << " + ";
- }
- }
- Polynomial operator + (const Polynomial &);
- Polynomial operator - (const Polynomial &);
- Polynomial operator * (const Polynomial &);
- const Polynomial operator=(const Polynomial &right)
- {;
- delete [] coefficient;
- counter = right.counter;
- coefficient = new int[counter];
- for (int i = 0; i < right.counter; i++)
- *(coefficient + i) = right.coefficient[i];
- return *this;
- }
- };
- int main()
- {
- char input[50];
- char input2[50];
- cout << "Example: -4x^0 + x^1 + 0x^2+ 4x^3 + -3x^4" << endl;
- cout << "Enter a polynomial: ";
- cin.getline(input, 50);
- Polynomial one(input);
- cout << "Enter second polynomial: ";
- cin.getline(input2, 50);
- Polynomial two(input2);
- int choice;
- do
- {
- cout << "1. Addition\n2. Subtraction\n3. Multiplication";
- cout << "\n4. Assignment\n5. Exit\nEnter your choice: ";
- cin >> choice;
- switch (choice)
- {
- case 1:
- {
- cout << "Addiction\n";
- Polynomial add = one + two;
- one.getPolynomial();
- cout << " + ";
- two.getPolynomial();
- cout << " = ";
- add.getPolynomial();
- cout << endl;
- }
- break;
- case 2:
- {
- cout << "Subtraction\n";
- Polynomial subtract = one - two;
- one.getPolynomial();
- cout << " - (";
- two.getPolynomial();
- cout << ") = ";
- subtract.getPolynomial();
- cout << endl;
- }
- break;
- case 3:
- {
- cout << "Multiplication\n";
- Polynomial multiply = one * two;
- one.getPolynomial();
- cout << " * (";
- two.getPolynomial();
- cout << ") = ";
- multiply.getPolynomial();
- cout << endl;
- }
- break;
- case 4:
- cout << "Assignment\n";
- cout << "First polynomial: ";
- one.getPolynomial();
- cout << "\nSecond polynomial: ";
- two.getPolynomial();
- cout << "\nFirst = Second\nFirst polynomial is now: ";
- one = two;
- one.getPolynomial();
- cout << endl;
- break;
- case 5:
- cout << "Goodbye\n";
- break;
- default:
- cout << "Invalid choice\n";
- break;
- }
- }while (choice != 5);
- return 0;
- }
- Polynomial::Polynomial(char *s)
- {
- char *string;
- string = new char [strlen(s) + 1];
- int length = strlen(string);
- strcpy(string, s);
- char *copy;
- copy = new char [length];
- strcpy(copy, string);
- char *p = strtok(string, " +-");
- counter = 0;
- while (p)
- {
- p = strtok(NULL, " +-");
- counter++;
- }
- coefficient = new int[counter];
- p = strtok(copy, " +");
- int a = 0;
- while (p)
- {
- long int coeff;
- char *endptr;
- coeff = strtol(p, &endptr, 10); //stops at first non number
- if (*p == 'x')
- coeff = 1;
- coefficient[a] = coeff;
- p = strtok(NULL, " +");
- a++;
- }
- }
- void Polynomial::setPolynomial(char *s)
- {
- char *string;
- string = new char [strlen(s) + 1];
- int length = strlen(string);
- strcpy(string, s);
- char *copy;
- copy = new char[length];
- strcpy(copy, string);
- char *p = strtok(string, " +-");
- counter = 0;
- while (p)
- {
- p = strtok(NULL, " +-");
- counter++;
- }
- coefficient = new int[counter];
- p = strtok(copy, " +");
- int a = 0;
- while (p)
- {
- long int coeff;
- char *endptr;
- coeff = strtol(p, &endptr, 10); //stops at first non number
- if (*p == 'x')
- coeff = 1;
- coefficient[a] = coeff;
- p = strtok(NULL, " +");
- a++;
- }
- }
- Polynomial Polynomial::operator + (const Polynomial &right)
- {
- Polynomial temp;
- //string, length, coefficient, counter
- int smallest, largest;
- smallest = (counter > right.counter) ? right.counter : counter;
- largest = (counter > right.counter) ? counter : right.counter;
- if (counter == right.counter)
- largest = counter;
- //make coefficient array
- temp.counter = largest;
- temp.coefficient = new int [largest];
- for (int i = 0; i < smallest; i++)
- temp.coefficient[i] = coefficient[i] + right.coefficient[i];
- for (int j = smallest; j < largest; j++)
- {
- if (counter > right.counter)
- temp.coefficient[j] = coefficient[j];
- else
- temp.coefficient[j] = right.coefficient[j];
- }
- return temp;
- }
- Polynomial Polynomial::operator - (const Polynomial &right)
- {
- Polynomial temp;
- int smallest, largest;
- smallest = (counter > right.counter) ? right.counter : counter;
- largest = (counter > right.counter) ? counter : right.counter;
- if (counter == right.counter)
- largest = counter;
- //make coefficient array
- temp.counter = largest;
- temp.coefficient = new int [largest];
- for (int i = 0; i < smallest; i++)
- temp.coefficient[i] = coefficient[i] - right.coefficient[i];
- for (int j = smallest; j < largest; j++)
- {
- if (counter > right.counter)
- temp.coefficient[j] = coefficient[j];
- else
- temp.coefficient[j] = right.coefficient[j];
- }
- return temp;
- }
- Polynomial Polynomial::operator * (const Polynomial &right)
- {
- Polynomial temp;
- //make coefficient array
- int count = (counter + right.counter) - 1;
- temp.counter = count;
- temp.coefficient = new int [count];
- for (int i = 0; i < counter; i++)
- {
- for (int j = 0; j < right.counter; j++)
- temp.coefficient[i+j] += coefficient[i] * right.coefficient[j];
- }
- return temp;
- }
Add Comment
Please, Sign In to add comment