c语言:长整数输入、长整数输出、加法、减法、除法、进制转换、退出系统
时间: 2023-09-28 13:04:21 浏览: 178
用C++实现长整数的输入输出
好的,我可以帮你实现这些功能。首先,需要了解长整数的表示方式。在C语言中,可以使用字符串来表示长整数,每一位数字都是一个字符,例如数字123456789可以表示为字符串"123456789"。下面是实现这些功能的代码示例:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 1000 // 定义最大长度
// 函数声明
void input(char* num);
void output(char* num);
void add(char* num1, char* num2, char* result);
void subtract(char* num1, char* num2, char* result);
void divide(char* num1, char* num2, char* result);
void convert(char* num, int from, int to);
int main() {
char num1[MAX_SIZE], num2[MAX_SIZE], result[MAX_SIZE];
int choice, from, to;
while (1) {
printf("请选择要进行的操作:\n");
printf("1. 长整数输入\n");
printf("2. 长整数输出\n");
printf("3. 长整数加法\n");
printf("4. 长整数减法\n");
printf("5. 长整数除法\n");
printf("6. 进制转换\n");
printf("7. 退出系统\n");
printf("请输入数字选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入第一个长整数:");
input(num1);
printf("请输入第二个长整数:");
input(num2);
break;
case 2:
printf("第一个长整数为:");
output(num1);
printf("第二个长整数为:");
output(num2);
break;
case 3:
add(num1, num2, result);
printf("两个长整数的和为:");
output(result);
break;
case 4:
subtract(num1, num2, result);
printf("两个长整数的差为:");
output(result);
break;
case 5:
divide(num1, num2, result);
printf("两个长整数的商为:");
output(result);
break;
case 6:
printf("请输入要转换的数字:");
input(num1);
printf("请输入要转换的进制(2-16):");
scanf("%d", &from);
printf("请输入要转换成的进制(2-16):");
scanf("%d", &to);
convert(num1, from, to);
break;
case 7:
printf("已退出系统。\n");
exit(0);
default:
printf("输入有误,请重新输入。\n");
break;
}
}
return 0;
}
// 长整数输入
void input(char* num) {
scanf("%s", num);
}
// 长整数输出
void output(char* num) {
printf("%s\n", num);
}
// 长整数加法
void add(char* num1, char* num2, char* result) {
int carry = 0, len1 = strlen(num1), len2 = strlen(num2), len = 0, i, j, k;
for (i = len1 - 1, j = len2 - 1; i >= 0 || j >= 0; i--, j--) {
int a = i >= 0 ? num1[i] - '0' : 0;
int b = j >= 0 ? num2[j] - '0' : 0;
int sum = a + b + carry;
result[len++] = sum % 10 + '0';
carry = sum / 10;
}
if (carry) {
result[len++] = carry + '0';
}
result[len] = '\0';
for (i = 0, k = len - 1; i < k; i++, k--) {
char temp = result[i];
result[i] = result[k];
result[k] = temp;
}
}
// 长整数减法
void subtract(char* num1, char* num2, char* result) {
int borrow = 0, len1 = strlen(num1), len2 = strlen(num2), len = 0, i, j, k;
for (i = len1 - 1, j = len2 - 1; i >= 0 || j >= 0; i--, j--) {
int a = i >= 0 ? num1[i] - '0' : 0;
int b = j >= 0 ? num2[j] - '0' : 0;
int diff = a - b - borrow;
if (diff < 0) {
diff += 10;
borrow = 1;
} else {
borrow = 0;
}
result[len++] = diff + '0';
}
while (len > 1 && result[len - 1] == '0') {
len--;
}
result[len] = '\0';
for (i = 0, k = len - 1; i < k; i++, k--) {
char temp = result[i];
result[i] = result[k];
result[k] = temp;
}
}
// 长整数除法
void divide(char* num1, char* num2, char* result) {
int len1 = strlen(num1), len2 = strlen(num2), i, j;
char* dividend = (char*)malloc((len1 + 1) * sizeof(char));
char* divisor = (char*)malloc((len2 + 1) * sizeof(char));
char* quotient = (char*)malloc((len1 + 1) * sizeof(char));
char* temp = (char*)malloc((len1 + 1) * sizeof(char));
strcpy(dividend, num1);
strcpy(divisor, num2);
for (i = 0; i < len1 - len2; i++) {
quotient[i] = '0';
}
quotient[i] = '\0';
for (i = len1 - len2; i >= 0; i--) {
int q = 0;
while (strcmp(dividend, divisor) >= 0) {
subtract(dividend, divisor, temp);
strcpy(dividend, temp);
q++;
}
quotient[i] = q + '0';
for (j = strlen(dividend); j >= 1; j--) {
dividend[j] = dividend[j - 1];
}
dividend[0] = '0';
}
while (strlen(quotient) > 1 && quotient[0] == '0') {
strcpy(quotient, quotient + 1);
}
strcpy(result, quotient);
free(dividend);
free(divisor);
free(quotient);
free(temp);
}
// 进制转换
void convert(char* num, int from, int to) {
char* table = "0123456789ABCDEF";
char* result = (char*)malloc((strlen(num) * 4 + 1) * sizeof(char));
char* temp = (char*)malloc((strlen(num) * 4 + 1) * sizeof(char));
result[0] = '0';
result[1] = '\0';
while (strcmp(num, "0") > 0) {
int r = 0, i;
for (i = strlen(num) - 1; i >= 0; i--) {
int d = num[i] - '0';
r = r * from + d;
num[i] = (r / to) + '0';
r %= to;
}
temp[0] = table[r];
temp[1] = '\0';
strcat(temp, result);
strcpy(result, temp);
}
printf("转换结果为:%s\n", result);
free(result);
free(temp);
}
```
注意,这里的加法、减法、除法都是针对正整数的,需要根据实际情况进行修改。另外,进制转换只支持2-16进制之间的转换。
阅读全文