Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*Define the limit of the stacks.*/
#define MAXSTACK 100
/*Defines the structure of a stack.*/
typedef struct {
int digits[MAXSTACK];
int top;
}arraystack;
/*Pushes new item onto the stack.*/
void push(arraystack *x, int y);
/*Pops new item from the stack.*/
void pop(arraystack *x, int y);
/*Pops items from stack and prints them.*/
void popprint(arraystack *x, int y);
/*Checks to see if the stack is empty.*/
int isEmpty(arraystack *x);
/*Addition function.*/
void addition(arraystack *x, arraystack *y);
/*Multiplication function.*/
void multiplication(arraystack *x, arraystack *y);
/*Revised addition function for use with multiplication.*/
arraystack *addformult(arraystack *x, arraystack *y);
/*Structs will be global.*/
arraystack value1;
arraystack value2;
arraystack dummy;
arraystack temp1;
arraystack result;
int main(void)
{
/*Variables for file input.*/
FILE *ifp;
char filename[30];
/*Variables for loops.*/
int counter, i, j;
/*Variables for strings.*/
char num1[100];
char num2[100];
/*Variables for conversion.*/
char c1;
int digit = 0;
/*Initialize top of stacks.*/
value1.top = -1;
value2.top = -1;
dummy.top = -1;
temp1.top = -1;
result.top = -1;
/*Ask for input file.*/
printf("Please enter the filename of the file you wish to open: ");
scanf("%s", &filename);
/*Open input file.*/
ifp = fopen(filename, "r");
/*Get number of digit sets from file.*/
fscanf(ifp, "%d", &counter);
/*For the number of digits in the file:*/
for (i = 0; i < counter; i++) {
/*Read in the first number.*/
fscanf(ifp, "%s", &num1);
/*Read in the next number.*/
fscanf(ifp, "%s", &num2);
/*Convert first number into a digit and push it into the first array.*/
j = 0;
while(num1[j] != '\0') {
c1 = num1[i];
digit = c1 - '0';
push(&value1, digit);
j++;
}
/*Convert second number into a digit and push it into the second array.*/
j = 0;
while(num2[j] != '\0') {
c1 = num2[i];
digit = c1 - '0';
push(&value2, digit);
j++;
}
/*Add the two arrays.*/
addition(&value1, &value2);
/*Multiply the two arrays.*/
multiplication(&value1, &value2);
}
/*Close input file.*/
fclose(ifp);
/*End program.*/
return 0;
}
/*Pushes items onto a stack.*/
void push(arraystack *x, int y) {
/*If the stack is full:*/
if (x->top >= MAXSTACK - 1) {
/*Error.*/
printf("Stack is full.\n");
}
/*Otherwise:*/
else {
/*Stack top raises by 1 and new digit is added.*/
x->top = x->top + 1;
x->digits[x->top] = y;
}
}
/*Pops items from a stack.*/
void pop(arraystack *x, int y) {
/*If the stack is empty:*/
if (x->top < 0) {
/*Error.*/
printf("Stack is empty.\n");
}
/*Otherwise:*/
else {
/*New digit is popped and stack top lowers by 1.*/
y = x->digits[x->top];
x->top = x->top - 1;
}
}
/*Checks to see if the stack is empty.*/
int isEmpty(arraystack *x) {
return (x->top < 0);
}
/*Pops items from stack and prints them.*/
void popprint(arraystack *x, int y) {
/*Variable for loop.*/
int i;
/*If the stack is empty:*/
if (x->top < 0) {
/*Error.*/
printf("Stack is empty.\n");
}
/*Otherwise:*/
else {
/*New digit is popped and printed and stack top lowers by 1.*/
y = x->digits[x->top];
for (i = 0; i <= x->top; i++) {
printf("%d", y);
}
printf("\n");
x->top = x->top - 1;
}
}
/*Addition function.*/
void addition(arraystack *x, arraystack *y) {
/*Variable for carry.*/
int carry = 0;
/*Variables for adding.*/
int num1, num2, sum;
/*Variable for printing.*/
int digit = 0;
/*While one stack is not empty:*/
while (x->top >= 0 || y->top >= 0) {
/*If first stack is empty:*/
if (x->top < 0) {
/*First addend is 0.*/
num1 = 0;
}
/*Otherwise:*/
else {
/*Pop the first addend.*/
pop(x, num1);
}
/*If second stack is empty:*/
if (y->top < 0) {
/*Second addend is 0.*/
num2 = 0;
}
/*Otherwise:*/
else {
/*Pop the second addend.*/
pop (y, num2);
}
/*Add num1, num2, and carry to get the sum.*/
sum = num1 + num2 + carry;
/*If sum < 10:*/
if (sum < 10) {
/*Carry = 0.*/
carry = 0;
/*Sum is pushed on the result stack.*/
push(&result, sum);
}
/*Else:*/
else {
/*Unit of result is pushed on result stack.*/
sum = sum % 10;
push(&result, sum);
/*Carry = 1.*/
carry = 1;
}
}
/*If carry is 1 after loop:*/
if (carry = 1) {
/*Push carry onto stack.*/
push(&result, carry);
}
/*Call popprint and send result.*/
popprint(&result, digit);
}
/*Multiplication function.*/
void multiplication(arraystack *x, arraystack *y) {
/*Variable for carry.*/
int carry = 0;
/*Variable for deciding factor.*/
int decide = 0;
/*Variable for loop.*/
int i;
/*Variables for product.*/
int num1 = 0;
int num2 = 0;
int product;
/*Variable for printing.*/
int digit = 0;
/*While second stack is not empty:*/
while (y->top >= 0) {
/*Add the necessary zeros to first temp stack.*/
for (i = 0; i < decide; i++) {
push(&temp1, 0);
}
/*Dummy stack is equivalent to first value stack.*/
for (i = 0; i <= value1.top; i++) {
dummy.digits[i] = value1.digits [i];
}
/*Pop value from second value stack.*/
pop(y, num2);
/*For the duration of dummy stack:*/
while (dummy.top >= 0) {
/*Pop value from dummy stack.*/
pop(&dummy, num1);
/*Multipy numbers and add carry to get product.*/
product = (num1 * num2) + carry;
/*If product is less than 10:*/
if (product < 10) {
/*Carry equals zero.*/
carry = 0;
/*Product is pushed onto first temp stack.*/
push(&temp1, product);
}
/*If product is 10 or greater:*/
else {
/*Value of product % 10 is pushed onto stack.*/
product = product % 10;
push(&temp1, product);
/*Value of (product-(product%10))/10 becomes carry.*/
carry = (product-(product%10))/10;
}
}
/*If second temp stack is empty:*/
if (result.top < 0) {
/*Second temp stack is first temp stack.*/
result = temp1;
/*First temp stack is emptied.*/
temp1.top = -1;
}
/*Otherwise:*/
else {
/*Add two temp stacks together.*/
addformult(&temp1, &result);
/*First temp stack is emptied.*/
temp1.top = -1;
}
/*Add 1 to deciding factor.*/
decide = decide + 1;
}
/*Call popprint and print result.*/
popprint(&result, digit);
}
/*Revised addition function for use with multiplication.*/
arraystack *addformult(arraystack *x, arraystack *y) {
/*Variable for carry.*/
int carry = 0;
/*Variables for adding.*/
int num1, num2, sum;
/*Variable for printing.*/
int digit = 0;
/*While one stack is not empty:*/
while (x->top >= 0 || y->top >= 0) {
/*If first stack is empty:*/
if (x->top < 0) {
/*First addend is 0.*/
num1 = 0;
}
/*Otherwise:*/
else {
/*Pop the first addend.*/
pop(x, num1);
}
/*If second stack is empty:*/
if (y->top < 0) {
/*Second addend is 0.*/
num2 = 0;
}
/*Otherwise:*/
else {
/*Pop the second addend.*/
pop (y, num2);
}
/*Add num1, num2, and carry to get the sum.*/
sum = num1 + num2 + carry;
/*If sum < 10:*/
if (sum < 10) {
/*Carry = 0.*/
carry = 0;
/*Sum is pushed on the result stack.*/
push(&result, sum);
}
/*Else:*/
else {
/*Unit of result is pushed on result stack.*/
sum = sum % 10;
push (&result, sum);
/*Carry = 1.*/
carry = 1;
}
}
/*If carry is 1 after loop:*/
if (carry = 1) {
/*Push carry onto stack.*/
push (&result, carry);
}
/*Send result to multiplication.*/
return &result;
}