B43. 乘法 创建程序,计算两个数的乘法运算,要求输出结果如下图所示。考志整型、小数 等不同类型的矩阵。 方法1西数原型:int multiply (int v1, int v2): double multiply(double v1, doible v2): 方法2西数模板 原型: template typename I> T multiply (T v1, T v2) ;
时间: 2024-02-16 14:05:32 浏览: 64
方法1:
```c++
#include <iostream>
using namespace std;
int multiply(int v1, int v2) {
return v1 * v2;
}
double multiply(double v1, double v2) {
return v1 * v2;
}
int main() {
int a = 3, b = 4;
double c = 3.14, d = 2.33;
cout << "int multiply: " << multiply(a, b) << endl;
cout << "double multiply: " << multiply(c, d) << endl;
return 0;
}
```
方法2:
```c++
#include <iostream>
using namespace std;
template <typename T>
T multiply(T v1, T v2) {
return v1 * v2;
}
int main() {
int a = 3, b = 4;
double c = 3.14, d = 2.33;
cout << "int multiply: " << multiply<int>(a, b) << endl;
cout << "double multiply: " << multiply<double>(c, d) << endl;
return 0;
}
```
阅读全文