This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:asowers:bignum [2012/11/29 20:33] – [Code] asowers | user:asowers:bignum [2012/11/30 01:33] (current) – [Welcome to Bignum!] asowers | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====Welcome to Bignum!==== | ||
+ | |||
+ | So the semester' | ||
+ | ====Code==== | ||
+ | |||
+ | bignum.c | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | #include " | ||
+ | #include " | ||
+ | #include " | ||
+ | |||
+ | struct { | ||
+ | char *first; | ||
+ | char *second; | ||
+ | int length; | ||
+ | int count; | ||
+ | int div; | ||
+ | }n; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | |||
+ | int select=1; | ||
+ | |||
+ | printf(" | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | n.length = n.length-1; | ||
+ | n.first = (char*)malloc(sizeof(char)*n.length); | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | 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; | ||
+ | } | ||
+ | |||
+ | |||
+ | | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | 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(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | if (select == 1){ | ||
+ | printf(" | ||
+ | n.first = add(n.first, | ||
+ | for (n.length; n.length> | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | break;} | ||
+ | else if (select == 2){ | ||
+ | printf(" | ||
+ | n.first = sub(n.first, | ||
+ | for (n.length; n.length> | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | break;} | ||
+ | else if (select == 3){ | ||
+ | printf(" | ||
+ | multiply(n.first, | ||
+ | for (n.length; n.length> | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | break;} | ||
+ | else if (select == 4){ | ||
+ | printf(" | ||
+ | divide(n.first, | ||
+ | printf(" | ||
+ | printf(" | ||
+ | break;} | ||
+ | else if (select == 5){ | ||
+ | printf(" | ||
+ | break;} | ||
+ | else{ | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | printf(" | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | add.h | ||
+ | <code c> | ||
+ | |||
+ | #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 | ||
+ | <code c> | ||
+ | #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 | ||
+ | <code c> | ||
+ | #ifndef _MULTIPLY_H | ||
+ | #define _MULTIPLY_H | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | 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; | ||
+ | { | ||
+ | holder = (second[count]*pow(10, | ||
+ | for(holder; holder > 0; --holder) | ||
+ | { | ||
+ | first = add(first, third, length); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | first = sub(first, third, length); | ||
+ | |||
+ | } | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | divide.h | ||
+ | <code c> | ||
+ | #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; | ||
+ | { | ||
+ | while(first[count] > 0) | ||
+ | { | ||
+ | ++*div; | ||
+ | first = sub(first, second, length); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | compiling and implementation | ||
+ | <cli> | ||
+ | lab46: | ||
+ | lab46: | ||
+ | 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: | ||
+ | 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: | ||
+ | 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: | ||
+ | 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: | ||
+ | |||
+ | </ | ||