#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
// 62 进制的字符映射
char base62[] = “0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”;
// 将 62 进制数转换为十进制
long long base62ToDecimal(char *num) {
long long decimal = 0;
int len = strlen(num);
for (int i = 0; i < len; i++) {
char digit = num[i];
int digitValue = -1;
for (int j = 0; j < 62; j++) {
if (base62[j] == digit) {
digitValue = j;
break;
}
}
if (digitValue == -1) {
printf(“输入的不是有效的 62 进制数!\n”);
return -1;
}
decimal += digitValue * pow(62, len - i - 1);
}
return decimal;
}
// 将十进制数转换为 62 进制
void decimalToBase62(long long decimal, char *result) {
char temp[100];
int index = 0;
while (decimal > 0) {
temp[index++] = base62[decimal % 62];
decimal /= 62;
}
temp[index] = ‘\0’;
int start = 0, end = index - 1;
while (start < end) {
char t = temp[start];
temp[start] = temp[end];
temp[end] = t;
start++;
end–;
}
strcpy(result, temp);
}
// 加法
void add(char *num1, char *num2, char *result) {
long long decimal1 = base62ToDecimal(num1);
long long decimal2 = base62ToDecimal(num2);
if (decimal1 == -1 || decimal2 == -1) {
return;
}
long long sum = decimal1 + decimal2;
decimalToBase62(sum, result);
}
// 减法
void subtract(char *num1, char *num2, char *result) {
long long decimal1 = base62ToDecimal(num1);
long long decimal2 = base62ToDecimal(num2);
if (decimal1 == -1 || decimal2 == -1) {
return;
}
long long difference = decimal1 - decimal2;
if (difference < 0) {
printf(“结果为负数,不支持!\n”);
return;
}
decimalToBase62(difference, result);
}
// 乘法
void multiply(char *num1, char *num2, char *result) {
long long decimal1 = base62ToDecimal(num1);
long long decimal2 = base62ToDecimal(num2);
if (decimal1 == -1 || decimal2 == -1) {
return;
}
long long product = decimal1 * decimal2;
decimalToBase62(product, result);
}
// 除法
void divide(char *num1, char *num2, char *result) {
long long decimal1 = base62ToDecimal(num1);
long long decimal2 = base62ToDecimal(num2);
if (decimal1 == -1 || decimal2 == -1) {
return;
}
if (decimal2 == 0) {
printf(“除数不能为 0!\n”);
return;
}
long long quotient = decimal1 / decimal2;
decimalToBase62(quotient, result);
}
// 平方根
void squareRoot(char *num, char *result) {
long long decimal = base62ToDecimal(num);
if (decimal == -1) {
return;
}
if (decimal < 0) {
printf(“不能计算负数的平方根!\n”);
return;
}
double root = sqrt(decimal);
decimalToBase62((long long)root, result);
}
// 幂运算
void power(char *num, int exponent, char *result) {
long long decimal = base62ToDecimal(num);
if (decimal == -1) {
return;
}
long long powerResult = (long long)pow(decimal, exponent);
decimalToBase62(powerResult, result);
}
// 阶乘
void factorial(char *num, char *result) {
long long decimal = base62ToDecimal(num);
if (decimal == -1) {
return;
}
if (decimal < 0) {
printf(“负数没有阶乘!\n”);
return;
}
long long fact = 1;
for (long long i = 1; i <= decimal; i++) {
fact *= i;
}
decimalToBase62(fact, result);
}
int main() {
int choice;
char num1[100], num2[100], result[100];
do {
printf("1. 加法\n");
printf("2. 减法\n");
printf("3. 乘法\n");
printf("4. 除法\n");
printf("5. 平方根\n");
printf("6. 幂运算\n");
printf("7. 阶乘\n");
printf("8. 继续\n");
printf("9. 退出\n");
printf("请选择: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入第一个 62 进制数: ");
scanf("%s", num1);
printf("请输入第二个 62 进制数: ");
scanf("%s", num2);
add(num1, num2, result);
printf("结果: %s\n", result);
break;
case 2:
printf("请输入被减数(62 进制): ");
scanf("%s", num1);
printf("请输入减数(62 进制): ");
scanf("%s", num2);
subtract(num1, num2, result);
printf("结果: %s\n", result);
break;
case 3:
printf("请输入第一个 62 进制数: ");
scanf("%s", num1);
printf("请输入第二个 62 进制数: ");
scanf("%s", num2);
multiply(num1, num2, result);
printf("结果: %s\n", result);
break;
case 4:
printf("请输入被除数(62 进制): ");
scanf("%s", num1);
printf("请输入除数(62 进制): ");
scanf("%s", num2);
divide(num1, num2, result);
printf("结果: %s\n", result);
break;
case 5:
printf("请输入要计算平方根的 62 进制数: ");
scanf("%s", num1);
squareRoot(num1, result);
printf("结果: %s\n", result);
break;
case 6:
printf("请输入底数(62 进制): ");
scanf("%s", num1);
int exponent;
printf("请输入指数: ");
scanf("%d", &exponent);
power(num1, exponent, result);
printf("结果: %s\n", result);
break;
case 7:
printf("请输入要求阶乘的 62 进制数: ");
scanf("%s", num1);
factorial(num1, result);
printf("结果: %s\n", result);
break;
case 8:
printf("继续进行其他操作...\n");
break;
case 9:
printf("退出系统!\n");
break;
default:
printf("无效的选择,请重新输入!\n");
}
} while (choice!= 9);
return 0;
}