Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Polynomial::insertTerm(double coeff, int exp) {
- if (coeff != 0) {
- if (head == NULL || exp > head -> exp){
- insertNode(NULL, coeff, exp);
- return;
- }
- Node *temp = head;
- while (temp -> link != NULL && temp -> link -> exp > exp)
- temp = temp -> link;
- if (temp -> link == NULL){
- if (temp -> exp == exp){
- temp -> coeff += coeff;
- if (temp -> coeff == 0)
- deleteNode(NULL, temp);
- }
- else
- insertNode(temp, coeff, exp);
- }
- else {
- if (exp == temp -> link -> exp){
- temp -> link -> coeff += coeff;
- if (temp -> link -> coeff == 0)
- deleteNode(temp, temp -> link);
- }
- else
- insertNode(temp, coeff, exp);
- }
- }
- return;
- }
- void Polynomial::print() {
- if (head == NULL)
- return;
- Node *temp = head;
- while (temp != NULL){
- double posCoeff = temp -> coeff;
- if (temp -> coeff < 0 && temp != head)
- posCoeff = - temp -> coeff;
- if (temp -> exp > 1)
- cout << posCoeff <<"x" <<"^" <<temp -> exp ;
- if (temp -> exp == 1)
- cout << posCoeff <<"x" ;
- if (temp -> exp == 0)
- cout << posCoeff;
- if (temp -> link != NULL){
- if (temp -> link -> coeff > 0)
- cout <<" + ";
- else
- cout <<" - ";
- }
- temp = temp -> link;
- }
- cout << endl;
- }
- Polynomial Polynomial::operator+(const Polynomial& b) {
- Polynomial result = b;
- Node *temp = head;
- while (temp != NULL){
- result.insertTerm(temp -> coeff, temp -> exp);
- temp = temp -> link;
- }
- return result;
- }
- Polynomial Polynomial::operator*(const Polynomial& b) {
- Polynomial result;
- Node *tempA = head;
- while (tempA != NULL) {
- Node *tempB = b.head;
- while (tempB != NULL) {
- result.insertTerm(tempA -> coeff * tempB -> coeff, tempA -> exp + tempB -> exp);
- tempB = tempB -> link;
- }
- tempA = tempA -> link;
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement