Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class LongInt // class declaration section
- {
- public:
- LongInt();
- LongInt(int[], int);
- void print() const;
- void read();
- LongInt add(LongInt) const;
- LongInt substract(LongInt) const;
- private:
- int *a;
- int length;
- };
- LongInt::LongInt() { //Class implementation section
- a = new int [5000];
- length = 5000;
- for(int i=0;i<length;i++)
- a[i]=0;
- }
- LongInt::LongInt(int num[], int l) {
- a = new int[5000];
- length = l;
- for(int i=0;i<length;i++)
- a[i]=num[i];
- }
- void LongInt::print() const {
- bool nozero = false ;
- //mycode
- for(int i=0;i<length;i++) {
- if(a[i] == 0 && nozero == false) {
- nozero = false;
- }
- else {
- nozero = true;
- cout << a[i];
- }
- }
- cout << endl;
- }
- void LongInt::read() {
- char c;
- cin.get(c);
- int l = 0;
- while (c != '+' && c != '-' && c != '$') {
- if('0' <= c && c <= '9')
- a[l++] = c - '0';
- cin.get(c);
- }
- length = l;
- }
- LongInt LongInt::add(LongInt other) const {
- LongInt result;
- //Make length of result +1 of largest of both integers
- result.length = ((length>other.length) ? length : other.length) + 1;
- result.a = new int [result.length];
- int carry = 0; //initial carry is 0
- int p1 = length - 1;
- int p2 = other.length - 1;
- int p3 = result.length - 1;
- for (int i=p3; i>0; i--) {
- result.a[p3] = carry;
- if (p1 >= 0) {
- result.a[p3] = result.a[p3] + a[p1];
- p1--;
- }
- if (p2 >= 0) {
- result.a[p3] = result.a[p3] + other.a[p2];
- p2--;
- }
- carry = (result.a[p3])/10;
- result.a[p3] = (result.a[p3])%10;
- p3--;
- }
- //last carry
- result.a[p3] = carry;
- return result;
- }
- LongInt LongInt::substract(LongInt other) const {
- LongInt result;
- //Make length of result -1 of largest of both integers
- result.length = ((length > other.length) ? length : other.length);
- result.a = new int [result.length];
- /* int carry = 0; //initial carry is 0 */
- int p1 = length - 1;
- int p2 = other.length - 1;
- int p3 = result.length - 1;
- for (int i=p3; i>0; i--) {
- if (other.a[p2] > a[p1]) { a[p1] = a[p1] + 10;
- a[p1-1] = a[p1-1] -1 ;
- result.a[p1] = a[p1] - other.a[p2];
- }
- else {
- result.a[p1] = a[p1] - other.a[p2];
- }
- /*result.a[p3] = carry;
- if (p1 >= 0) {
- result.a[p3] = result.a[p3] + a[p1];
- p1--;
- }
- if (p2 >= 0) {
- result.a[p3] = result.a[p3] - other.a[p2];
- p2--;
- }
- carry = (result.a[p3] +10);
- result.a[p3] = (result.a[p3]) -10;
- p3--;*/
- p1--;
- p2--;
- p3--;
- }
- return result;
- //my code
- }
- //client code to test the functinoality of the class LongInt
- int main() {
- LongInt I1,I2,I3,I4,I5;
- I1.read();
- cout << "First Integer is ";
- I1.print();
- I2.read();
- cout << "Second integer is: ";
- I2.print();
- I3.read();
- cout << "Third Integer is: " ;
- I3.print();
- I4 = I2.add(I1); //add them
- cout << "\nFirst + Second is: " << endl;
- I4.print();
- I5 = I4.substract(I3); // substrect I4 - I3
- cout << "\nTotal - Third is: " << endl;
- I5.print();
- return 0;
- }
Add Comment
Please, Sign In to add comment