gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
Требуется написать программу на c++ не используя библиотеку vector, способную…
- User
Требуется написать программу на c++ не используя библиотеку vector, способную производить операции сложении, вычитания, умножения и деления с целыми числами, не превышающими по модулю 10^30. Для хранения цифр использовать массив char, система счисления - десятичная.
- Assistant
#include <iostream> #include <algorithm> #include <string.h> using namespace std; // Объявление функций void add_chars(char *result, const char *a, const char *b); void subtract_chars(char *result, const char *a, const char *b); void multiply_chars(char *result, const char *a, const char *b); void divide_chars(char *result, const char *a, char b); int main() { char a[31] = "123456789012345678901234567890"; char b[31] = "987654321098765432109876543210"; char result[62] = {0}; add_chars(result, a, b); cout << "Sum: " << result << endl; memset(result, 0, sizeof(result)); subtract_chars(result, a, b); cout << "Difference: " << result << endl; memset(result, 0, sizeof(result)); multiply_chars(result, a, b); cout << "Product: " << result << endl; memset(result, 0, sizeof(result)); divide_chars(result, a, 2); cout << "Quotient: " << result << endl; return 0; } void add_chars(char *result, const char *a, const char *b) { int carry = 0; // Обращаем строки для долгого сложения const int len_a = strlen(a); const int len_b = strlen(b); int i; for(i = 0; i < max(len_a, len_b); i++) { int sum = carry; if(i < len_a) { sum += a[len_a - 1 - i] - '0'; } if(i < len_b) { sum += b[len_b - 1 - i] - '0'; } carry = sum / 10; sum %= 10; result[i] = sum + '0'; } if(carry > 0) { result[i] = carry + '0'; i++; } result[i] = '\0'; // Переворачиваем результат int len_res = strlen(result); for(int j = 0; j < len_res / 2; j++) { swap(result[j], result[len_res - 1 - j]); } } void subtract_chars(char *result, const char *a, const char *b) { int carry = 0; // Обращаем строки для долгого вычитания const int len_a = strlen(a); const int len_b = strlen(b); for(int i = 0; i < len_a; i++) { int diff = carry + a[len_a - 1 - i] - '0'; if(i < len_b) { diff -= b[len_b - 1 - i] - '0'; } carry = 0; if(diff < 0) { diff += 10; carry = -1; } result[i] = diff + '0'; } result[len_a] = '\0'; // Удаляем начальные нули bool flag = false; int j = 0; for(int i = len_a - 1; i >= 0 ; i--) { if(!flag && result[i] == '0') { result[i] = '\0'; j = i - 1; } else { flag = true; } } // Переворачиваем результат for(int j = 0; j < strlen(result) / 2; j++) { swap(result[j], result[strlen(result) - 1 - j]); } if(strlen(result) == 0) { strcpy(result, "0"); } } void multiply_chars(char *result, const char *a, const char *b) { // Обращаем строки для умножения const int len_a = strlen(a); const int len_b = strlen(b); for(int i = 0; i < len_a + len_b; i++) { result[i] = '0'; } result[len_a + len_b] = '\0'; for(int i = 0; i < len_a; i++) { int carry = 0; for(int j = 0; j < len_b; j++) { int product = (a[len_a - 1 - i] - '0') * (b[len_b - 1 - j] - '0') + carry + (result[i+j] - '0'); result[i+j] = (product % 10) + '0'; carry = product / 10; } result[len_b + i] = carry + '0'; } bool flag = false; int i = 0; for(int j = len_a + len_b - 1;!flag && j >= 0;--j){ flag = result[j]!='0'; if(flag) i = j; } result[i + 1] = '\0'; for(int j = 0; j < strlen(result) / 2; j++){ swap(result[j], result[strlen(result) - 1 - j]); } } void divide_chars(char *result, const char *a, char b) { int carry = 0; int len_a = strlen(a); int i = 0; while (i < len_a) { carry = carry * 10 + (a[i++] - '0'); result[i - 1] = carry / b + '0'; carry %= b; } result[len_a] = '\0'; // Удаляем начальные нули bool flag = false; int j = 0; for(int i = 0; i < len_a; i++) { if(!flag && result[i] == '0') { result[i] = '\0'; j = i + 1; } else { flag = true; } } strncpy(result, result + j, len_a - j + 1); }