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)?