Huge integers subtraction?(with programming code)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 18
Reputation: wujianwei is an unknown quantity at this point 
Solved Threads: 0
wujianwei wujianwei is offline Offline
Newbie Poster

Huge integers subtraction?(with programming code)

 
0
  #1
Apr 22nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Huge integers subtraction?(with programming code)

 
0
  #2
Apr 22nd, 2007
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Huge integers subtraction?(with programming code)

 
0
  #3
Apr 22nd, 2007
Oh and stop spamming the forum with duplicate posts. It will just be ignored.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Huge integers subtraction?(with programming code)

 
0
  #4
Apr 22nd, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 18
Reputation: wujianwei is an unknown quantity at this point 
Solved Threads: 0
wujianwei wujianwei is offline Offline
Newbie Poster

Re: Huge integers subtraction?(with programming code)

 
0
  #5
Apr 25th, 2007
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Huge integers subtraction?(with programming code)

 
0
  #6
Apr 25th, 2007
Was there a question?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC