写一个计算器,使用方法重载实现整数、单/双精度浮点数的加法计算,实现char类型拼接。结果
时间: 2024-03-16 15:43:23 浏览: 67
c语言浮点数高精度加法计算
好的,我可以为您编写这个计算器,以下是代码示例:
```c++
#include <iostream>
#include <string>
using namespace std;
class Calculator {
public:
int add(int a, int b) {
return a + b;
}
float add(float a, float b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
string add(char a, char b) {
string result = "";
result += a;
result += b;
return result;
}
};
int main() {
Calculator calc;
int a = 1, b = 2;
float c = 1.5, d = 2.5;
double e = 1.234, f = 3.456;
char g = 'H', h = 'i';
cout << "整数加法结果:" << calc.add(a, b) << endl;
cout << "单精度浮点数加法结果:" << calc.add(c, d) << endl;
cout << "双精度浮点数加法结果:" << calc.add(e, f) << endl;
cout << "字符拼接结果:" << calc.add(g, h) << endl;
return 0;
}
```
运行结果如下:
```
整数加法结果:
阅读全文