-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
182 lines (157 loc) · 4.63 KB
/
main.cpp
File metadata and controls
182 lines (157 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//Kurt Fitz
#include <iostream>
#include <fstream>
#include "linkedListType.cpp"
using namespace std;
int main(int argc, char** argv)
{
//function to add two linked lists
linkedListType* addNumbers(linkedListType* n1, linkedListType* n2);
//function to multiply two linked lists
linkedListType* multiplyNumbers(linkedListType* n1, linkedListType* n2);
linkedListType* num1;
linkedListType* num2;
num1 = new linkedListType();
num2 = new linkedListType();
if(argc != 4){ //argc should be 3 for correct execution (two input, one output).
cout << "usage: " << argv[0] << " expected three 3 files at command line two input files, and one output file." << endl;
return 1;
}
ifstream in1(argv[1]);
ifstream in2(argv[2]);
ofstream out(argv[3]);
//open first input file and read in number
if(in1.is_open()){
while(in1.good()){ //loop while extraction from file is possible
char c = in1.get(); //get character from file
if(in1.good()){
num1->insert(c);
}
}
in1.close(); //close the file
}
else cout << "Unable to open file1" << endl;
//open second input file and read in number
if(in2.is_open()){
while(in2.good()){
char c = in2.get();
if(in2.good()){
num2->insert(c);
}
}
in2.close(); //close the file
}
else cout << "Unable to open file2" << endl;
//Call addNumbers and print result
linkedListType* result;
result = new linkedListType();
result = addNumbers(num1, num2);
cout << "Call to addNumbers with num1, num2 :" << endl;
result->print();
if(out.is_open()){
digitType* tempNum;
string output = "";
tempNum = new digitType();
tempNum = result->getFirst();
out << "Results of addition of num1, num2: ";
while(tempNum->link != NULL){
output = tempNum->info + output;
tempNum = tempNum->link;
}
if(tempNum->link == NULL)
output = tempNum->info + output;
out << output << endl;
}
//Call multiplyNumbers and print result
cout << "Call to multiplyNumbers with num1, num2: " << endl;
result = multiplyNumbers(num1, num2);
result->print();
if(out.is_open()){
digitType* tempNum;
string output = "";
tempNum = new digitType();
tempNum = result->getFirst();
out << "Results of multiplcation of num1, num2: ";
while(tempNum->link != NULL){
output = tempNum->info + output;
tempNum = tempNum->link;
}
if(tempNum->link == NULL)
output = tempNum->info + output;
out << output << endl;
out.close();
}
return 0;
};
linkedListType* addNumbers(linkedListType* num1, linkedListType* num2)
{
linkedListType* result;
result = new linkedListType();
digitType* firstNum = new digitType();
digitType* secondNum = new digitType();
int carry = 0;
firstNum = num1->getFirst();
secondNum = num2->getFirst();
while(firstNum != NULL || secondNum != NULL)
{
// subtracting the char '0' converts char to int
int val1 = firstNum != NULL ? firstNum->getInfo() : 0;
int val2 = secondNum != NULL ? secondNum->getInfo() : 0;
int temp = carry + val1 + val2;
if(temp > 9){
carry = temp / 10;
temp = temp % 10;
} else {
carry = 0;
}
result->insertLast(temp+'0');
firstNum = firstNum != NULL ? firstNum->link : NULL;
secondNum = secondNum != NULL ? secondNum->link : NULL;
}
if(carry>0)
result->insertLast(carry+'0');
return result;
}
linkedListType* multiplyNumbers(linkedListType* num1, linkedListType* num2){
linkedListType* result;
result = new linkedListType();
digitType* firstNum;
digitType* secondNum;
digitType* tempSecondNum;
firstNum = new digitType();
secondNum = new digitType();
tempSecondNum = new digitType();
tempSecondNum = secondNum;
firstNum = num1->getFirst();
secondNum = num2->getFirst();
int carry = 0;
int placeHolder = 0;
while(firstNum != NULL){
int val1 = firstNum != NULL ? firstNum->getInfo() : 0;
linkedListType* tempList;
tempList = new linkedListType();
for(int i = 0; i<placeHolder; i++){
tempList->insert(0 + '0');
}
while(secondNum != NULL){
int val2 = secondNum != NULL ? secondNum->getInfo() : 0;
int temp = val1 * val2 + carry;
if(temp > 9){
carry = temp / 10;
temp = temp % 10;
}else{
carry = 0;
}
tempList->insertLast(temp + '0');
secondNum = secondNum != NULL ? secondNum->link : NULL;
}
if(carry>0){
tempList->insertLast(carry + '0');
}
result = addNumbers(tempList, result);
secondNum = num2->getFirst();
firstNum = firstNum != NULL ? firstNum->link : NULL;
placeHolder = placeHolder + 1;
}
return result;
}