COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
largenum_incomplete.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
131
132
133 string str;
134 char * digits = NULL; // a dynamic array for storing an input number
135 int numDigits;
136 int val;
137
138 // get a number from the user
139 input_num( digits, numDigits);
140
141 // scan the digits in reverse, and create a list of nodes for
142 // the value of every 5 digits
143 str.clear();
144 for (int i = numDigits-1; i >=0; --i) {
145 str = digits[i] + str;
146 if (str.length()==5) {
147 val = atoi(str.c_str());
148
149 // TASK 1b: insert a value as a node to the head of the linked list
150
151
152
153
154 str.clear();
155 }
156 }
157 // the digits array is scanned and there are still digits
158 // stored in str that are not inserted into the list yet
159 if (!str.empty()) {
160 val = atoi(str.c_str());
161
162 // TASK 1c: insert a value as a node to the head of the linked list
163
164
165
166 }
167
168 // free the dynamic array
169 if (digits != NULL) {
170 delete [] digits;
171
172 }
173
174 // TASK 1d: return the pointer to the linked list
175
176
177}
178
179
180
181// return the length of a linked list
182int list_length(Node * head)
183{
184 // TASK 3: Modify this print function to one that
185 // count the number of nodes in a linked list
186
187 Node * current = head;
188 while (current != NULL)
189 {
190 // process the current node, e.g., print the content
191 cout << current->value << " -> ";
192 current = current->next;
193 }
194
195 cout << "NULL\n";
196}
197
198// return if the number n1 is larger than n2
199bool larger(Node * n1, Node * n2)
200{
201 int len1 = list_length(n1);
202 int len2 = list_length(n2);
203
204 // TASK 4a: handle the case
205 // when the list lengths are different
206
207
208
209
210 // the two lists are of equal length
211
212 Node * curr1 = n1, * curr2 = n2;
213
214 while (curr1 != NULL) {
215 if (curr1->value > curr2->value)
216 return true;
217 else if (curr1->value < curr2->value)
218 return false;
219
220 // TASK 4b: advance curr1, curr2
221 // to point to the next nodes
222
223
224
225
226 }
227
228 return false;
229}
230
231
232
233int main()
234{
235 Node * n1, * n2;
236
237 cout << "expr> ";
238 n1 = create_num_list();
239 cin.get(); // skip the '>' sign
240 cin.get(); // the space after the '>' sign
241 n2 = create_num_list();
242
243 // TASK 2: call print_list() on n1 and n2 for checking
244
245
246
247
248 if (larger(n1, n2)) {
249 cout << "Yes, ";
250 print_num(n1);
251 cout << " is larger." << endl;
252 }
253 else {
254 cout << "No, ";
255 print_num(n1);
256 cout << " is not larger." << endl;
257 }
258
259 // TASK 5: free the linked lists
260
261
262
263
264 return 0;
265}
Node * create_num_list()
void grow_array(char *&array, int &size)
void delete_list(Node *&head)
void print_num(Node *head)
void delete_head(Node *&head)
int list_length(Node *head)
void head_insert(Node *&head, int v)
bool larger(Node *n1, Node *n2)
int main()
void print_list(Node *head)
void input_num(char *&digits, int &numDigits)
Definition 3.cpp:6
int value
Definition 3.cpp:7
Node * next
Definition ex4ex5.cpp:8