## Experiment report of unary sparse polynomial calculator

##### Course Name: data structure

##### Experiment Name: unary sparse polynomial calculator

##### College: Qian Xuesen College

##### Experiment date: May 8, 2020

Promise of good faith: I promise that the procedures in this experiment report and this experiment report are compiled by myself, without plagiarism.

### 1、 Purpose of the experiment

- Familiar with the data structure of one variable polynomial addition (using linked list)
- Strengthen the basic operation of C + + and the use of STL
- In depth understanding of class encapsulation, abstract meaning, and use in practice

### 2、 Experimental environment

- Operating system: Windows 10
- IDE：VSCODE

### 3、 Project design and Implementation

First, we need to design the type of data storage. Here we use a struct.

```
struct Node
{
int cof;
int pow;
Node *next; //coefficient power
};
```

Secondly, we use a linked list to record a polynomial. Here you create a class list.

```
class List
{
public:
Node *head;
public:
~List();
void buildlst();
};
//Head is the head node of polynomial
```

Finally, the class ploy is used to provide a variety of operation symbol overloading

```
class Ploy //Polynomial
{
public:
List sequence;
//size=sequence->head->data
~Ploy()
{
// sequence.~List();
}
Ploy operator+(const Ploy &a);
Ploy operator-(const Ploy &a);
int cal(int x);
};
```

It can be seen that the overloading of symbols + and – is provided in poly, which makes the code “part” and easy to check and modify.

Let’s take a detailed look at the implementation of operator +

```
Ploy Ploy::operator+(const Ploy &a)
{
Ploy temp;
Node *now,
*p = sequence.head->next,
*q = a.sequence.head->next; // It's a little bit over the node, because it stores the number of polynomial terms
now = new Node;
temp.sequence.head = now;
now->cof = 0;
now->pow = 0;
int length = 0;
while (p != NULL && q != NULL)
{
Node *t = new Node;
now->next = t;
now = t;
t->next = NULL;
//They were treated separately
//1. Current item P > Q
//2.p<q
//3.p==q
if (p->pow > q->pow)
{
t->pow = p->pow;
t->cof = p->cof;
p = p->next;
}
else if (p->pow < q->pow)
{
t->pow = q->pow;
t->cof = q->cof;
q = q->next;
}
else
{
t->pow = q->pow;
t->cof = q->cof + p->cof;
p = p->next;
q = q->next;
}
length++;
// cout<<" "<<t->cof<<" ";
}
//Process the remaining items
while (p != NULL)
{
Node *t = new Node;
now->next = t;
now = t;
t->next = NULL;
t->cof = p->cof;
t->pow = p->pow;
p = p->next;
length++;
}
while (q != NULL)
{
Node *t = new Node;
now->next = t;
now = t;
t->next = NULL;
t->cof = q->cof;
t->pow = q->pow;
q = q->next;
length++;
}
temp.sequence.head->cof = length;
return temp;
}
```

Finally, the main function is used for testing

```
int main()
{
cout << "Build a Polynomial\n";
Ploy a, b;
a.sequence.buildlst();
b.sequence.buildlst();
cout << "write down the number X\n";
int x;
cin >> x;
cout << (a + b).cal(x);
cout << endl;
cout << (a - b).cal(x);
system("pause");
}
```

### 4、 Experimental results

You can see that the polynomials 3x ^ 3 + 2x ^ 2 + X and 6x ^ 4 + 2x ^ 3 are input, and let x = 2

`A+B=642`

A-B=-492

### 5、 Experiment summary

In this experiment, we independently completed the unary sparse polynomial calculator, deeply understood the encapsulation and abstraction of C + + classes, handled some errors that may occur due to data, and enhanced the robustness of the program. However, due to the limitation of the size of C language int, it can not process larger data, which can be solved by high-precision calculation, but it is beyond the scope of this experiment.