Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
7.8 kB
1
Indexable
Never
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_DIGITS 3100

typedef struct {
    int size;
    int digits[MAX_DIGITS];
    int sign;
} LONGNUMBER;

void reverseString(char* str);
LONGNUMBER createLongNumberFromString(const char* str);
void printLongNumber(LONGNUMBER num);
LONGNUMBER addLongNumbers(LONGNUMBER a, LONGNUMBER b);
LONGNUMBER subtractLongNumbers(LONGNUMBER a, LONGNUMBER b);
LONGNUMBER multiplyLongNumbers(LONGNUMBER a, LONGNUMBER b);
LONGNUMBER factorialLongNumber(int n);
LONGNUMBER powerLongNumber(LONGNUMBER a, int n);
LONGNUMBER sumRangeLongNumbers(int from, int to);

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 = {0, {0}, 1};
    int start = 0;
    if (str[0] == '-') {
        num.sign = -1;
        start = 1;
    }
    num.size = strlen(str) - start;
    for (int i = 0; i < num.size; i++) {
        num.digits[i] = str[num.size - 1 - i + start] - '0';
    }
    return num;
}

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.size == 1 && a.digits[0] == 0) && (b.size == 1 && b.digits[0] == 0)) {
        LONGNUMBER zero = {1, {0}, 1};
        return zero;
    }

    if (a.sign != b.sign) {
        if (a.sign == -1) {
            a.sign = 1;
            LONGNUMBER result = subtractLongNumbers(b, a);
            if (result.size == 1 && result.digits[0] == 0) result.sign = 1;
            return result;
        } else {
            b.sign = 1;
            LONGNUMBER result = subtractLongNumbers(a, b);
            if (result.size == 1 && result.digits[0] == 0) result.sign = 1;
            return result;
        }
    }

    LONGNUMBER result = {0, {0}, a.sign};
    int carry = 0;
    for (int i = 0; i < MAX_DIGITS; i++) {
        if (i >= a.size && i >= b.size && carry == 0) break;
        int sum = (i < a.size ? a.digits[i] : 0) + (i < b.size ? b.digits[i] : 0) + carry;
        result.digits[i] = sum % 10;
        carry = sum / 10;
        result.size = i + 1;
    }
    if (result.size == 1 && result.digits[0] == 0) result.sign = 1;

    return result;
}


int compareLongNumbers(LONGNUMBER a, LONGNUMBER b) {
    if (a.size > b.size) return 1; 
    if (a.size < b.size) return -1; 

    for (int i = a.size - 1; i >= 0; i--) {
        if (a.digits[i] > b.digits[i]) return 1;
        if (a.digits[i] < b.digits[i]) return -1;
    }

    return 0;
}

LONGNUMBER subtractLongNumbers(LONGNUMBER a, LONGNUMBER b) {
    if (a.sign == -1 || b.sign == -1) {
        b.sign = -b.sign;
        return addLongNumbers(a, b);
    }

    if (compareLongNumbers(a, b) == -1) {
        LONGNUMBER temp = a;
        a = b;
        b = temp;
        a.sign = -1;
    }

    LONGNUMBER result = {0, {0}, 1};
    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;
        if (sub != 0) result.size = i + 1;
    }
    result.sign = a.sign;
    return result;
}

LONGNUMBER multiplyLongNumbers(LONGNUMBER a, LONGNUMBER b) {
    LONGNUMBER result;
    memset(&result, 0, sizeof(result));

    if ((a.size == 1 && a.digits[0] == 0) || (b.size == 1 && b.digits[0] == 0)) {
        result.sign = 1;
        result.size = 1;
        result.digits[0] = 0;
        return result;
    }

    result.sign = a.sign == b.sign ? 1 : -1;

    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;
        }
    }

    result.size = a.size + b.size;
    while (result.size > 1 && result.digits[result.size - 1] == 0) {
        result.size--;
    }

    return result;
}


LONGNUMBER factorialLongNumber(int n) {
    LONGNUMBER result = createLongNumberFromString("1");
    for (int i = 2; i <= n; i++) {
        char str[10];
        sprintf(str, "%d", i);
        LONGNUMBER iNum = createLongNumberFromString(str);
        LONGNUMBER temp = multiplyLongNumbers(result, iNum);
        result = temp;
    }
    return result;
}

LONGNUMBER powerLongNumber(LONGNUMBER a, int n) {
    LONGNUMBER result = createLongNumberFromString("1");
    for (int i = 0; i < n; i++) {
        LONGNUMBER temp = multiplyLongNumbers(result, a);
        result = temp;
    }
    return result;
}

LONGNUMBER sumRangeLongNumbers(int from, int to) {
    LONGNUMBER sum = createLongNumberFromString("0");

    char numberStr[50];

    for (int i = from; i <= to; i++) {
        sprintf(numberStr, "%d", i);
        LONGNUMBER current = createLongNumberFromString(numberStr);
        LONGNUMBER newSum = addLongNumbers(sum, current);
        sum = newSum;
    }

    return sum;
}


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);
            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);
            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);
            break;
        case 4: // Факториал
            printf("Enter a number: ");
            scanf("%d", &n);
            LONGNUMBER fact = factorialLongNumber(n);
            printf("Result: ");
            printLongNumber(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);
            break;
        case 6: // Сумма диапазона
            printf("Enter range (from, to): ");
            scanf("%d %d", &from, &to);
            LONGNUMBER rangeSum = sumRangeLongNumbers(from, to);
            printf("Result: ");
            printLongNumber(rangeSum);
            break;
        default:
            printf("Invalid choice\n");
    }

    return 0;
}
Leave a Comment