Data Structure Learning (C ++) - Single List Application (1 yuan in multiplex [2])

zhaozj2021-02-16  58

According to the original arrangement, the explanation of the polynomial to the previous article should be over, but I still want to do some extensions. For example, you are very clear that the coefficients of the polynomial are certainly not always integers, but why use integers? I see the original book is integrity, I also have this question. However, once you move your hand, you will find that it is not only to define the Term; change to float coef; a lot of details should be considered (give a prompt, how many floating point is ). I tried it, I finally gave up; the reason is that these are just for learning, there is no need to make so complicated, you can explain the problem.

In the following, some heavy load operators will be an example of our work will be to make a polynomial operation look more in line usage. Complete this is what I think I will change the " " of the original book to PolyAdd (), always give it to you. Soon, you will see how important the in situ calculation is more important in the polynomial operation. Before you look down, make sure you understand the content.

Ready to work

Hereinafter, the overload of the assignment operation of the single-strand list will be completed, please add this part to the public part of the list class. Indeed, this part can also be placed in a polynomial class; however, copying a polynomial is actually copying a single-link table, making a multi-term assignment with its single-lapse list, making the derived class can be shared.

Operator = (Const List & l)

{

Makeempty ();

For (Node * p = l.first-> link; p! = null; p = p-> link) LastInsert (P-> DATA);

}

Remember this List (const list & l) in the List class. When I was afraid of it, I used it directly, since now = can be used, for this grammar List b = a; the way is also completed. Now you can put it from private to public.

List (Const List & l)

{

FigSt = current = last = new node ; prior = null;

For (Node * p = l.first-> link; p! = null; p = p-> link) LastInsert (P-> DATA);

}

Finally, I can write A = B C * D this way.

Friend Polynomial Operator (Polynomial & Polya, Polynomial & Polyb)

{

Polynomial tempa = polya; polynomial tempb = polyb;

PolyAdd (Tempa, Tempb);

Return TEMPA;

}

Friend Polynomial Operator * (Polynomial & Polya, Polynomial & Polyb)

{

Node * PA = polya.pgetfirst () -> link;

Node * Pb = polyb.pgetfirst () -> link;

Polynomial Polytempa, PolyTempb;

INT COEF, Exp;

IF (PA == NULL || PB == Null) Return Polytempa; for (PA = POLYA.PGETFIRST () -> LINK; PA! = NULL; PA = PA-> Link)

{

For (PB = polyb.pgetfirst () -> link; pb! = null; pb = pb-> link)

{

COEF = PA-> Data.coef * Pb-> data.coef;

Exp = PA-> DATA.EXP PB-> Data.exp;

Term Term (COEF, Exp);

Polytempb.lastInsert (TERM);

}

PolyAdd (POLYTEMPA, POLYTEMPB);

Polytempb.initialize ();

}

Return PolyTempa;

}

[Post "is obvious, I am lazy on" ", but this is the most convenient. As long as the multiplication part is referred to, it is still very simple, I will not explain. For "-", you can complete (-a) such an algorithm, then you can use the addition to complete, and if you are like me, it is likely to do this - A = -1 × a, really not Advocate, ultra-low efficiency. For division, if you use compilation to write multi-byte division (like manual calculations), you can make a gourd can also make it out, but you must first complete "-". If you want to write, you have to have a long, leave it to you. Here you understand the importance of the in situ addition, these operations are actually achieved by it.

转载请注明原文地址:https://www.9cbs.com/read-22995.html

New Post(0)