COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
largenum.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <iomanip>
3#include <string>
4#include <cctype>
5#include <cstdlib>
6
7using namespace std;
8
9struct Node
10{
11 int value;
12 Node * next;
13};
14
15// output the linked list
16void print_list(Node * head)
17{
18 Node * current = head;
19 while (current != NULL)
20 {
21 // process the current node, e.g., print the content
22 cout << current->value << " -> ";
23 current = current->next;
24 }
25 cout << "NULL\n";
26}
27
28// output the large number stored in the linked list
29void print_num(Node * head)
30{
31 Node * current = head;
32 while (current != NULL)
33 {
34 if (current == head)
35 cout << current->value;
36 else
37 cout << setw(5) << setfill('0') << current->value;
38 current = current->next;
39 }
40}
41
42// insert a value as a node to the head of a linked list
43void head_insert(Node * & head, int v)
44{
45 Node * p = new Node;
46 p->value = v;
47 p->next = head;
48 head = p;
49}
50
51// delete the head node from a linked list
52void delete_head( Node * & head)
53{
54 if (head != NULL) {
55 Node * p = head;
56 head = head->next;
57 delete p;
58 }
59}
60
61// free an entire linked list
62void delete_list(Node * & head)
63{
64 while ( head != NULL )
65 {
66 delete_head(head);
67 }
68}
69
70// double the capacity of an array
71// array: input array
72// size: original size of array, updated to new size of array
73void grow_array( char * & array, int & size )
74{
75 if (array == NULL)
76 return;
77
78 int newSize = size * 2;
79
80 // doubled the size of the array;
81 char * tmp = new char [ newSize ];
82 // copy original contents
83 for (int i = 0; i < size; ++i)
84 tmp[i] = array[i];
85
86 delete [] array;
87
88 array = tmp;
89 size = newSize;
90}
91
92// get a number from a user
93// by reading character by character until a space is hit
94// use dynamic array to store the digits
95// digits: character array that stores the digits of the number
96// numDigits: number of digits read from input
97void input_num(char * & digits, int & numDigits)
98{
99 int arraysize = 32;
100 digits = new char [arraysize];
101 char c;
102 int numRead = 0;
103
104 // read each digit as a character until a white space is hit
105 c = cin.get();
106 while (!isspace(c))
107 {
108 if (numRead >= arraysize)
109 grow_array( digits, arraysize );
110
111 digits[numRead] = c;
112 numRead++;
113
114 c = cin.get();
115 }
116
117 numDigits = numRead;
118
119}
120
121// get a large integer from user input
122// and store in a linked list of Node
123// each node stores the value of a chunk of 5 digits taken from the large integer
124// e.g., if the input is 43323000089500012, the linked list is
125// 43 -> 32300 -> 895 -> 12 -> NULL
126//
128{
129 // TASK 1a: declare a pointer pointing to the head of the link list
130 Node * head = NULL;
131
132 string str;
133 char * digits = NULL; // a dynamic array for storing an input number
134 int numDigits;
135 int val;
136
137 // get a number from the user
138 input_num( digits, numDigits);
139
140 // scan the digits in reverse, and create a list of nodes for
141 // the value of every 5 digits
142 str.clear();
143 for (int i = numDigits-1; i >=0; --i) {
144 str = digits[i] + str;
145 if (str.length()==5) {
146 val = atoi(str.c_str());
147
148 // TASK 1b: insert a value as a node to the head of the linked list
149 head_insert(head, val);
150
151 str.clear();
152 }
153 }
154 // the digits array is scanned and there are still digits
155 // stored in str that are not inserted into the list yet
156 if (!str.empty()) {
157 val = atoi(str.c_str());
158
159 // TASK 1c: insert a value as a node to the head of the linked list
160 head_insert(head, val);
161 }
162
163 if (digits != NULL) {
164 delete [] digits;
165
166 }
167
168 // TASK 1d: return the pointer to the linked list
169 return head;
170}
171
172
173
174// return the length of a linked list
175int list_length(Node * head)
176{
177 // TASK 3: Modify this print function to one that
178 // count the number of nodes in a linked list
179
180 int num = 0;
181
182 Node * current = head;
183 while (current != NULL)
184 {
185 // process the current node, e.g., print the content
186 ++num;
187 current = current->next;
188 }
189
190 return num;
191}
192
193// return if the number n1 is larger than n2
194bool larger(Node * n1, Node * n2)
195{
196 int len1 = list_length(n1);
197 int len2 = list_length(n2);
198
199 // TASK 4a: handle the case
200 // when the list lengths are different
201 if (len1 > len2)
202 return true;
203 else if (len1 < len2)
204 return false;
205
206 // the two lists are of equal length
207
208 Node * curr1 = n1, * curr2 = n2;
209
210 while (curr1 != NULL) {
211 if (curr1->value > curr2->value)
212 return true;
213 else if (curr1->value < curr2->value)
214 return false;
215
216 // TASK 4b: advance curr1, curr2
217 // to point to the next nodes
218 curr1 = curr1->next;
219 curr2 = curr2->next;
220 }
221
222 return false;
223}
224
225
226int main()
227{
228 Node * n1, * n2;
229
230 cout << "expr> ";
231 n1 = create_num_list();
232 cin.get(); // skip the '>' sign
233 cin.get(); // the space after the '>' sign
234 n2 = create_num_list();
235
236 // TASK 2: call print_list() on n1 and n2 for checking
237 print_list(n1);
238 print_list(n2);
239
240 if (larger(n1, n2)) {
241 cout << "Yes, ";
242 print_num(n1);
243 cout << " is larger." << endl;
244 }
245 else {
246 cout << "No, ";
247 print_num(n1);
248 cout << " is not larger." << endl;
249 }
250
251 // TASK 5: free the linked lists
252 delete_list(n1);
253 delete_list(n2);
254
255 return 0;
256}
Node * create_num_list()
Definition largenum.cpp:127
void grow_array(char *&array, int &size)
Definition largenum.cpp:73
void delete_list(Node *&head)
Definition largenum.cpp:62
void print_num(Node *head)
Definition largenum.cpp:29
void delete_head(Node *&head)
Definition largenum.cpp:52
int list_length(Node *head)
Definition largenum.cpp:175
void head_insert(Node *&head, int v)
Definition largenum.cpp:43
bool larger(Node *n1, Node *n2)
Definition largenum.cpp:194
int main()
Definition largenum.cpp:226
void print_list(Node *head)
Definition largenum.cpp:16
void input_num(char *&digits, int &numDigits)
Definition largenum.cpp:97
Definition 3.cpp:6
int value
Definition 3.cpp:7
Node * next
Definition ex4ex5.cpp:8