Thread: ~| Big Number Class |~

  1. #1
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104

    Unhappy ~| Big Number Class |~

    Can anyone tell me or help me to build a Class that will take arbitarily long integers and do the arithmatic operation on them ?
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Its pretty easy to do. Use strings or character arrays and overload the arithmetic operators.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The basics:
    - Store blocks of the integer in a primitive type (probably unsigned char).
    - Overload the arithmetic operators (make sure to store any values that carry over).
    - Include a member for the sign (bool possibly, or possibly a specific bit in the data).

    There are libraries out there that have this capability (try Google). It would help if you showed what you have done so far.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46
    you'll probably need to reference the monumental
    "The Art of Computer Programming"
    Volume 2 - Seminumerical Algorithms
    in order to optimize the code.
    I've done it only in C
    to boldy code where...

  5. #5
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    I am looking for a class................
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    We are telling you what needs to be included in the class. If you are looking for someone to give you a prewritten class that does everything you want, it ain't gonna happen.

    Show us what you have written so far and we will help out though.

  7. #7
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    Originally posted by PJYelton
    We are telling you what needs to be included in the class. If you are looking for someone to give you a prewritten class that does everything you want, it ain't gonna happen.

    Show us what you have written so far and we will help out though.
    Why this not gonna heppen I don't know..........

    I have searched in the web got few........outline and code segment.................

    And I've not got that Knuth's book........not available here

    Ok! You can see my just (+) operatinos code but after doing this and as it's not working as I thought ((

    Code:
    #include<iostream.h>
    #include<string.h>
    #include<stdlib.h>
    #include<conio.h>
    
    char *sum(char n1[1000],char n2[1000]);
    
    int main()
    {
    
    	char num[120]={0};
    	char ans[120]={0};
    
    	while(cin >> num)
    	{
    		if(atol(num)==0)break;
    		strcpy(ans,sum(ans,num));
    		num[0]=NULL;
    	}
    	cout << ans << endl;getch();
    	return 0;
    }
    
    char *rev(char str[1000])
    {
    	int l=strlen(str);
    	char tmp[1000]={0};
    	for(int i=0;i<l;i++)
    		tmp[i]=str[(l-1)-i];
    	strcpy(str,tmp);
    	return str;
    }
    
    char *sum(char n1[1000],char n2[1000])
    {
    	int l1=strlen(n1);
    	int l2=strlen(n2);
    	if(l1>l2)
    	{
    		char tmp[1000];
    		strcpy(tmp,n1);
    		strcpy(n1,n2);
    		strcpy(n2,tmp);
    		int tl;
    		tl=l1;
    		l1=l2;
    		l2=tl;
    }
    
    	char cry='0';
    	char ch[1000]={0};
    
    	int lsm=(l1>l2)?l2:l1;
    
    	for(int i=1;i<=lsm;i++)
    	{
    		if(cry=='1')
    			ch[i-1]=(char)(*(n1+l1-i)+*(n2+l2-i)-'0'+'1'-'0');
    		else
    			ch[i-1]=(char)(*(n1+l1-i)+*(n2+l2-i)-'0');
    		if(ch[i-1]>'9')
    		{
    			cry='1';
    			(ch[i-1]-=10);
    		}
    		else
    		{
    			cry='0';
    		}
    	}
    
    	int ldf=(l2-lsm);
    	char chh[1000]={0};
    	for(int j=1;j<=ldf;j++)
    	{
    		if(cry=='1')
    			chh[j-1]=(char)(*(n2+ldf-j)+'1'-'0');
    		else
    			chh[j-1]=(char)(*(n2+ldf-j));
    		if(chh[j-1]>'9')
    		{
    			cry='1';
    			(chh[j-1]-=10);
    		}
    		else
    		{
    			cry='0';
    		}
    	}
    
    	static char ans[1000]={0};
    	if(cry=='1')
    	{
    		strcpy(ans,strcat("1",strcat(rev(chh),rev(ch))));
    		return (ans);
    	}
    	else
    	{
    		strcpy(ans,strcat(rev(chh),rev(ch)));
    		return (ans);
    	}
    }
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    In this particular case, I would use unsigned char instead of signed (normal) char. Also, I wouldn't worry about string operations with the character arrays, but instead use the chars simply as 8-bit ints (each one capable of storing a maximum value of 256, not just a single digit). The result would be to treat your unsigned char* not as a string, but as you would an int* (for example).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    Originally posted by Zach L.
    In this particular case, I would use unsigned char instead of signed (normal) char. Also, I wouldn't worry about string operations with the character arrays, but instead use the chars simply as 8-bit ints (each one capable of storing a maximum value of 256, not just a single digit). The result would be to treat your unsigned char* not as a string, but as you would an int* (for example).

    sounds great...............!

    But if I can made a class or get one this will be very easy to use like Java's BigNumber class...........
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    There are links to several libraries here.

    Ahh... the power of Google.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    Thanks! But these are all for Linux........

    Ok! If you get links like this plz inform here
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    They're most likely standard C/C++. If so, they'll work on any platform with a standard compliant compiler. GNU MP looks like a decent one, and it says its portable.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM