Please help me with my project

Discussion in 'C' started by Martin0027, May 12, 2011.

  1. Martin0027

    Martin0027 New Member

    Joined:
    May 12, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Write a program that will multiply arbitrarily large real numbers (stored in the chain). Numbers can be positive or negative and the program will accept a comma as decimal point.
    Example output:
    15321548949451231684.12654894856 x 15,658,213,548,949.5684516 =239908085351191302633721198032836,254474415727249696

    Classic calculator Is not problem for me, but this program is too hard for me.
    So anyone who know how I can do this please help. Thank you
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Store numbers as strings, then implement the maths algorithms you used to do long addition et al when you were 10 to solve them. Here's a simple example of addition and you can work out the rest (e.g. use C++ string objects instead of static buffers):
    Code:
    	char num1[5], num2[5], result[5];
    	strcpy(num1,"0123");
    	strcpy(num2,"0456");
    	int carry=result[4]=0;
    	for (int i=3; i>=0; i--)
    	{
    		int res=num1[i]-'0' + num2[i]-'0' + carry;
    		if (res>9)
    		{
    			res-=10;
    			carry=1;
    		}
    		else
    		{
    			carry=0;
    		}
    		result[i]=res+'0';
    	}
    	printf("%s+%s=%s\n",num1,num2,result);
    
    Long division is trickiest so leave that until last. By the time you've done the rest you should be able to handle it. Start with addition, then do subtraction, then multiplication. Start with integer maths and it's up to you when you start adding functionality to handle fixed or floating point.

    If you can't figure it out then do a few examples on paper to remind yourself how it was done, then do a few more while you figure out the algorithm, then you can implement that algorithm.
     
  3. Martin0027

    Martin0027 New Member

    Joined:
    May 12, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thank you so much !!!
     
  4. Martin0027

    Martin0027 New Member

    Joined:
    May 12, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    but I have another question. how I can transform classic number to string ??
     
  5. Avenger625

    Avenger625 New Member

    Joined:
    Feb 1, 2011
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    U can use sprintf().
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice