面试题答案
一键面试设计思路
NumericValue
类设计:- 使用
union
来存储不同类型的数据,以节省内存空间。 - 使用枚举类型来标记当前存储的数据类型。
- 使用
- 类型转换运算符:
- 实现
operator int()
和operator double()
。在转换时,根据枚举类型判断当前数据类型,若为浮点数转int
则考虑精度损失和截断问题;若为int
转double
则直接转换。
- 实现
- 运算符重载:
- 对于
+
、-
、*
、/
运算符,首先判断两个操作数的数据类型。 - 如果类型相同,直接进行相应类型的运算。
- 如果类型不同,将
int
类型转换为double
类型后再进行运算,以保证精度。
- 对于
实现细节
#include <iostream>
#include <stdexcept>
// 定义枚举类型表示数据类型
enum class NumericType {
Int,
Double
};
class NumericValue {
private:
union {
int intValue;
double doubleValue;
};
NumericType type;
public:
// 构造函数,根据传入的值类型初始化
NumericValue(int value) : intValue(value), type(NumericType::Int) {}
NumericValue(double value) : doubleValue(value), type(NumericType::Double) {}
// 类型转换运算符
operator int() const {
if (type == NumericType::Int) {
return intValue;
} else if (type == NumericType::Double) {
return static_cast<int>(doubleValue); // 考虑精度损失和截断
}
throw std::runtime_error("Invalid type conversion");
}
operator double() const {
if (type == NumericType::Int) {
return static_cast<double>(intValue);
} else if (type == NumericType::Double) {
return doubleValue;
}
throw std::runtime_error("Invalid type conversion");
}
// 运算符重载
NumericValue operator+(const NumericValue& other) const {
if (type == NumericType::Int && other.type == NumericType::Int) {
return NumericValue(intValue + other.intValue);
} else if (type == NumericType::Double && other.type == NumericType::Double) {
return NumericValue(doubleValue + other.doubleValue);
} else {
double left = static_cast<double>(*this);
double right = static_cast<double>(other);
return NumericValue(left + right);
}
}
NumericValue operator-(const NumericValue& other) const {
if (type == NumericType::Int && other.type == NumericType::Int) {
return NumericValue(intValue - other.intValue);
} else if (type == NumericType::Double && other.type == NumericType::Double) {
return NumericValue(doubleValue - other.doubleValue);
} else {
double left = static_cast<double>(*this);
double right = static_cast<double>(other);
return NumericValue(left - right);
}
}
NumericValue operator*(const NumericValue& other) const {
if (type == NumericType::Int && other.type == NumericType::Int) {
return NumericValue(intValue * other.intValue);
} else if (type == NumericType::Double && other.type == NumericType::Double) {
return NumericValue(doubleValue * other.doubleValue);
} else {
double left = static_cast<double>(*this);
double right = static_cast<double>(other);
return NumericValue(left * right);
}
}
NumericValue operator/(const NumericValue& other) const {
if (type == NumericType::Int && other.type == NumericType::Int) {
if (other.intValue == 0) {
throw std::runtime_error("Division by zero");
}
return NumericValue(intValue / other.intValue);
} else if (type == NumericType::Double && other.type == NumericType::Double) {
if (other.doubleValue == 0) {
throw std::runtime_error("Division by zero");
}
return NumericValue(doubleValue / other.doubleValue);
} else {
double left = static_cast<double>(*this);
double right = static_cast<double>(other);
if (right == 0) {
throw std::runtime_error("Division by zero");
}
return NumericValue(left / right);
}
}
};
你可以通过以下方式测试这些函数:
int main() {
NumericValue a(5);
NumericValue b(2.5);
NumericValue resultAdd = a + b;
std::cout << "Addition result: " << static_cast<double>(resultAdd) << std::endl;
NumericValue resultSub = a - b;
std::cout << "Subtraction result: " << static_cast<double>(resultSub) << std::endl;
NumericValue resultMul = a * b;
std::cout << "Multiplication result: " << static_cast<double>(resultMul) << std::endl;
NumericValue resultDiv = a / b;
std::cout << "Division result: " << static_cast<double>(resultDiv) << std::endl;
return 0;
}