创建程序,计算两个数的乘法运算,要求输出结果如下图所示。考虑整型、小数等不同类型的矩阵。方法1函数原型:int multiply(int vl,int v2);double multiply(double vl, double v2): 方法2函数模板原型 template<typename T> T multiply(T vl,T v2); 结果图: 请输入两个整数,用空格分隔开,按回车键结束。99输出结果: X=9 y=9x*y=81
时间: 2023-12-03 10:43:13 浏览: 30
方法1代码:
```c++
#include <iostream>
using namespace std;
int multiply(int vl, int v2)
{
return vl * v2;
}
double multiply(double vl, double v2)
{
return vl * v2;
}
int main()
{
int x, y;
cout << "请输入两个整数,用空格分隔开,按回车键结束。";
cin >> x >> y;
cout << "X=" << x << " y=" << y << " x*y=" << multiply(x, y) << endl;
return 0;
}
```
方法2代码:
```c++
#include <iostream>
using namespace std;
template<typename T>
T multiply(T vl, T v2)
{
return vl * v2;
}
int main()
{
int x, y;
cout << "请输入两个整数,用空格分隔开,按回车键结束。";
cin >> x >> y;
cout << "X=" << x << " y=" << y << " x*y=" << multiply(x, y) << endl;
double a, b;
cout << "请输入两个小数,用空格分隔开,按回车键结束。";
cin >> a >> b;
cout << "a=" << a << " b=" << b << " a*b=" << multiply(a, b) << endl;
return 0;
}
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)
![](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)