1. Preface
The implementation of the large number of subtraction is the winter vacation in C + + 's big job, originally I was written in string, but later to see the job requirements to use the chain list to achieve, so I do not want to use the list of the realization of the
2.Num class 2.1Node Class
First, the internal node class is used to create a one-way list, and size is used to calculate sizes to facilitate comparisons between NUM objects
class Node { public: int val; Node*next; Node(int v, Node*n) { val = v; next = n; } }; Node*head; int size;
2.2 Constructors, assignment functions, and destructors
The first is the default constructor, which may not actually be used, but for convenience, it is still written
Num() { head = NULL; 0; false; }
Then is the primary constructor, when encountering '. ' , we use-100来 as a marker.
The linked list is a number of reverse construction, convenient for adding and reducing operations
Num(const string&s) { false; head = NULL; 0; for (int0; i < s.size(); i++) { if','||s[i]=='-') continue; size++; if'.') new Node(-100, head); else new'0', head); } }
Next is the copy constructor and the assignment function, which are basically the same
It is important to note that the assignment function first confirms whether the assignment object is itself, and if it is itself, returns the value of the
Num (Constnum& num) {D = num. D size = Num.size; head = NULL; Node*cur = Num.head, *temp=head; while(cur) {if(!head) {head =NewNode (Cur->val, NULL); temp = head; }Else{Temp->next =NewNode (Cur->val, NULL); temp = temp->next; } cur = cur->next; }} num&operator=(ConstNum&num) {if( This= = &num)return* This; Free (); size = Num.size; head = NULL; D = num. D Node*cur = Num.head, *temp=head; while(cur) {if(!head) {head =NewNode (Cur->val, NULL); temp = head; }Else{Temp->next =NewNode (Cur->val, NULL); temp = temp->next; } cur = cur->next; }return* This; }
Finally, the destructor, we only need to reclaim the memory of the linked list
To facilitate recycling of the linked list memory within the function, write the recycle operation as the free function
~Num() { free(); } void free() { 0; while (head) { Node*temp = head->next; delete head; head = temp; } head = NULL; }
The above is the basic constituent function of the Num class
2.3 Addition operation and subtraction operation
For ease of operation, the addition and subtraction of the large number in front, the decimal in the following, directly to the object itself, so use +=,-= and return the object itself reference
Add and subtract operations nothing to say, starting from the lowest bit to add in turn
An operation that determines whether a decimal point is performed when rounding or borrow, in case of an incorrect operation
num&operator+=(ConstNum&num) {Node*pa = head, *PB = Num.head; while(pa| | PB) {intb =0;if(PB) {b = pb->val; PB = pb->next; }if(Pa->val! =-100) {Pa->val + = b;if(Pa->val >9) {if(Pa->next) {if(Pa->next->val = =-100) pa->next->next->val++;Elsepa->next->val++; }Else{Pa->next =NewNode (1, NULL); size++; } Pa->val-=Ten; }} PA = pa->next; }return* This; } num&operator-=(ConstNum&num) {Node*pa = head, *PB = Num.head; while(PA | | pb) {intb =0;if(PB) {b = pb->val; PB = pb->next; }if(Pa->val! =-100) {Pa->val-= b;if(Pa->val <0) {if(Pa->next->val = =-100) pa->next->next->val--;Elsepa->next->val--; Pa->val + =Ten; }} PA = pa->next; }return* This; }
2.4 Multiplication operation
Multiplication is actually like a column calculation, which involves rounding, but it's easy to understand
operator*(const Num&num) { '0')); Node *pr = res.head, *temp; for (Node*pa = head; pa != NULL; pa = pa->next) { int0; temp = pr; for (Node*pb = num.head; pb != NULL; pb = pb->next,pr=pr->next) { int temp = pa->val*pb->val + carry + pr->val; 10; 10; } pr->val += carry; pr = temp->next; } return res; }
2.5 Division Operation
Division operation we use the divisor minus removal number multiplied by a dry 10
Like 58 divided by 5 first with 58-50=8 then quotient plus 10 then with 8-5=3 quotient plus 1 because 3:5 small, do not operate the final result is 11
However, the topic requires division operation to keep 10 decimal places rounding, we only need to dividend times the 10^11, and finally add the decimal point to rounding operations
We first implemented two functions related to 10
void Mul10(constint& x) { for (int0; i < x; ++i) { new Node(0, head); size++; } } void Div10(constint&x) { for (int0; i < x; ++i) { Node*temp = head->next; delete head; head = temp; size--; } }
Then the division operation, RES is used to return the result, p is used to add to res
D for subtracting from dividend
Numoperator/(Constnum&num) {num res (string (1,' 0 '), p (String (1,' 1 ')); Res. Btrue; P.D =true; num d = num; while(num<=* This) {intlen = size-num.size; D.mul10 (len); P.mul10 (len);intz =0;if(* This< D) {D.DIV10 (1); P.DIV10 (1); z =-1; }if(Res.head->val = =0&&res.size==1) res = p;Elseres + = P; * This-= D; Balance (); D.DIV10 (len + z); P.DIV10 (len + z); }returnRes }
We notice that there is a comparison < operation in the division operation, and then the overload < function
Subtraction of large numbers using linked list for C + +