...multiplication using stacks

This is a discussion on ...multiplication using stacks within the C Programming forums, part of the General Programming Boards category; this segment is proving to be the death of me... i'm required to scan digits from an input file which ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    32

    Angry ...multiplication using stacks

    this segment is proving to be the death of me...

    i'm required to scan digits from an input file which are to long to record as int variables.

    so i record them as strings and convert them to ints before pushing them onto stacks.

    i have a function that adds the strings and reports the answer just fine. [add(num, x, y, z) where x and y represents the two stacks to add and z is the stack where the answer is to be stored.]

    my problem lies with multiplication, for instance 75 * 36. i have two temp stacks and one answer stack for multiplication. the two temp stacks print out what they're supposed to have, but when i go to add them, it mucks up. here is my multiplication function.

    Code:
    while(num[0].top != Empty && num[1].top != Empty)	{
    		digit2 = pop(num, 1);
    		while(num[0].top != Empty)	{
    			digit = pop(num, 0);
    			
    			product = (digit * digit2) + carry;
    			
    			if(product >= 10)	{
    				carry = product / 10;
    				product = product % 10;
    				push(num, product, 3);
    				//printf("push 3\t%d\n", product);
    			}
    			
    			else	{
    				carry = 0;
    				push(num, product, 3);
    				//printf("push 3\t%d\n", product);
    			}
    		}
    		zeros++;
    		
    		if(carry != 0)	{
    			push(num, carry, 3);
    			//printf("push 3\t%d\n", carry);
    			carry = 0;
    		}
    		
    		for(i = 0; i < zeros; i++)	{
    			push(num, 0, 4);
    			//printf("push 4\t0\n");
    		}
    		
    		num[0].top = t1;
    		
    		digit2 = pop(num, 1);
    		
    		while(num[0].top != Empty)	{
    			digit = pop(num, 0);
    			
    			product = (digit * digit2) + carry;
    			
    			if(product >= 10)	{
    				carry = product / 10;
    				product = product % 10;
    				push(num, product, 4);
    				//printf("push 4\t%d\n", product);
    			}
    			
    			else	{
    				carry = 0;
    				push(num, product, 4);
    				//printf("push 4\t%d\n", product);
    			}
    		}
    		if(carry != 0)	{
    			push(num, carry, 3);
    			//printf("push 4\t%d\n", carry);
    			carry = 0;
    		}
    		zeros++;
    	}
    	add(num, 3, 4, 5);
    0 is the stack for the first number to multiply and
    1 is the stack for the second number

    3 is the first temp stack for multiplication and
    4 is the second temp stack

    5 is supposed to contain the result.

    i hope i've given you enough information to point me in some sort of direction, i'll be monitering this thread so please feel free to ask absolutely any questions you have.

    thanks in advance,
    michael

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Ay caramba! There are highly-advanced methods like Karatsuba, Toom-Cook and Schonage-Strassen to multiply large numbers quickly, but will you take the trouble to implement them? In tricked-out assembly? Why don't you leave that problem to people from GMP, Apfloat etc.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me find a multiplication toy.
    By QuestionC in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-24-2007, 01:27 PM
  2. Please Help Me With This Code Of Implementing Stacks
    By raghu_equinox in forum C Programming
    Replies: 3
    Last Post: 10-19-2006, 08:22 AM
  3. Help With Stacks
    By penance in forum C Programming
    Replies: 7
    Last Post: 10-09-2005, 03:47 PM
  4. multiplication table
    By SpEkTrE in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 04:46 PM
  5. Stacks stacks stacks
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-06-2002, 03:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21