====Welcome to Bignum!==== So the semester's winding down and Bignum is done. I don't think my solution was very intuitive; the two numbers are character arrays. There program works however and was a brilliant testing of pointer and array manipulation with a small amount of struct fun I threw in at the end for practice. The struct does nothing(the program works without it), but served as a quaint tutorial for what's to come later. Overall, I'm pleased with the result. bignum location is @ /home/asowers/src/cprog/new_bignum BONUS: check out option 5 ====Code==== bignum.c #include #include #include #include "add.h" #include "multiply.h" #include "sub.h" #include "divide.h" struct { char *first; char *second; int length; int count; int div; }n; int main() { int select=1; printf("Welcome to BigNum!\n"); printf("select a length for your array size: "); scanf("%u", &n.length); n.length = n.length-1; n.first = (char*)malloc(sizeof(char)*n.length); printf("\nFirst number: "); fgetc(stdin); for(n.count = n.length; n.count >= 0; -- n.count) { n.first[n.count] = fgetc(stdin); n.first[n.count] = n.first[n.count] - 48; } n.second = (char*)malloc(sizeof(char)*n.length); printf("\nSecond number: "); fgetc(stdin); for(n.count = n.length; n.count >= 0; -- n.count) { n.second[n.count] = fgetc(stdin); n.second[n.count] = n.second[n.count] - 48; } while(select != 0 ){ printf("_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n"); printf("|Please choose an Option(1-5): |\n"); printf("|Option 1: Add |\n"); printf("|Option 2: Subtract |\n"); printf("|Option 3: Multiply |\n"); printf("|Option 4: Divide |\n"); printf("|Option 5: Exit |\n"); printf("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n"); scanf("%d", &select); if (select == 1){ printf("\nYou selected addition.\nThe answer is: "); n.first = add(n.first, n.second, n.length); for (n.length; n.length>=0; --n.length) { printf("%hhu", n.first[n.length]); } printf("\n"); break;} else if (select == 2){ printf("\nYou selected subtraction.\nThe answer is: "); n.first = sub(n.first, n.second, n.length); for (n.length; n.length>=0; --n.length) { printf("%hhu", n.first[n.length]); } printf("\n"); break;} else if (select == 3){ printf("\nYou selected multiplication.\nThe answer is: "); multiply(n.first, n.second, n.length); for (n.length; n.length>=0; --n.length) { printf("%hhu", n.first[n.length]); } printf("\n"); break;} else if (select == 4){ printf("\nYou selected division.\nThe answer is: "); divide(n.first, n.second, n.length, &n.div); printf("%hhu", n.div); printf("\n"); break;} else if (select == 5){ printf("\nYou selected to exit.\nThe answer is: Tom's mom"); break;} else{ printf("Incorrect in.\n"); } } printf("\nGoodbye!\n"); return(0); } add.h #ifndef _ADDITION_H #define _ADDITION_H char * add(char * first, char * second, int length) { int count = 0; char carry = 0; for(count; count <= length; ++count) { int holder = (first[count] + second[count])+carry; if (holder > 9) { first[count] = holder-10; carry=1; } else { first[count] = holder; carry=0; } } return first; } #endif sub.h #ifndef _SUB_H #define _SUB_H char * sub(char *first, char *second, int length) { char count = 0; for(count; count <= length; ++count) { if (first[count] < second[count]) { first[count+1] = first[count+1] - 1; first[count] = first[count] + 10; } first[count] = first[count] - second[count]; } return first; } #endif multiply.h #ifndef _MULTIPLY_H #define _MULTIPLY_H #include #include "sub.h" void multiply(char * first, char * second, int length) { int count = 0; int holder = 0; char *third; third = (char*)malloc(sizeof(char)*length); for (count; count <= length; ++count) { third[count] = first[count]; } for(count=0; count <= length; ++count) { holder = (second[count]*pow(10, count)); for(holder; holder > 0; --holder) { first = add(first, third, length); } } first = sub(first, third, length); } #endif divide.h #ifndef _DIVIDE_H #define _DIVIDE_H void divide(char * first, char * second, int length, int *div) { int count = 0; char *fourth; fourth = (char*)malloc(sizeof(char)*length); for (count; count <= length; ++count) { fourth[count] = first[count]; } for(count=length; count>=0; --count) { while(first[count] > 0) { ++*div; first = sub(first, second, length); } } } #endif compiling and implementation lab46:~/src/cprog/new_bignum$ gcc bignum.c -o bignum -lm lab46:~/src/cprog/new_bignum$ ./bignum Welcome to BigNum! select a length for your array size: 4 First number: 1234 Second number: 0100 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ |Please choose an Option(1-5): | |Option 1: Add | |Option 2: Subtract | |Option 3: Multiply | |Option 4: Divide | |Option 5: Exit | -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- 1 You selected addition. The answer is: 1334 Goodbye! lab46:~/src/cprog/new_bignum$ ./bignum Welcome to BigNum! select a length for your array size: 5 First number: 09999 Second number: 00001 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ |Please choose an Option(1-5): | |Option 1: Add | |Option 2: Subtract | |Option 3: Multiply | |Option 4: Divide | |Option 5: Exit | -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- 1 You selected addition. The answer is: 10000 Goodbye! Welcome to BigNum! select a length for your array size: 5 First number: 00400 Second number: 00050 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ |Please choose an Option(1-5): | |Option 1: Add | |Option 2: Subtract | |Option 3: Multiply | |Option 4: Divide | |Option 5: Exit | -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- 3 You selected multiplication. The answer is: 20000 Goodbye! lab46:~/src/cprog/new_bignum$ ./bignum Welcome to BigNum! select a length for your array size: 4 First number: 5250 Second number: 0050 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ |Please choose an Option(1-5): | |Option 1: Add | |Option 2: Subtract | |Option 3: Multiply | |Option 4: Divide | |Option 5: Exit | -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- 4 You selected division. The answer is: 105 Goodbye! lab46:~/src/cprog/new_bignum$ ./bignum Welcome to BigNum! select a length for your array size: 1 First number: 1 Second number: 1 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ |Please choose an Option(1-5): | |Option 1: Add | |Option 2: Subtract | |Option 3: Multiply | |Option 4: Divide | |Option 5: Exit | -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- 5 You selected to exit. The answer is: Tom's mom Goodbye! lab46:~/src/cprog/new_bignum$