main.cpp为:#include<iostream> #include<string> #include "findmin.h" using namespace std; int main() { int n1, n2, n3; int a[20]; double b[20]; string c[20]; cin >> n1; for (int i = 0; i < n1; i++) cin >> a[i]; cin >> n2; for (int i = 0; i < n2; i++) cin >> b[i]; cin >> n3; for (int i = 0; i < n3; i++) cin >> c[i]; cout << Min(a, n1) << endl; cout << Min(b, n2) << endl; cout << Min(c, n3) << endl; return 0; }。写出findmin.cpp
时间: 2024-01-28 14:05:55 浏览: 50
Certainly! Here is a possible implementation of `findmin.cpp` for the program:
```c++
#include "findmin.h"
int Min(int a[], int n) {
int min = a[0];
for (int i = 1; i < n; i++) {
if (a[i] < min) {
min = a[i];
}
}
return min;
}
double Min(double a[], int n) {
double min = a[0];
for (int i = 1; i < n; i++) {
if (a[i] < min) {
min = a[i];
}
}
return min;
}
string Min(string a[], int n) {
string min = a[0];
for (int i = 1; i < n; i++) {
if (a[i] < min) {
min = a[i];
}
}
return min;
}
```
This file defines three versions of the `Min` function, using function overloading to differentiate between the input types `int[]`, `double[]`, and `string[]`. Each function iterates through the array and updates a variable `min` to hold the current minimum value, then returns that value when the iteration is complete.
Note that this implementation assumes that the input arrays are non-empty. If there is a possibility that an array might be empty, additional checks should be added to handle that case.
阅读全文