[英]Call function for both min and max in C++
我必須編寫一個程序,在其中輸入參與者的數量,然后輸入姓名、姓氏、年齡、身高和體重。
限制 - 參與者人數必須在 1-30 之間
未滿(<18 歲)和身高<140cm 和>220cm 的必須從列表(數組)中剔除。
最后,我必須顯示最瘦和最胖的參與者的姓名和體重。 我被困在寫最小/最大 function 以找出我的陣列中最薄和最厚的。 對於 min 和 max,我得到相同的結果,但我實際上不知道如何顯示它們的名稱。
到目前為止,這是我的代碼。 有人可以幫忙嗎? 我已經花了幾個小時玩代碼無濟於事。
#include <iostream>
#include <string>
using namespace std;
int N;
void remove_persons(string name[], string surname[], float weight[], float height[], float age[])
{
for (int i = 0; i < N; i++)
{
if (age[i] < 18 || height[i] < 140 || height[i] > 220)
{
for (int j = i; j < N - 1; j++)
{
name[j] = name[j + 1];
surname[j] = surname[j + 1];
age[j] = age[j + 1];
height[j] = height[j + 1];
weight[j] = weight[j + 1];
}
N--;
i--;
}
}
}
float fatfunc(string name[], string surname[], float weight[])
{
float fat = weight[0];
int index_fat = 0;
for (int i = 1; i < N; i++)
{
if (weight[i] > fat)
{
fat = weight[i];
index_fat = i;
}
}
return index_fat;
}
float thinfunc(string name[], string surname[], float weight[])
{
float thin = weight[0];
int index_thin = 0;
for (int i = 1; i < N; i++)
{
if (weight[i] < thin)
{
thin = weight[i];
index_thin = i;
}
}
return index_thin;
}
int main()
{
string name[99];
string surname[99];
float weight[99];
float height[99];
float age[99];
int fat, thin;
do
{
cout << "Nr. of participants 1-30: ";
cin >> N;
} while (N <= 0 || N >= 30);
for (int i = 0; i < N; i++)
{
cout << "Name[" << i << "]: ";
cin >> name[i];
cout << "Surname[" << i << "]: ";
cin >> surname[i];
do
{
cout << "Age[" << i << "] (>18 years): ";
cin >> age[i];
} while (age[i] <= 18);
cout << "Height in cm [" << i << "] (>140 & <220 cm): ";
cin >> height[i];
cout << "Weight [" << i << "]): ";
cin >> weight[i];
}
remove_persons(name, surname, age, weight, height);
thin = thinfunc(name, surname, weight);
cout << endl;
cout << surname[thin] << " " + name[thin] << " is the thinest with a weight of " << weight[thin] << endl;
fat = fatfunc(name, surname, weight);
cout << endl;
cout << surname[fat] << " " + name[fat] << " is the fatest with a weight of " << weight[fat] << endl;
return 0;
}
正如其他人所說,您可以使用std::minmax_element來查找容器中的最小和最大元素。
下面是一個如何使用std::minmax_element的小例子。 請注意,這個免費的 function 返回一個包含 2 個迭代器的std::pair
。 第一個迭代器指向容器中具有最小值的元素,第二個迭代器指向容器中具有最大值的元素。
我使用 結構化綁定將第一個迭代器綁定到lightest
並將第二個迭代器綁定到heaviest
。
#include <vector>
#include <algorithm>
#include <iostream>
int main()
{
std::vector<float> weights{120, 500, 92.4, 162, 172.3, 99.23};
// Note that lightest and heaviest are iterators where the lightest iterator
// points to the the smallest element in the vector and the
// heaviest iterator points to the largest element in the vector.
const auto [lightest, heaviest] = std::minmax_element(
weights.begin(), weights.end());
std::cout << "Lightest: " << *lightest << " Heaviest: " << *heaviest << '\n';
}
如果你不使用標准容器(我認為這不明智),這里是你如何使用std::minmax_element和C
風格arrays
。
int main()
{
float weights[]{120, 500, 92.4f, 162, 172.3f, 99.23f};
const auto [lightest, heaviest] = std::minmax_element(
weights, std::end(weights));
std::cout << "Lightest: " << *lightest << " Heaviest: " << *heaviest << '\n';
}
不是直接的答案,但在 C++20 中,您可以使用完全不同的方法。 您不必從輸入數組(向量)中刪除任何內容,只需使用過濾器跳過基於 for 循環的范圍內不需要的項目。
#include <string>
#include <vector>
#include <ranges>
#include <iostream>
#include <format>
struct person_t
{
std::string name;
unsigned int age;
double height;
};
// a predicate operating on a person and returning a boolean
// this can later be used in a filter in a range based for loop
bool valid_height(const person_t& person)
{
return (person.height >= 1.4) && (person.height <= 2.2);
}
bool valid_age(const person_t& person)
{
return person.age >= 18;
}
// overload to output a person, makes code more readable later
std::ostream& operator<<(std::ostream& os, const person_t& person)
{
os << std::format("person : [name = {0}, age = {1}, height = {2}]\n", person.name, person.age, person.height);
return os;
}
int main()
{
// Create an "array" with 3 persons
std::vector<person_t> persons{ {"Alice", 20, 1.78}, {"Bob", 4, 78.5}, {"Charlie", 19, 2.4 } };
// loop only over those persons that meet a criterion
// https://en.cppreference.com/w/cpp/ranges/filter_view
// and pick up to 30 matching persons
// https://en.cppreference.com/w/cpp/ranges/take_view
for (const auto& person : persons
| std::views::filter(valid_height)
| std::views::filter(valid_age)
| std::views::take(30))
{
std::cout << person;
}
return 0;
}
對於你的第一個問題,寫 min max function: 答案:你傳遞了 remove_person function 的錯誤值,你這樣做了 //remove_persons(name, surname, age, weight, height); 應該是這樣 // remove_persons(name, surname, weight, height,age); 對於您顯示名稱的其他問題,代碼在這里(我提到了已完成更正的位置):
#include <iostream>
#include <string>
using namespace std;
int N;
void remove_persons(string name[], string surname[], float weight[],
float height[], float age[])
{
for (int i = 0; i < N; i++)
{ if (age[i] < 18 || height[i] < 140 || height[i] > 220)
{
for (int j = i; j < N - 1; j++)
{
name[j] = name[j + 1];
surname[j] = surname[j + 1];
age[j] = age[j + 1];
height[j] = height[j + 1];
weight[j] = weight[j + 1];
}
N--;
i--;
}
}
}
//correction
void fat_and_thin(string name[], string surname[], float weight[])
{
int fat, thin;
fat = thin = weight[0];
//correction
int j = 0;
int k = 0;
for (int i = 1; i < N; i++)
{
if (weight[i] < thin)
{
thin = weight[i];
j = i;
}
if (weight[i] > fat)
{
fat = weight[i];
k = i;
}
}
//correction
cout <<name[j]<<surname[j]<<" is The thinest has : " << thin << endl;
cout << name[k] << surname[k] << " is The fatest has : " << fat <<endl;
}
int main()
{
string name[99];
string surname[99];
float weight[99];
float height[99];
float age[99];
do
{
cout << "Nr. of participants 1-30: ";
cin >> N;
} while (N <= 0 || N >= 30);
for (int i = 0; i < N; i++)
{
cout << "Name[" << i << "]: ";
cin >> name[i];
cout << "Surname[" << i << "]: ";
cin >> surname[i];
do
{
cout << "Age[" << i << "] (>18 years): ";
cin >> age[i];
} while (age[i] <= 18);
cout << "Height in cm [" << i << "] (>140 & <220 cm): ";
cin >> height[i];
cout << "Weight [" << i << "]): ";
cin >> weight[i];
}
//correction
remove_persons(name, surname, weight, height,age);
fat_and_thin(name, surname, weight);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.