944,200 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1231
  • C++ RSS
Apr 22nd, 2007
0

Huge integers subtraction?(with programming code)

Expand Post »
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];
   }
   
   for (int i=0;i<strlen(a);i++)
      cout<<result[i];                       //How can i improvw  this ?
}
There is always some errors here; how can i improve it?
Last edited by ~s.o.s~; Apr 22nd, 2007 at 7:19 am. Reason: Added code tags, learn to use them.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wujianwei is offline Offline
18 posts
since Mar 2007
Apr 22nd, 2007
0

Re: Huge integers subtraction?(with programming code)

Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 22nd, 2007
0

Re: Huge integers subtraction?(with programming code)

Oh and stop spamming the forum with duplicate posts. It will just be ignored.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 22nd, 2007
0

Re: Huge integers subtraction?(with programming code)

ConvertToInteger can be reduced to this:
  1. void HugeInteger::convertToInteger()
  2. {
  3. array1 = new int(strlen(a));
  4. for (int i=0;i<strlen(a);i++)
  5. array1[i] = a[i] - '0';
  6.  
  7. array2 = new int(strlen(b));
  8. for (int i=0;i<strlen(b);i++)
  9. array2[i] = b[i] - '0';
  10. }
Last edited by Ancient Dragon; Apr 22nd, 2007 at 8:37 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 25th, 2007
0

Re: Huge integers subtraction?(with programming code)

  1. <LI class=li1>void HugeInteger::convertToInteger() <LI class=li1>{ <LI class=li1> array1 = new int(strlen(a)); <LI class=li1> for (int i=0;i<strlen(a);i++) <LI class=li2> array1[i] = a[i] - '0'; <LI class=li1> <LI class=li1> array2 = new int(strlen(b)); <LI class=li1> for (int i=0;i<strlen(b);i++) <LI class=li1> array2[i] = b[i] - '0';
  2. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wujianwei is offline Offline
18 posts
since Mar 2007
Apr 25th, 2007
0

Re: Huge integers subtraction?(with programming code)

Was there a question?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ in subversion
Next Thread in C++ Forum Timeline: Please Help With This Array!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC