Skip to content

Instantly share code, notes, and snippets.

@moreirayokoyama
Created March 11, 2014 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moreirayokoyama/9484549 to your computer and use it in GitHub Desktop.
Save moreirayokoyama/9484549 to your computer and use it in GitHub Desktop.
The objective of the exercise was to accept two operands with any length (not restricted to any datatype size limitation) and perform a sum operation with both.
#include <stdio.h>
#include <stdlib.h>
int digit_to_int(char d)
{
char str[2];
str[0] = d;
str[1] = '\0';
return (int) strtol(str, NULL, 10);
}
typedef struct _digit
{
int value;
struct _digit *next;
} digit;
typedef struct
{
digit *first;
digit *last;
} operand;
operand* newOperand()
{
operand *o = malloc(sizeof(operand));
o->first = NULL;
return o;
}
void insertDigitToOperand(operand *o, int c)
{
digit *d = malloc(sizeof(digit));
d->value = c;
d->next = NULL;
if(o->first == NULL)
o->first = d;
else
o->last->next = d;
o->last = d;
}
operand* getOperand()
{
printf("Enter the value for the operand: ");
operand *o = newOperand();
char c;
while((c=getchar())!='\n')
{
if(c < '0' || c > '9')
{
printf("\n\nThe input was not a valid number.\n");
return NULL;
}
insertDigitToOperand(o, digit_to_int(c));
}
return o;
}
void moveToNextOperandDigit(operand* o)
{
if(o->first == NULL)
return;
digit *d = o->first;
o->first = d->next;
if(o->first == NULL)
o->last = NULL;
free(d);
}
void clearOperand(operand* o)
{
while(o->first != NULL)
moveToNextOperandDigit(o);
free(o);
}
typedef struct
{
digit *top;
int carry;
} sum;
sum* newSum()
{
sum *s = malloc(sizeof(sum));
s->top = NULL;
s->carry = 0;
return s;
}
void insertDigitToSum(sum *s, int i)
{
digit *d = malloc(sizeof(digit));
d->value = i;
d->next = s->top;
s->top = d;
}
sum* getSum(operand *o1, operand *o2)
{
sum *s = newSum();
if(o1->first == NULL && o2->first == NULL)
{
insertDigitToSum(s, 0);
return s;
}
while(o1->first != NULL || o2->first != NULL)
{
int digitSum = 0;
if(o1->first == NULL)
digitSum = o2->first->value;
else if(o2->first == NULL)
digitSum = o1->first->value;
else
digitSum = o1->first->value + o2->first->value;
digitSum += s->carry;
int digitValue = digitSum % 10;
int carryValue = digitSum / 10;
insertDigitToSum(s, digitValue);
s->carry = carryValue;
moveToNextOperandDigit(o1);
moveToNextOperandDigit(o2);
}
if(s->carry > 0)
{
insertDigitToSum(s, s->carry);
s->carry = 0;
}
return s;
}
void moveToNextSumDigit(sum* s)
{
if(s->top == NULL)
return;
digit *d = s->top;
s->top = d->next;
free(d);
}
void clearSum(sum* s)
{
while(s->top != NULL)
moveToNextSumDigit(s);
free(s);
}
void showSum(sum* s)
{
while(s->top != NULL)
{
printf("%d", s->top->value);
moveToNextSumDigit(s);
}
}
int main()
{
operand *operand1 = getOperand();
operand *operand2 = getOperand();
sum *s = getSum(operand1, operand2);
showSum(s);
clearOperand(operand1);
clearOperand(operand2);
clearSum(s);
}
@moreirayokoyama
Copy link
Author

There have to be a better way for doing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment