ernie.WPI.EDU> cat n_int.h // file n_int.h, cs2223, D97/98 // class definition of an n-digit int implemented as a stack // 01Mar98 NW First version #ifndef N_INT_H #define N_INT_H /*** includes ***/ #include #include #include #include #include #include class n_int { private: stack st; // the int void reverse(); // reverse the order in the stack public: void operator=(n_int); // overloaded replacement operator n_int operator+(n_int); // overloaded addition operator void get_file(char *name); // read a data file void put_file(char *name); // write a data file void print(int line_length); // print a number }; #endif // N_INT_H // Copyright 1998 Norman Wittels ernie.WPI.EDU> cat n_int.C // file n_int.C, cs2223, D97/98 // code for n-digit integer class // 01Mar98 NW First version #include "n_int.h" // private functions void n_int::reverse() // reverse the order in the stack { queue q; // temp holding place while (!st.empty()) // enqueue the stack { q.push(st.top()); st.pop(); } while(!st.empty()) st.pop(); // clear the stack while (!q.empty()) // dequeue into the stack { st.push(q.front()); q.pop(); } } // end reverse() // public functions void n_int::operator=(n_int other) { st = other.st; // copy the stack } // end operator=() n_int n_int::operator+(n_int other) // overloaded addition operator { int sum, carry; n_int answer; stack first, second; // temp stacks first = st; second = other.st; carry = 0; while (!first.empty() || !second.empty()) { sum = 0; if (!first.empty()) { sum += first.top(); first.pop(); } if (!second.empty()) { sum += second.top(); second.pop(); } sum += carry; answer.st.push(sum%10); carry = sum/10; } if (carry != 0) answer.st.push(carry); answer.reverse(); // put answer in right order return answer; } // end operator+(); void n_int::get_file(char *name) // read a data file { ifstream the_file; the_file.open(name); // open the file if (!the_file) // could not open the file { while(!st.empty()) st.pop(); // make sure the stack is empty return; } char temp; while (the_file >> temp) // read a digit { temp -= 48; // convert from char to a number if ((temp < 0) || (temp > 9)) temp = 0; // make sure it is a digit st.push(temp); // add to the stack } return; } // end get_file() void n_int::put_file(char *name) // write a data file { ofstream the_file; the_file.open(name); // open the file if (!the_file) return; // could not open the file stack temp_stack; temp_stack = st; // keep a copy because the later popping is destructive reverse(); // reverse the stack char temp; while (!st.empty()) // pop the whole stack { temp = st.top(); st.pop(); temp += 48; // convert from char to a number the_file << temp; // write to file } st = temp_stack; // restore the stack return; } // end put_file() void n_int::print(int line_length) // write a data file { stack temp_stack; temp_stack = st; // keep a copy because the later popping is destructive reverse(); // reverse the stack int temp; int count = 0; while (!st.empty()) // pop the whole stack { if (count >= line_length) { count = 0; cout << endl; } temp = st.top(); // get a digit st.pop(); cout << temp; // print a digit count++; } st = temp_stack; // restore the stack return; } // end put_file() // Copyright 1998 Norman Wittels ernie.WPI.EDU> cat add_n.C // file add_n.C, cs2223, D97/98 // // program which performs n-digit addition // // 08Mar98 NW, First version // // includes // #include #include "n_int.h" int main () { n_int n1, n2, n3; // the numbers n1.get_file("n1.dat"); cout << "n1" << endl; n1.print(20); cout << endl << endl; n2.get_file("n2.dat"); cout << "n2" << endl; n2.print(20); cout << endl << endl; n3 = n1 + n2; cout << "n3" << endl; n3.put_file("n3.dat"); n3.print(20); cout << endl; return 0; } // end main() // Copyright 1998 Norman Wittels ernie.WPI.EDU> g++ *.C ernie.WPI.EDU> a.out n1 01234567890123456789 01234567890123456789 01234567890123456789 01234567890123456789 01234567890123456789 0 n2 96557316587678891032 16802647625346513035 46312346343636230300 58797455897951518186 87634684600635469846 84135894974981312198 4987603055789745 n3 96557316587678891032 16802647625346514270 03101358689315131535 15586468243630419421 44423696946314371081 40924907320660213433 0666504290357635 ernie.WPI.EDU>