Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
6.6 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];   // Массив цифр, хранящихся в обратном порядке
} 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}};
    num.size = strlen(str);
    for (int i = 0; i < num.size; i++) {
        num.digits[i] = str[num.size - 1 - i] - '0';
    }
    return num;
}

void printLongNumber(LONGNUMBER num) {
    for (int i = num.size - 1; i >= 0; i--) {
        printf("%d", num.digits[i]);
    }
    printf("\n");
}

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

LONGNUMBER subtractLongNumbers(LONGNUMBER a, LONGNUMBER b) {
    LONGNUMBER result = {0, {0}};
    int borrow = 0;
    for (int i = 0; i < a.size; i++) {
        int aDigit = a.digits[i];
        int bDigit = i < b.size ? b.digits[i] : 0;
        int diff = aDigit - bDigit - borrow;
        if (diff < 0) {
            diff += 10;
            borrow = 1;
        } else {
            borrow = 0;
        }
        result.digits[i] = diff;
    }
    result.size = a.size;
    while (result.size > 1 && result.digits[result.size - 1] == 0) result.size--;
    return result;
}

LONGNUMBER multiplyLongNumbers(LONGNUMBER a, LONGNUMBER b) {
    LONGNUMBER result = {0, {0}};
    for (int i = 0; i < a.size + b.size; i++) {
        result.digits[i] = 0;
    }

    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]; // Достаточно для любых n <= 10^9
        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 result = createLongNumberFromString("0");
    for (int i = from; i <= to; i++) {
        char str[10]; // Достаточно для любых i <= 10^9
        sprintf(str, "%d", i);
        LONGNUMBER iNum = createLongNumberFromString(str);
        LONGNUMBER temp = addLongNumbers(result, 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);
            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