Untitled
unknown
plain_text
7 months ago
8.1 kB
4
Indexable
Never
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_DIGITS 1000 typedef struct { int size; int* digits; int sign; } LONGNUMBER; void reverseString(char* str); LONGNUMBER addLongNumbers(LONGNUMBER a, LONGNUMBER b); LONGNUMBER subtractLongNumbers(LONGNUMBER a, LONGNUMBER b); LONGNUMBER sumRangeLongNumbers(int from, int to); LONGNUMBER multiplyLongNumbers(LONGNUMBER a, LONGNUMBER b); LONGNUMBER factorialLongNumber(int n); LONGNUMBER powerLongNumber(LONGNUMBER a, int n) ; LONGNUMBER createLongNumberFromString(const char* str); void freeLongNumber(LONGNUMBER* num); int compareAbsoluteValues(LONGNUMBER a, LONGNUMBER b); void reverseString(char* str) { int n = strlen(str); for (int i = 0; i < n / 2; i++) { char temp = str[i]; str[i] = str[n - i - 1]; str[n - i - 1] = temp; } } LONGNUMBER createLongNumberFromString(const char* str) { LONGNUMBER num; int start = 0; if (str[0] == '-') { num.sign = -1; start = 1; } else { num.sign = 1; } num.size = strlen(str) - start; num.digits = (int*)malloc(num.size * sizeof(int)); for (int i = start; i < strlen(str); i++) { num.digits[strlen(str) - 1 - i] = str[i] - '0'; } return num; } void freeLongNumber(LONGNUMBER* num) { free(num->digits); num->size = 0; } void printLongNumber(LONGNUMBER num) { if (num.sign == -1) printf("-"); for (int i = num.size - 1; i >= 0; i--) { printf("%d", num.digits[i]); } printf("\n"); } LONGNUMBER addLongNumbers(LONGNUMBER a, LONGNUMBER b) { if (a.sign != b.sign) { b.sign = -b.sign; LONGNUMBER result = subtractLongNumbers(a, b); b.sign = -b.sign; return result; } LONGNUMBER result = {0, NULL, a.sign}; int maxSize = a.size > b.size ? a.size : b.size; result.digits = (int*)calloc(maxSize + 1, sizeof(int)); int carry = 0; for (int i = 0; i < maxSize || carry; ++i) { int sum = carry + (i < a.size ? a.digits[i] : 0) + (i < b.size ? b.digits[i] : 0); result.digits[result.size++] = sum % 10; carry = sum / 10; } return result; } LONGNUMBER subtractLongNumbers(LONGNUMBER a, LONGNUMBER b) { if (a.sign != b.sign) { b.sign *= -1; LONGNUMBER result = addLongNumbers(a, b); b.sign *= -1; return result; } if (a.sign < 0 && b.sign < 0) { a.sign = b.sign = 1; return subtractLongNumbers(b, a); } LONGNUMBER result; int isALarger = compareAbsoluteValues(a, b); if (isALarger >= 0) { result.sign = 1; } else { LONGNUMBER temp = a; a = b; b = temp; result.sign = -1; } result.digits = (int*)calloc(a.size, sizeof(int)); int borrow = 0; for (int i = 0; i < a.size; i++) { int sub = (a.digits[i] - (i < b.size ? b.digits[i] : 0) - borrow); if (sub < 0) { sub += 10; borrow = 1; } else { borrow = 0; } result.digits[i] = sub; } result.size = a.size; while (result.size > 1 && result.digits[result.size - 1] == 0) { result.size--; } return result; } int compareAbsoluteValues(LONGNUMBER a, LONGNUMBER b) { if (a.size != b.size) { return a.size - b.size; } for (int i = a.size - 1; i >= 0; i--) { if (a.digits[i] != b.digits[i]) { return a.digits[i] - b.digits[i]; } } return 0; } LONGNUMBER multiplyLongNumbers(LONGNUMBER a, LONGNUMBER b) { LONGNUMBER result; result.size = a.size + b.size; result.digits = (int*)calloc(result.size, sizeof(int)); result.sign = a.sign * b.sign; for (int i = 0; i < a.size; i++) { for (int j = 0; j < b.size; j++) { result.digits[i + j] += a.digits[i] * b.digits[j]; result.digits[i + j + 1] += result.digits[i + j] / 10; result.digits[i + j] %= 10; } } while (result.size > 1 && result.digits[result.size - 1] == 0) result.size--; return result; } LONGNUMBER powerLongNumber(LONGNUMBER a, int n) { LONGNUMBER result = createLongNumberFromString("1"); for (int i = 0; i < n; i++) { LONGNUMBER temp = multiplyLongNumbers(result, a); freeLongNumber(&result); result = temp; } return result; } LONGNUMBER factorialLongNumber(int n) { LONGNUMBER result = createLongNumberFromString("1"); for (int i = 2; i <= n; i++) { char str[20]; sprintf(str, "%d", i); LONGNUMBER iNum = createLongNumberFromString(str); LONGNUMBER temp = multiplyLongNumbers(result, iNum); freeLongNumber(&result); freeLongNumber(&iNum); result = temp; } return result; } LONGNUMBER sumRangeLongNumbers(int from, int to) { LONGNUMBER result = createLongNumberFromString("0"); for (int i = from; i <= to; i++) { char str[20]; sprintf(str, "%d", i); LONGNUMBER iNum = createLongNumberFromString(str); LONGNUMBER temp = addLongNumbers(result, iNum); freeLongNumber(&result); freeLongNumber(&iNum); result = temp; } return result; } int main() { char strA[MAX_DIGITS], strB[MAX_DIGITS]; int choice, n, from, to; printf("\nChoose an operation:\n"); printf("1. +\n"); printf("2. -\n"); printf("3. *\n"); printf("4. !(One number)\n"); printf("5. pow(a, b)\n"); printf("6. for sum in range(a, b)\n"); printf("Enter your choice (1, 2, 3, 4, 5, or 6): "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter two numbers: "); scanf("%s %s", strA, strB); LONGNUMBER a = createLongNumberFromString(strA); LONGNUMBER b = createLongNumberFromString(strB); LONGNUMBER sum = addLongNumbers(a, b); printf("Result: "); printLongNumber(sum); freeLongNumber(&a); freeLongNumber(&b); freeLongNumber(&sum); break; case 2: printf("Enter two numbers (a - b): "); scanf("%s %s", strA, strB); a = createLongNumberFromString(strA); b = createLongNumberFromString(strB); LONGNUMBER difference = subtractLongNumbers(a, b); printf("Result: "); printLongNumber(difference); freeLongNumber(&a); freeLongNumber(&b); freeLongNumber(&difference); break; case 3: printf("Enter two numbers: "); scanf("%s %s", strA, strB); a = createLongNumberFromString(strA); b = createLongNumberFromString(strB); LONGNUMBER product = multiplyLongNumbers(a, b); printf("Result: "); printLongNumber(product); freeLongNumber(&a); freeLongNumber(&b); freeLongNumber(&product); break; case 4: printf("Enter a number: "); scanf("%d", &n); LONGNUMBER fact = factorialLongNumber(n); printf("Result: "); printLongNumber(fact); freeLongNumber(&fact); break; case 5: printf("Enter base and exponent in one line: "); scanf("%s %d", strA, &n); a = createLongNumberFromString(strA); LONGNUMBER powResult = powerLongNumber(a, n); printf("Result: "); printLongNumber(powResult); freeLongNumber(&a); freeLongNumber(&powResult); break; case 6: printf("Enter range (from, to): "); scanf("%d %d", &from, &to); LONGNUMBER rangeSum = sumRangeLongNumbers(from, to); printf("Result: "); printLongNumber(rangeSum); freeLongNumber(&rangeSum); break; default: printf("Invalid choice\n"); } return 0; }
Leave a Comment