bsc

Comprehensive codebase and coursework for the 4-year B.Sc. Computer Science Honours degree (WBSU)

git clone https://github.com/notamitgamer/bsc.git
· rebuilt hourlyOpen on GitHub
root / semester_2 / assignments / assignment_13.cpp

assignment_13.cpp (4500B)


      1 /* WAP to scan a polynomial using linked list and add two polynomials. */
      2 
      3 #include <iostream>
      4 #include <cmath>
      5 using namespace std;
      6 
      7 struct node {
      8     int coeff, exp;
      9     node *next = nullptr;
     10 };
     11 
     12 class Polynomial {
     13 private:
     14     node *head;
     15 
     16     void clear() {
     17         node *temp;
     18         while (head != nullptr) {
     19             temp = head;
     20             head = head->next;
     21             delete temp;
     22         }
     23     }
     24 
     25     void copyFrom(const Polynomial &other) {
     26         if (other.head == nullptr) {
     27             head = nullptr;
     28             return;
     29         }
     30         head = new node{other.head->coeff, other.head->exp, nullptr};
     31         node *current = head;
     32         node *otherCurrent = other.head->next;
     33         while (otherCurrent != nullptr) {
     34             current->next = new node{otherCurrent->coeff, otherCurrent->exp, nullptr};
     35             current = current->next;
     36             otherCurrent = otherCurrent->next;
     37         }
     38     }
     39 
     40 public:
     41     Polynomial() { head = nullptr; }
     42 
     43     Polynomial(const Polynomial &other) {
     44         copyFrom(other);
     45     }
     46 
     47     Polynomial& operator=(const Polynomial &other) {
     48         if (this != &other) {
     49             clear();
     50             copyFrom(other);
     51         }
     52         return *this;
     53     }
     54 
     55     ~Polynomial() {
     56         clear();
     57     }
     58 
     59     void insertTerm(int coeff, int exp) {
     60         if (coeff == 0) return;
     61         node *newNode = new node{coeff, exp, nullptr};
     62 
     63         if (head == nullptr || exp > head->exp) {
     64             newNode->next = head;
     65             head = newNode;
     66             return;
     67         }
     68         if (head->exp == exp) {
     69             head->coeff += coeff;
     70             delete newNode;
     71             if (head->coeff == 0) {
     72                 node* temp = head;
     73                 head = head->next;
     74                 delete temp;
     75             }
     76             return;
     77         }
     78         node *temp = head;
     79         while (temp->next != nullptr && temp->next->exp > exp) temp = temp->next;
     80         if (temp->next != nullptr && temp->next->exp == exp) {
     81             temp->next->coeff += coeff;
     82             delete newNode;
     83             if (temp->next->coeff == 0) {
     84                 node* toDelete = temp->next;
     85                 temp->next = temp->next->next;
     86                 delete toDelete;
     87             }
     88         } else {
     89             newNode->next = temp->next;
     90             temp->next = newNode;
     91         }
     92     }
     93 
     94     void inputPolynomial() {
     95         int n, coeff, exp;
     96         cout << "Enter number of terms: ";
     97         cin >> n;
     98         for (int i = 0; i < n; i++) {
     99             cout << "Coefficient: "; cin >> coeff;
    100             cout << "Exponent: "; cin >> exp;
    101             insertTerm(coeff, exp);
    102         }
    103     }
    104 
    105     Polynomial add(const Polynomial &other) const {
    106         Polynomial result;
    107         node *p = head, *q = other.head;
    108         while (p != nullptr && q != nullptr) {
    109             if (p->exp == q->exp) {
    110                 int sum = p->coeff + q->coeff;
    111                 if (sum != 0) result.insertTerm(sum, p->exp);
    112                 p = p->next; q = q->next;
    113             } else if (p->exp > q->exp) {
    114                 result.insertTerm(p->coeff, p->exp);
    115                 p = p->next;
    116             } else {
    117                 result.insertTerm(q->coeff, q->exp);
    118                 q = q->next;
    119             }
    120         }
    121         while (p != nullptr) { result.insertTerm(p->coeff, p->exp); p = p->next; }
    122         while (q != nullptr) { result.insertTerm(q->coeff, q->exp); q = q->next; }
    123         return result;
    124     }
    125 
    126     void display() const {
    127         if (head == nullptr) { cout << "0\n"; return; }
    128         node *temp = head;
    129         bool first = true;
    130         while (temp != nullptr) {
    131             if (!first && temp->coeff > 0) cout << " + ";
    132             else if (!first && temp->coeff < 0) cout << " - ";
    133             else if (temp->coeff < 0) cout << "-";
    134             int c = abs(temp->coeff);
    135             if (temp->exp == 0) cout << c;
    136             else if (temp->exp == 1) cout << (c == 1 ? "x" : to_string(c) + "x");
    137             else cout << (c == 1 ? "x^" + to_string(temp->exp) : to_string(c) + "x^" + to_string(temp->exp));
    138             first = false;
    139             temp = temp->next;
    140         }
    141         cout << endl;
    142     }
    143 };
    144 
    145 int main() {
    146     Polynomial p1, p2, result;
    147     cout << "--- First Polynomial ---\n";
    148     p1.inputPolynomial();
    149     cout << "--- Second Polynomial ---\n";
    150     p2.inputPolynomial();
    151 
    152     cout << "First: "; p1.display();
    153     cout << "Second: "; p2.display();
    154 
    155     result = p1.add(p2);
    156     cout << "Sum: "; result.display();
    157     return 0;
    158 }