I am having problems to figure out how to do arithmetic operations on positive integers represented as strings (addition, subtraction, multiplication, incrementing and decrementing).
I did the addition as following:

#include<stdio.h>
#define MAX 100000

typedef struct
{
    int arr[MAX];
}NUMBER;

void read(NUMBER *add_num)
{
    int i,digit=0;
    char ch[101];
    scanf("%s",ch);
    while(ch[digit])
        digit++;
    for(i=0;i < MAX;i++)
    {
        digit--;
        if(digit >= 0)
            add_num->arr[i]=ch[digit]-'0';
        else
            add_num->arr[i]=0;
    }
}

void addition(NUMBER a,NUMBER b,NUMBER *add_res)
{
    int carry=0;
    int i,temp;
    for(i=0;i < MAX;i++)
    {
        temp=a.arr[i]+b.arr[i]+carry;
        add_res->arr[i]=temp % 10;
        carry=temp / 10;
    }
}

void print(NUMBER *add_num)
{
    int i;
    for(i=MAX-1;add_num->arr[i]==0;i--);
    for(;i>=0;i--)
        printf("%d",add_num->arr[i]);
}

int main()
{
    NUMBER x,y,z;
    printf("enter two positive integers: \n");
    read(&x);
    read(&y);
    printf("addition result: ");
    addition(x,y,&z);
    print(&z);
    return 0;
}

How to use the function for addition to find the product of two string numbers?
Could someone explain the algorithm for division (it seems the hardest)?

Firstly, your struct needs some work. Since long-hand addition starts from the right not the left, it would make sense to keep track of where the right most digit is in the array. To accomplish this an additional field(size) will come in handy.

Another thing to keep in mind, if the addition results in a carry over from adding the 2 leftmost digits you will need an extra digit. There's 2 basic ways around this, build the array backwards or shovel everything down one space, when needed.

Your read function has no validation of the data being input by the user. This can lead to no end of trouble.

Rather than passing the placeholders for the answer into the function, it would be more intuitive, to only pass in the items needed to create the answer and return the answer.

Subtraction is the same as adding a negative number.
Multiplication is simply adding the multiplicand to itself multiplier times.
Division will depend on whether you will stick to integer division or floating point division.

As Dani's @ tinstaafl suggests ...

firstly ...

to get a design for just simple addition (of large int's of any size) to work,

you could use some functions to read in dynamic C strings of any size, maybe something like this:

Note that file "readLIne2.h" is available here:

http://developers-heaven.net/forum/index.php/topic,2582.msg3143.html#msg3143

/* add_dynamic_Cstrings.c */

#include "readLine2.h"



/* 2 handy utilities here ... */
int takeInChr( const char* msg )
{
    char chr;
    fputs( msg, stdout ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}
int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    if( tolower( takeInChr( "More (y/n) ? " )) == 'n' ) return 0;
    /* else ... */
    return 1;
}

int is_valid( const char* str )
{
    while( *str )
    {
        if( *str < '0' || *str > '9' ) return 0;
        ++str;
    }
    return 1;
}


char* add( const char* a, unsigned len_a, const char* b, unsigned len_b, unsigned* len_c )
{
    char* c;
    int i, j;
    int overflow = 0;
    if( len_a >= len_b )
    {
        c = newMem( len_a+1 );
        c[len_a] = 0;
        for( i = len_a-1, j = len_b-1; j >= 0; --i, --j )
        {
            c[i] = a[i] + (b[j] - '0') + overflow;
            if( c[i] > '9' )
            {
                overflow = 1;
                c[i] -= 10;
            }
            else
            {
                overflow = 0;
            }
        }

        for( ; i >= 0; --i )
        {
            c[i] = a[i] + overflow;
            if( c[i] > '9' )
            {
                overflow = 1;
                c[i] -= 10;
            }
            else
            {
                overflow = 0;
            }
        }

        if( overflow )
        {
            int  k;
            char* d = newMem( len_a+2 );
            d[0] = '1';
            d[len_a+1] = 0;
            for( k = len_a; k > 0; --k )
                d[k] = c[k-1];
            free( c );
            *len_c = len_a+1;

            return d;
        }

        /* else */
        *len_c = len_a;
        return c;
    }


    return add( b, len_b, a, len_a, len_c );
}


char* takeInBigInt( const char* prompt, unsigned* len_a )
{
    char* a;
    while( 1 )
    {
        fputs( prompt, stdout ); fflush( stdout );
        a = readLine2( stdin, len_a );
        if( is_valid( a ) ) break;
        free( a );
        fputs( "Not a valid entry ... +ve numbers only accepted here.\n", stdout );
    }
    return a;
}



int main()
{
    char * a, * b, * c;
    unsigned len_a, len_b, len_c;
    do
    {
        a = takeInBigInt( "Enter 1st BIG (+ve) )integer to add: ", &len_a );
        b = takeInBigInt( "Enter 2nd BIG (+ve) )integer to add: ", &len_b );
        c = add( a, len_a, b, len_b, &len_c );

        printf( "%s + %s = %s\n", a, b, c );
        printf( "len_c = %d\n", len_c );
        free( c );
        free( b );
        free( a );
    }
    while( more() );

    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.