954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Huge integers subtraction?(with programming code)easier to understand

#ifndef HUGEINTEGER_H
#define HUGEINTEGER_H
class HugeInteger
{
public:
       HugeInteger();
       
       void input();
       int setNumber(char []);
       
       void output();
       void convertToInteger();
       void subtract();
       
       bool isEqualTo();
       bool isNotEqualTo();
       bool isGreaterThan();
       bool isLessThan();
       bool isGreaterThanOrEqualTo();
       bool isLessThanOrEqualTo();
       bool isZero();
       
       ~HugeInteger();
private:
        char a[41];
        char b[41];
        int *array1;
        int *array2;
        int result[41];
};
#endif
#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];
   }
   
   for (int i=0;i<strlen(a);i++)
      cout<<result[i];
}
bool HugeInteger::isEqualTo()
{
   cout<<"The 1st is equal to the 2nd: ";
   if(strcmp(a,b)==0)
      return true;
   else
      return false;
}
bool HugeInteger::isNotEqualTo()
{
   cout<<"The 1st is not equal to the 2nd: ";
   if(strcmp(a,b)!=0)
      return true;
   else
      return false;
}
bool HugeInteger::isGreaterThan()
{
   cout<<"The 1st is greater than the 2nd: ";
   if(strcmp(a,b)>0)
      return true;
   else
      return false;
}
bool HugeInteger::isLessThan()
{
   cout<<"The 1st is less than the 2nd: ";
   if(strcmp(a,b)<0)
      return true;
   else
      return false;
}
bool HugeInteger::isGreaterThanOrEqualTo()
{
   cout<<"The 1st is greater than or equal to the 2nd: ";
   if(strcmp(a,b)>=0)
      return true;
   else
      return false;
}
bool HugeInteger::isLessThanOrEqualTo()
{
   cout<<"The 1st is less than or equal to the 2nd: ";
   if(strcmp(a,b)<=0)
      return true;
   else
      return false;
}
bool HugeInteger::isZero()
{
   
}
HugeInteger::~HugeInteger(){}
#include<iostream>
using namespace std;
#include "HugeInteger.h"
int main()
{
   HugeInteger numbers;
   
   system("pause");
   return 0;
}
wujianwei
Newbie Poster
18 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

you can help me by copying the code to compile... and please tell me what goes wrong? thanx...

wujianwei
Newbie Poster
18 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Why can't you figure it out yourself.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
you can help me by copying the code to compile... and please tell me what goes wrong? thanx...


OK. It doesn't compile. That's the first thing that's wrong.

What's going wrong is obvious. You want someone else to take your code and blindly figure out
1) what it's supposed to do
2) what it really does
3) fix what they find wrong
4) repost it
so you can hand it in an get an A on your project.

Please tellus what's wrong with this.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You