c语言:长整数1输入、长整数2输入、长整数输出、加法、减法、除法、退出系统
时间: 2023-10-07 18:12:46 浏览: 139
用C++实现长整数的输入输出
好的,以下是 C 语言实现长整数输入、输出、加减除法和退出系统的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200 // 数字串的最大长度
// 枚举运算符类型
typedef enum { ADD, SUB, MUL, DIV } Operator;
// 倒置字符串
void reverse(char* str, int len) {
int i = 0, j = len - 1;
while (i < j) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
// 去掉前导零
void removeLeadingZero(char* str) {
int len = strlen(str);
int i = 0;
while (i < len - 1 && str[i] == '0') {
i++;
}
if (i > 0) {
for (int j = i; j < len; j++) {
str[j - i] = str[j];
}
str[len - i] = '\0';
}
}
// 长整数加法
char* add(char* num1, char* num2) {
int len1 = strlen(num1), len2 = strlen(num2);
int len = len1 > len2 ? len1 : len2;
char* result = (char*)malloc(sizeof(char) * (len + 2)); // 多分配一位用于存放进位
memset(result, '0', len + 1);
result[len + 1] = '\0';
reverse(num1, len1);
reverse(num2, len2);
for (int i = 0; i < len; i++) {
int n1 = i < len1 ? num1[i] - '0' : 0;
int n2 = i < len2 ? num2[i] - '0' : 0;
int sum = result[i] - '0' + n1 + n2;
result[i] = sum % 10 + '0';
result[i + 1] += sum / 10;
}
reverse(result, len + 1);
removeLeadingZero(result);
return result;
}
// 长整数减法
char* sub(char* num1, char* num2) {
int len1 = strlen(num1), len2 = strlen(num2);
int len = len1 > len2 ? len1 : len2;
char* result = (char*)malloc(sizeof(char) * (len + 1));
int borrow = 0;
reverse(num1, len1);
reverse(num2, len2);
for (int i = 0; i < len; i++) {
int n1 = i < len1 ? num1[i] - '0' : 0;
int n2 = i < len2 ? num2[i] - '0' : 0;
int diff = n1 - n2 - borrow;
if (diff < 0) {
diff += 10;
borrow = 1;
} else {
borrow = 0;
}
result[i] = diff + '0';
}
reverse(result, len);
removeLeadingZero(result);
return result;
}
// 长整数乘法
char* mul(char* num1, char* num2) {
int len1 = strlen(num1), len2 = strlen(num2);
int len = len1 + len2 + 1;
char* result = (char*)malloc(sizeof(char) * (len + 1));
memset(result, '0', len);
result[len] = '\0';
reverse(num1, len1);
reverse(num2, len2);
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
int n1 = num1[i] - '0', n2 = num2[j] - '0';
int mul = n1 * n2 + result[i + j] - '0';
result[i + j] = mul % 10 + '0';
result[i + j + 1] += mul / 10;
}
}
reverse(result, len);
removeLeadingZero(result);
return result;
}
// 长整数除法
char* div(char* num1, char* num2) {
int len1 = strlen(num1), len2 = strlen(num2);
if (len2 == 1 && num2[0] == '0') {
return NULL;
}
if (len1 < len2 || (len1 == len2 && strcmp(num1, num2) < 0)) {
char* result = (char*)malloc(sizeof(char) * 2);
result[0] = '0';
result[1] = '\0';
return result;
}
int len = len1 - len2 + 2;
char* result = (char*)malloc(sizeof(char) * (len + 1));
memset(result, '0', len);
result[len] = '\0';
char* temp = (char*)malloc(sizeof(char) * (len1 + 1));
memset(temp, '0', len1 + 1);
temp[len1] = '\0';
strncpy(temp, num1, len2);
for (int i = len2 - 1; i < len1; i++) {
int quotient = 0;
while (strcmp(temp, num2) >= 0) {
quotient++;
char* diff = sub(temp, num2);
strcpy(temp, diff);
free(diff);
}
result[i - len2 + 2] = quotient + '0';
temp[i + 1] = num1[i + 1];
}
removeLeadingZero(result);
free(temp);
return result;
}
int main() {
char num1[N], num2[N];
while (1) {
printf("请选择操作:\n");
printf("1. 长整数加法\n");
printf("2. 长整数减法\n");
printf("3. 长整数乘法\n");
printf("4. 长整数除法\n");
printf("5. 退出系统\n");
int op;
scanf("%d", &op);
if (op == 5) {
break;
}
printf("请输入第一个长整数:");
scanf("%s", num1);
printf("请输入第二个长整数:");
scanf("%s", num2);
char* result;
switch (op) {
case 1:
result = add(num1, num2);
printf("结果为:%s\n", result);
free(result);
break;
case 2:
result = sub(num1, num2);
printf("结果为:%s\n", result);
free(result);
break;
case 3:
result = mul(num1, num2);
printf("结果为:%s\n", result);
free(result);
break;
case 4:
result = div(num1, num2);
if (result == NULL) {
printf("除数不能为0!\n");
} else {
printf("结果为:%s\n", result);
free(result);
}
break;
default:
printf("无效操作!\n");
break;
}
}
printf("感谢使用!\n");
return 0;
}
```
该程序实现了长整数的加减乘除四种基本运算,并提供了退出系统的选项。用户可以根据提示输入操作和操作数,程序会输出对应的结果。需要注意的是,该程序假设输入的长整数都是非负整数。
阅读全文