写一个计算器,使用方法重载实现整数、单/双精度浮点数的加法计算,实现char类型拼接
时间: 2024-03-14 11:47:19 浏览: 129
好的,我可以帮你写一个这样的计算器,使用方法重载实现不同类型的加法计算。下面是代码示例:
```c++
#include <iostream>
#include <string>
using namespace std;
// 定义加法运算函数,使用方法重载实现不同类型的加法计算
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
float add(float a, float b) {
return a + b;
}
// 定义 char 类型拼接函数
string add(char a, char b) {
string result = "";
result += a;
result += b;
return result;
}
int main() {
int a = 5, b = 7;
double c = 3.14, d = 2.718;
float e = 1.23f, f = 4.56f;
char g = 'H', h = 'i';
// 调用不同类型的加法函数,进行计算
cout << "整数加法计算结果:" << add(a, b) << endl;
cout << "双精度浮点数加法计算结果:" << add(c, d) << endl;
cout << "单精度浮点数加法计算结果:" << add(e, f) << endl;
cout << "char 类型拼接结果:" << add(g, h) << endl;
return 0;
}
```
这个计算器可以实现整数、单/双精度浮点数的加法计算,以及 char 类型的拼接。你可以根据需要进行调用。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![mwp](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)