#include
#include
#include
using namespace std;
// 将三进制数转换为十进制
int ternaryToDecimal(string num) {
int decimal = 0;
int power = 0;
for (int i = num.size() - 1; i >= 0; i–) {
int digit = num[i] - ‘0’;
if (digit < 0 || digit > 2) {
cout << “输入的不是有效的三进制数!” << endl;
return -1;
}
decimal += digit * pow(3, power);
power++;
}
return decimal;
}
// 将十进制转换为三进制
string decimalToTernary(int num) {
string ternary;
while (num > 0) {
ternary += (num % 3) + ‘0’;
num /= 3;
}
reverse(ternary.begin(), ternary.end());
return ternary;
}
// 加法
string addTernary(string num1, string num2) {
int decimal1 = ternaryToDecimal(num1);
int decimal2 = ternaryToDecimal(num2);
if (decimal1 == -1 || decimal2 == -1) {
return “错误: 无效的输入”;
}
int sum = decimal1 + decimal2;
return decimalToTernary(sum);
}
// 减法
string subtractTernary(string num1, string num2) {
int decimal1 = ternaryToDecimal(num1);
int decimal2 = ternaryToDecimal(num2);
if (decimal1 == -1 || decimal2 == -1) {
return “错误: 无效的输入”;
}
int difference = decimal1 - decimal2;
if (difference < 0) {
return “错误: 结果为负数”;
}
return decimalToTernary(difference);
}
// 乘法
string multiplyTernary(string num1, string num2) {
int decimal1 = ternaryToDecimal(num1);
int decimal2 = ternaryToDecimal(num2);
if (decimal1 == -1 || decimal2 == -1) {
return “错误: 无效的输入”;
}
int product = decimal1 * decimal2;
return decimalToTernary(product);
}
// 除法
string divideTernary(string num1, string num2) {
int decimal1 = ternaryToDecimal(num1);
int decimal2 = ternaryToDecimal(num2);
if (decimal1 == -1 || decimal2 == -1) {
return “错误: 无效的输入”;
}
if (decimal2 == 0) {
return “错误: 除数不能为 0”;
}
int quotient = decimal1 / decimal2;
return decimalToTernary(quotient);
}
// 平方根
string squareRootTernary(string num) {
int decimal = ternaryToDecimal(num);
if (decimal == -1) {
return “错误: 无效的输入”;
}
if (decimal < 0) {
return “错误: 不能计算负数的平方根”;
}
double root = sqrt(decimal);
return decimalToTernary(static_cast(root));
}
// 幂运算
string powerTernary(string num, int exponent) {
int decimal = ternaryToDecimal(num);
if (decimal == -1) {
return “错误: 无效的输入”;
}
int powerResult = static_cast(pow(decimal, exponent));
return decimalToTernary(powerResult);
}
// 阶乘
string factorialTernary(string num) {
int decimal = ternaryToDecimal(num);
if (decimal == -1) {
return “错误: 无效的输入”;
}
if (decimal < 0) {
return “错误: 负数没有阶乘”;
}
int fact = 1;
for (int i = 1; i <= decimal; i++) {
fact *= i;
}
return decimalToTernary(fact);
}
int main() {
int choice;
string num1, num2, result;
do {
cout << "1. 加法" << endl;
cout << "2. 减法" << endl;
cout << "3. 乘法" << endl;
cout << "4. 除法" << endl;
cout << "5. 平方根" << endl;
cout << "6. 幂运算" << endl;
cout << "7. 阶乘" << endl;
cout << "8. 继续" << endl;
cout << "9. 退出" << endl;
cout << "请选择: ";
cin >> choice;
switch (choice) {
case 1:
cout << "请输入第一个三进制数: ";
cin >> num1;
cout << "请输入第二个三进制数: ";
cin >> num2;
result = addTernary(num1, num2);
cout << "结果: " << result << endl;
break;
case 2:
cout << "请输入被减数(三进制): ";
cin >> num1;
cout << "请输入减数(三进制): ";
cin >> num2;
result = subtractTernary(num1, num2);
cout << "结果: " << result << endl;
break;
case 3:
cout << "请输入第一个三进制数: ";
cin >> num1;
cout << "请输入第二个三进制数: ";
cin >> num2;
result = multiplyTernary(num1, num2);
cout << "结果: " << result << endl;
break;
case 4:
cout << "请输入被除数(三进制): ";
cin >> num1;
cout << "请输入除数(三进制): ";
cin >> num2;
result = divideTernary(num1, num2);
cout << "结果: " << result << endl;
break;
case 5:
cout << "请输入要计算平方根的三进制数: ";
cin >> num1;
result = squareRootTernary(num1);
cout << "结果: " << result << endl;
break;
case 6:
cout << "请输入底数(三进制): ";
cin >> num1;
int exponent;
cout << "请输入指数: ";
cin >> exponent;
result = powerTernary(num1, exponent);
cout << "结果: " << result << endl;
break;
case 7:
cout << "请输入要求阶乘的三进制数: ";
cin >> num1;
result = factorialTernary(num1);
cout << "结果: " << result << endl;
break;
case 8:
cout << "继续进行其他操作..." << endl;
break;
case 9:
cout << "退出系统!" << endl;
break;
default:
cout << "无效的选择,请重新输入!" << endl;
}
} while (choice!= 9);
return 0;
}