Hey i have a code where i want to add/subtracrt 2 big integer. But i don't know how to do the carry thing.
Does anybody has an idea?

Here is my Code:

#include <iostream.h>
#include <string.h>

#define SIZE 40

class Huge
 {

  public:
  
   Huge();
   
   void menu();
  
   void rules();
  
   void intadd();
   
   void intsub();
   
   void input();
   
   void output();
   
   int getint(char);
   
  private:
  
  char x[SIZE];
  char y[SIZE];
  char newX[SIZE];
  char newY[SIZE];
  char val[SIZE];
  
 };
Huge::Huge()
 {
  
  for(int i = 0; i < SIZE; i++)
   {
   
    x[i] = '0';
    y[i] = '0';
    val[i]='0';
   }
  x[SIZE] = y[SIZE] = '\0';
 }
    
void Huge:: menu()
 {

  int Choice;
  
 while(Choice != 2)
   {
   
    cout<<endl;
    
    cout<<" Press 0 for adding two numbers. "<<endl;
    
    cout<<endl; 
    
    cout<<" Press 1 for subtracting two numbers. "<<endl;
    
    cout<<endl; 
    
    cout<<" Press 2 for exit the Program. "<<endl;
    
    cin>>Choice;
    
  switch (Choice)
   { 
   
    case 0:
     rules();
     intadd();
     break;
    
    case 1:
     rules();
     intsub();
     break;
   }
  }
 }
int Huge::getint(char a)
 {

   return((int)a-48);

 }
void Huge:: input()
 {
  cout<<endl;
  
  cout<<" Enter Your first array. "<<endl;

  cout<<endl;

  cin>>x;

  cout<<" Enter Your second array. "<<endl;

  cout<<endl;

  cin>>y;

 }
void Huge:: rules()
 {

  cout<<" Put an huge integer in but it has to be under 40 didgits."<<endl;
  
  cout<<endl;
  
 }

void Huge::intadd()
 {
 
  int a,b;
  
  int c;
  
  int s,w;
  
  int d=0;

  
  input();

   
   

  s=strlen(x);
  w=strlen(y);

 int i;

 for(i=s-1;i>=0;i--)
 { 
  newX[d]=x[i];
  d++;
 }
 newX[s] = '\0';
 d=0;
 for (i=w-1;i>=0;i--)
 {
  newY[d]=y[i];
  
  d++;
 }
 newY[w] = '\0'; 
 
 for(i=0;i<strlen(newX);i++)
 {
  
  a=getint(newX[i]);
  b=getint(newY[i]);
   
  c=a+b;
   
  val[i]=c+48;
  
 
 }
  
  
  
 
 for(i=s-1;i>=0;i--)
  cout<<val[i];
 cout<<endl;
}
void Huge::intsub()
 { 
  int a,b;
  
  int c;
  
  int s,w;
  
  int d=0;

  
  input();

   
   

  s=strlen(x);
  w=strlen(y);

 int i;

 for(i=s-1;i>=0;i--)
 { 
  newX[d]=x[i];
  d++;
 }
 newX[s] = '\0';
 d=0;
 for (i=w-1;i>=0;i--)
 {
  newY[d]=y[i];
  
  d++;
 }
 newY[w] = '\0'; 
 
 for(i=0;i<strlen(newX);i++)
 {
  
  a=getint(newX[i]);
  b=getint(newY[i]);
   
  c=a-b;
  
  val[i]=c+48;
  
 
 }
  
  
  
 
 for(i=s-1;i>=0;i--)
  cout<<val[i];
 cout<<endl;
 }
int main()
 {

  Huge h;

  h.menu();

  return 0;

 }

Recommended Answers

All 7 Replies

Please repost with code tags and formatting.

[code=cplusplus] // paste code here

[/code]

That's a lot of code to wade through for a fairly vague question. Can you be more specific on what the problem is and pinpoint where to look in the code?

void Huge::intadd()
{	
	int a,b;
	
	int s,w;
	
	int d=0;
	int i;
	int Max;

	int carryBit=0;
	
	input();

	s=strlen(x);
	w=strlen(y);
	//find largest array to use this length
	Max = s;
	if(s<w)
		Max = w;
	
	for(i=s-1;i>=0;i--)
	{	
		newX[d]=x[i];
		d++;
	}
	newX[s] = '\0';
	d=0;
	for (i=w-1;i>=0;i--)
	{
		newY[d]=y[i];		
		d++;
	}
	newY[w] = '\0';	
	
	//fill with 0's where needed to make arrays same length
	if(s >w)
	{
		for(int i= w; i<s; i++)
			newY[i] = '0';
		newY[s] = '\0';
	}
	if(w >s)
	{
		for(int i= s; i<w; i++)
			newX[i] = '0';
		newX[w] = '\0';
	}
	
	for(i=0;i<Max;i++)
	{
		
		a=getint(newX[i]);
		b=getint(newY[i]);
		
		if(a+b+carryBit>9)
		{
			val[i]=(a+b+carryBit)%10+48;
			carryBit=1;
		}
		else
		{
			val[i]=(a+b+carryBit)+48;
			carryBit=0;		
		}
	}
	if(carryBit ==1)
		val[Max] = 49;
	
	for(i=s-1;i>=0;i--)
		cout<<val[i];
	cout<<endl;

That is my carryBit i just made... and it ist working.
The only thing i need to do is in the intsub function when i've 9 - 10 i get -1 that it is borrowing a number from the next number...

Do you intend to allow negative numbers? If not then somehow you have to identify which input is larger, x or y. (Padding the smaller one with zeros if the they don't have the same number of digits is useful, just like with intadd().) The larger ultimately determines the value of a and the smaller determines the value of b (if x equals y then it doesn't matter). Then once you have a and b you need to determine if a < b to determine if you have to carry from newx or not.

//determine larger of x and y to avoid negative numbers
determine length of x
determine length of y
if x is identical to y then result is zero
if x longer than y then larger is x and smaller is y
   pad y as needed
if x is shorter than y then larger is y and smaller is x
   pad x as needed
if length of x and y are the same then
  loop through x and y char by char
     if x[i] larger than y[i] then larger is x and smaller is y
        break out of loop 
     if x[i] is smaller y[i] then larger is y and smaller is x
         break out of loop

//then subtract smaller from larger
carry starts as zero
loop through larger and smaller
    a  is larger[i] converted to int
    b is smaller[i] converted to int

    if (a + carry) < b
      val[i] is a + carry - b + 10 + 48
      carry is -1
   else
      val[i] is a + carry - b + 48
      carry is 0

Then find last nonzero element of val.

Then reverse val using elements zero to element which has the last non zero value to get result with traditional left to right representation of numbers with most significant digit on left.

Null terminate val or result as desired.

if x longer than y then larger is x and smaller is y
pad y as needed
if x is shorter than y then larger is y and smaller is x
pad x as needed

hmm i don;t really understand how u mean that... first i allready determind them with this code right? that was what u meant? I determind them after the SIZE which is 40 didgets.

class Huge
	{
		public:
			Huge();	
			void menu();
			void rules();
			void intadd();
			void intsub();
			void input();
			void output();		
			int getint(char);	
		private:
		char x[SIZE];
		char y[SIZE];
		char newX[SIZE];
		char newY[SIZE];
		char val[SIZE];
	};

then what do u mean with the shorter/longer thing for what do u need that? should it be like that?

if(x[SIZE]<y[SIZE])
		{

i am sry i am really new to programming so my knowledge is based on simple groundbasics...

thanks a lot!

Okay, don't try doing this with input that is 20 digits long. Start with input like this:

char x[5] = "1";
char y[5] = "100";

Which is shorter, x or y? If you do x - y you will get a negative number, but if you do y - x you will get a positive number. So if you don't want to allow negative numbers then you can't have x - y. So how do you know you will get a negative number? Welll, if x is shorter than y then x - y has to be negative. What if x and y are of the same length, say:

char x[3] = 987 and char [y] = 984?

You can't use length to decide which is larger. You have to do a char by char evalulation to see which is larger.

Then, if you want a loop to subtract you could pad the shorter input so it has the same number of digits as the longer.
x = "1";
y = "100"
reversedX = "1";
reversedY = "001"
paddedReversedX = "100";
paddedReversedY = "001";
paddedReversedAnswer = paddedReversedY - paddedReversedX = "990";

Last non zero index in paddedReversedAnswer is 1; so reverse paddedReversedAnswer by reversing elements from 0 to less than or equal to 1 and adding the null terminal char as needed.

ok i don't get the reversed one but i get the last one!

so now i have also a flip function to get the big number on the top.

but it is not working i allways get out 9 -.-

#include <iostream.h>
#include <string.h>
#define SIZE 40

class Huge
	{
		public:
				Huge();
			
			void menu();
		
			void rules();
		
			void intadd();
			
			void intsub();
			
			void input();
			
			void output();
			
			int getint(char);
			
			bool isgreaterthan();
			
		private:
		
		char x[SIZE];
		char y[SIZE];
		char newX[SIZE];
		char newY[SIZE];
		char val[SIZE];
	};
Huge::Huge()
	{
		
	for(int i = 0; i < SIZE; i++)
			{
			
				x[i] = '0';
				y[i] = '0';
				val[i]='0';
			}
		x[SIZE] = y[SIZE] = '\0';
	}
				
void Huge:: menu()
	{

		int Choice;
		
	while(Choice != 2)
			{	
				cout<<endl;
				
				cout<<" Press 0 for adding two numbers. "<<endl;
				
				cout<<endl; 
				
				cout<<" Press 1 for subtracting two numbers. "<<endl;
				cout<<endl; 
				
				cout<<" Press 2 for exit the Program. "<<endl;
				
				cin>>Choice;
				
		switch (Choice)
			{ 
				case 0:
					rules();
					intadd();
					break;
				case 1:
					rules();
					intsub();
					break;
			}
		}
	}
int Huge::getint(char a)
	{
			return((int)a-48);
	}
void Huge:: input()
	{
		cout<<endl;
		
		cout<<" Enter Your first array. "<<endl;

		cout<<endl;

		cin>>x;

		cout<<" Enter Your second array. "<<endl;

		cout<<endl;

		cin>>y;

	}
void Huge:: rules()
	{

		cout<<" Put an huge integer in but it has to be under 40 didgits."<<endl;
		
		cout<<endl;
		
	}

void Huge::intadd()
{
	
	char a,b;
	
	int s,w;
	
	int d=0;
	
	int i;

	int carryBit=0;
	
	input();
	
	int Max;
	
	s=strlen(x);
	w=strlen(y);
	//find largest array to use this length
	Max = s;
	if(s<w)
		Max = w;
	
	for(i=s-1;i>=0;i--)
	{	
		newX[d]=x[i];
		d++;
	}
	newX[s] = '\0';
	d=0;
	for (i=w-1;i>=0;i--)
	{
		newY[d]=y[i];
		
		d++;
	}
	newY[w] = '\0';	
	
	//fill with 0's where needed to make arrays same length
	if(s >w)
	{
		for(int i= w; i<s; i++)
			newY[i] = '0';
		newY[s] = '\0';
	}
	if(w >s)
	{
		for(int i= s; i<w; i++)
			newX[i] = '0';
		newX[w] = '\0';
	}
			
		a=getint(newX[i]);
		b=getint(newY[i]);

	for(i=0;i<Max;i++)
	{

		if(a+b+carryBit>9)
		{
			val[i]=(a+b+carryBit)%10+48;
			carryBit=1;
		}
		else
		{
			val[i]=(a+b+carryBit)+48;
			carryBit=0;
		
		}
	
	
	}
	if(carryBit ==1)
		val[Max] = 49;
		
		
		
	
	for(i=s-1;i>=0;i--)
		cout<<val[i];
	cout<<endl;
}
void Huge::intsub()
	{	
	
		int newB=0;
		int newA=0;
	
		int Max;
		
		char a,b;
		
		int s,w;
		
		int d=0;
		
		int i;

		int borrowBit=0;
		
		input();
		
		if(!isgreaterthan())
		{
			
			
			a=newA;
			b=newB;
			a=newB;
			b=newA;
		}	
		s=strlen(x);
		w=strlen(y);

	Max = s;
	if(s<w)
		Max = w;
	
	for(i=s-1;i>=0;i--)
	{	
		newX[d]=x[i];
		d++;
	}
	newX[s] = '\0';
	d=0;
	for (i=w-1;i>=0;i--)
	{
		newY[d]=y[i];
		
		d++;
	}
	newY[w] = '\0';	
	
	for(i=0;i<Max;i++)
	{
		
		a=getint(newX[i]);
		b=getint(newY[i]);
		
		if(a==b)
		{
			a=0;
			b=0;
		}
		
	if ((a + borrowBit) < b)
    {
	      val[i]=a + borrowBit - b + 10 + 48;
	      borrowBit--;
    }
   else
    {  
	      val[i]=a + borrowBit - b + 48;
	      borrowBit=0;
	}
		cout<<val[i]<<endl;
	}
	cout<<endl;
	}
bool Huge::isgreaterthan()//flipping the big number with the small number that the big one is on the top
{
	if(strlen(y)>strlen(x))
		
		return false;
		
	else
	{
		if(strlen(x)>strlen(y))
			
			return true;
		
		else
		{
			if(strcmp(x,y))
			
			return true;
			
		}
	}
	
	return false;
	
}
int main()
	{

		Huge h;

		h.menu();

		return 0;

	}
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.