Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include <malloc.h>
- #include <assert.h>
- #define MAX_SIZE 500
- typedef char* verylong;
- verylong input_long(void);
- verylong add_verylong(verylong vl1, verylong vl2);
- verylong add(verylong ln, verylong sn);
- verylong multi(verylong vl1, verylong vl2);
- int c2n(char ch);
- char n2c(int n);
- void main() {
- verylong a, b, c;
- do {
- printf("enter the first long integer: ");
- a = input_long();
- } while (!a);
- do {
- printf("enter the second long integer: ");
- b = input_long();
- } while (!b);
- c = add_verylong(a, b);
- printf("%s + %s = %s\n", a, b, c);
- c = multiply_verylong(a, b);
- printf("%s * %s = %s\n", a, b, c);
- free(c);
- free(a);
- free(b);
- //system("pause");
- }
- verylong input_long(void) {
- /*
- * This function is used to input a 'verylong' number from the user:
- */
- // Setting up a place in the RAM for the number:
- verylong res = (verylong)malloc(sizeof(char) * MAX_SIZE);
- assert(res);
- char dig;
- int i = 0;
- // Getting the number from the user:
- gets(res);
- // Iterating through 'res' until we reach NULL:
- for (i = 0; *(res + i) != '\0'; i++) {
- dig = *(res + i);
- if ((dig > '9') || (dig < '0')) {
- printf("wrong input");
- return NULL;
- }
- }
- // Finally, returning the place in the memory the correct number is located in, after resizing it and asserting it:
- res = (verylong)realloc(res, sizeof(char) * (strlen(res) + 1));
- assert(res);
- return res;
- }
- verylong add_verylong(verylong vl1, verylong vl2) {
- /*
- * Since the logic 'add' function only handles the option in which vl1.size >= vl2.size, we use this function to invoke 'add_vl' properly:
- */
- if (strlen(vl1) > strlen(vl2)) add_vl(vl1, vl2);
- else add(vl2, vl1);
- }
- verylong multiply_verylong(verylong vl1, verylong vl2) {
- /*
- * Since the logic 'multi' function only handles the option in which vl1.size >= vl2.size, we use this function to invoke 'add_vl' properly:
- */
- if (strlen(vl1) > strlen(vl2)) add_vl(vl1, vl2);
- else add(vl2, vl1);
- }
- verylong add(verylong ln, verylong sn) {
- /*
- * This function calculates the sum between numbers when ln is longer than sn physically.
- */
- int i, j, longerlen, shorterlen, len1 = strlen(ln), len2 = strlen(sn), pow = 1, carry = 0, sum;
- // Allocating memory space for the result: (the size will be the longer number's size +2 for nullchar and carry)
- verylong res = (verylong)calloc(sizeof(char), (len1 + 2)), res_opt = (verylong)calloc(sizeof(char), (len1 + 1));
- // Adding each number exactly like decimal addition:
- for (i = len1, j = len2; i > 0; i--, j--) {
- if (j > 0) {
- sum = c2n(*(ln + -1 + i)) + c2n(*(sn + j - 1)) + carry;
- *(res + i) = n2c((sum % 10));
- carry = sum / 10;
- }
- else {
- sum = c2n(*(ln + i - 1)) + carry;
- *(res + i) = n2c((sum % 10));
- carry = sum / 10;
- }
- }
- *res = '0' + carry * 1;
- *(res + len1 + 1) = '\0';
- if (*res == '0') {
- strcpy(res_opt, res + 1);
- return res_opt;
- }
- return res;
- }
- verylong multi(verylong ln, verylong sn) {
- /*
- * This function calculates the multiplication between numbers when ln is longer than sn physically.
- */
- int i, pow = 1, temp, j, len2;
- // Allocating memory for the reuslt: (the size will be the size of vl1) (this will be changed in future runs)
- verylong res = (verylong)calloc(sizeof(char), (strlen(ln) + 1));
- assert(res);
- *res = '0';
- *(res + 1) = '\0';
- len2 = strlen(sn);
- for (i = len2; i >= 0; i--) {
- // Calculating the temporary vl2 as a number one place at a time:
- temp = c2n(*(ln + i - 1)) * pow;
- // Multiplying vl1 by vl2 one place at a time:
- for (j = 0; j < temp; j++) {
- res = (add_verylong(res, sn));
- }
- // Going up the places:
- pow = pow * 10;
- }
- // Finally, returning the multiplication result:
- return res;
- }
- /*
- * Those are optional helping functions that help us calculate and convert from int to char and vice versa:
- */
- int c2n(char ch) {
- /*
- * This function converts a charnum into a real int:
- * (does not have input checking)
- */
- return (ch - '0');
- }
- char n2c(int n) {
- /*
- * This function converts a charnum into a real int:
- * (does not have input checking)
- */
- return (n + '0');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement