class HugeInteger
{
public:
       HugeInteger();
       
       void input();
       int setNumber(char []);
       
       void output();
       void convertToInteger();
       void subtract();
private:
        char a[41];
        char b[41];
        int *array1;
        int *array2;
        int result[41];
};

 
 
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
#include "HugeInteger.h"
HugeInteger::HugeInteger()
{
   input();
}
void HugeInteger::input()
{
   cout<<"Enter the 1st Integer: ";
   cin.getline(a,70,'\n');
   
   while(setNumber(a)!=2)
   {
      if(setNumber(a)==0)
      {
         cout<<a<<" -> Invalid Input.\n\nEnter the 1st integer: ";
         cin.getline(a,70,'\n');
      }
            
      if(setNumber(a)==1)
      {
         cout<<a<<" -> Too Long.\n\nEnter the 1st integer: ";
         cin.getline(a,70,'\n');
      }
   }
   
   cout<<"Enter the 2nd Integer: ";
   cin.getline(b,70,'\n');
   
   while(setNumber(b)!=2)
   {
      if(setNumber(b)==0)
      {
         cout<<b<<" -> Invalid Input.\n\nEnter the 2nd integer: ";
         cin.getline(b,70,'\n');
      }
            
      if(setNumber(b)==1)
      {
         cout<<b<<" -> Too Long.\n\nEnter the 2nd integer: ";
         cin.getline(b,70,'\n');
      }
   }
   
   cout<<endl;
   output();
}
int HugeInteger::setNumber(char string[])
{
   for( int i = 0; string[i] != '\0'; i++ )
   {
      if(string[i]=='0'||string[i]=='1'||string[i]=='2'||string[i]=='3'||string[i]=='4'){}
      else if(string[i]=='5'||string[i]=='6'||string[i]=='7'||string[i]=='8'||string[i]=='9'){}
      else
         return 0;
   }
   
   if(strlen(string)>40)
      return 1;
   
   return 2;
}
void HugeInteger::output()
{
   int i = 0;
   for( ; a[i] == '0'; i++ )
      a[i]=' ';
      
   int m=0;
   for(int j=i; a[j]!='\0' ; j++,m++ )
            a[m]=a[j];
            
   a[m]='\0';
   
   cout<<"1st -> "<<a<<endl;
   
   int k = 0;
   for( ; b[k] == '0'; k++ )
      b[k]=' ';
      
   int n=0;
   for(int l=k; b[l]!='\0' ; l++,n++ )
            b[n]=b[l];
            
   b[n]='\0';
   cout<<"2nd -> "<<b<<endl;
   
   convertToInteger();
   for (int i=0;i<strlen(a);i++)
      cout<<array1[i];
   cout<<endl;
   for (int i=0;i<strlen(b);i++)
      cout<<array2[i];
   cout<<endl;
   
   subtract();
}
void HugeInteger::convertToInteger()
{
   array1 = new int(strlen(a));
   for (int i=0;i<strlen(a);i++)
      switch (a[i])
      {
         case '0':array1[i]=0;break;
         case '1':array1[i]=1;break;
         case '2':array1[i]=2;break;
         case '3':array1[i]=3;break;
         case '4':array1[i]=4;break;
         case '5':array1[i]=5;break;
         case '6':array1[i]=6;break;
         case '7':array1[i]=7;break;
         case '8':array1[i]=8;break;
         case '9':array1[i]=9;break;
      }
   
   array2 = new int(strlen(b));
   for  (int i=0;i<strlen(b);i++)
      switch (b[i])
      {
         case '0':array2[i]=0;break;
         case '1':array2[i]=1;break;
         case '2':array2[i]=2;break;
         case '3':array2[i]=3;break;
         case '4':array2[i]=4;break;
         case '5':array2[i]=5;break;
         case '6':array2[i]=6;break;
         case '7':array2[i]=7;break;
         case '8':array2[i]=8;break;
         case '9':array2[i]=9;break;
      }
}
void HugeInteger::subtract()
{
   if(strlen(a)>=strlen(b))
   {
      int i = strlen(a)-1;
      
      for(int j = strlen(b)-1; i>=0 && j>=0 ; i--, j--  )
      { 
         result[i]=array1[i]-array2[j];
      }
      
      for(; i>=0 ; i-- )
         result[i]=array1[i];
   }
   
   [B]for (int i=0;i<strlen(a);i++)
      cout<<result[i];                       //How can i improvw  this ?
[/B]}

There is always some errors here; how can i improve it?

Member Avatar for iamthwee

Oh and stop spamming the forum with duplicate posts. It will just be ignored.

ConvertToInteger can be reduced to this:

void HugeInteger::convertToInteger()
{
   array1 = new int(strlen(a));
   for (int i=0;i<strlen(a);i++)
      array1[i] = a[i] - '0';
   
   array2 = new int(strlen(b));
   for  (int i=0;i<strlen(b);i++)
      array2[i] = b[i] - '0';
}
void HugeInteger::convertToInteger()
{
   array1 = new int(strlen(a));
   for (int i=0;i<strlen(a);i++)
      array1[i] = a[i] - '0';

   array2 = new int(strlen(b));
   for  (int i=0;i<strlen(b);i++)
      array2[i] = b[i] - '0';
}
Member Avatar for iamthwee

Was there a question?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.