c++ 如何找到最大值和最小值

xiozqbni  于 2023-06-25  发布在  其他
关注(0)|答案(4)|浏览(219)

我想找出一个函数中的最大值和最小值。我试过了,但我只得到了最小的价值。这是因为代码不是从第一行读取的,而第一行包含最高值。这是我的代码:

  1. void price(ifstream& infile) {
  2. infile.clear();
  3. infile.seekg(0);
  4. int year, mall, cafe, expenses;
  5. infile >> year >> mall >> cafe >> expenses;
  6. int high_mall{}, high_year{}, high_expenses{};
  7. int low_mall{}, low_year{}, low_expenses{};
  8. low_mall = mall;
  9. low_year = year;
  10. low_expenses = expenses;
  11. for (int year{}, mall{}, cafe{}, expenses{};
  12. nameFile >> year >> mall >> cafe >> expenses;) {
  13. if (expenses > high_expenses) {
  14. high_expenses = expenses;
  15. high_year = year;
  16. high_mall = mall;
  17. }
  18. if (expenses < low_expenses) {
  19. low_expenses = expenses;
  20. low_year = year;
  21. low_mall = mall;
  22. }
  23. }
  24. cout << "Highest expenses are " << high_expenses << "at" << high_year << endl;
  25. cout << "Total mall that year are " << high_mall << endl;
  26. cout << "Lowest expenses are " << low_expenses << "at" << low_year << endl;
  27. cout << "Total mall that year are " << low_mall << endl;
  28. }

这是我的代码。我尝试删除第一个内联,并得到最高值,但最小值变为0。有人知道怎么修吗?我在想std::numeric_limits。但是我能不使用它来解决这个问题吗?

ozxc1zmp

ozxc1zmp1#

因为low_expenses的初始值为零,所以你永远找不到任何一年的费用更低,除非费用是负的。你应该初始化你的low_expenses为最大可能的整数,如下所示:

  1. low_expenses{ std::numeric_limits<int>::max() };

如果开销可以为负,那么将high_expenses初始化为最低可能值也会有所帮助:

  1. high_expenses{ std::numeric_limits<int>::min() };

您需要包含<limits>头才能工作。
此外,正如WhozCraig所指出的,这条线

  1. infile >> year >> mall >> cafe >> expenses;

将丢弃一行输入。这是不必要的。您可以将low_...high_...初始化为第一行的值,但随后可能还必须检查第一个I/O操作是否成功。(如果没有行)

t5fffqht

t5fffqht2#

初始化你的最高和最低的内容后,初始读取之前,进入循环读取其余部分。
示例:

  1. void price(ifstream &infile)
  2. {
  3. infile.clear();
  4. infile.seekg(0);
  5. int year, mall, cafe, expenses;
  6. if (infile >> year >> mall >> cafe >> expenses)
  7. {
  8. int high_mall{mall}, high_year{year}, high_expenses{expenses};
  9. int low_mall{mall}, low_year{year}, low_expenses{expenses};
  10. while (infile >> year >> mall >> cafe >> expenses)
  11. {
  12. if (expenses > high_expenses)
  13. {
  14. high_expenses = expenses;
  15. high_year = year;
  16. high_mall = mall;
  17. }
  18. else if (expenses < low_expenses)
  19. {
  20. low_expenses = expenses;
  21. low_year = year;
  22. low_mall = mall;
  23. }
  24. }
  25. cout << "Highest expenses are " << high_expenses << "at" << high_year << endl;
  26. cout << "Total mall that year are " << high_mall << endl;
  27. cout << "Lowest expenses are " << low_expenses << "at" << low_year << endl;
  28. cout << "Total mall that year are " << low_mall << endl;
  29. }
  30. }
展开查看全部
k97glaaz

k97glaaz3#

一个好的做法是使用标准库已经提供的thin。std::minmax_element正是你所需要的。
也可以将stream转换为某些特定数据的迭代器:std::istream_iterator
这两件事可以组合成下面的代码:

  1. struct Data
  2. {
  3. int year, mall, cafe, expenses;
  4. };
  5. std::istream& operator>>(std::istream& in, Data& data)
  6. {
  7. return in >> data.year >> data.mall >> data.cafe >> data.expenses;
  8. }
  9. std::ostream& operator<<(std::ostream& out, const Data& data)
  10. {
  11. return out << data.year << ", " << data.mall << ", " << data.cafe << ", " << data.expenses;
  12. }
  13. int main()
  14. {
  15. auto result = std::minmax_element(std::istream_iterator<Data>{std::cin}, {},
  16. [](const auto& a, const auto& b) { return a.expenses < b.expenses; });
  17. std::cout << *result.first << '\n';
  18. std::cout << *result.second << '\n';
  19. return 0;
  20. }

https://godbolt.org/z/EMvjx5Yrq

展开查看全部
9w11ddsr

9w11ddsr4#

我试了一下,效果很好

  1. void price (ifstream & infile)
  2. {
  3. infile.clear();
  4. infile.seekg(0);
  5. int year, mall, cafe, expenses;
  6. int high_mall {} , high_year {}, high_expenses {};
  7. int low_mall;
  8. int low_year;
  9. int low_expenses;
  10. for (int year {}, mall {}, cafe {}, expenses {};
  11. nameFile >> year >> mall >> cafe >> expenses; )
  12. {
  13. if (expenses > high_expenses)
  14. {
  15. high_expenses = expenses;
  16. high_year = year;
  17. high_mall = mall;
  18. }
  19. if (expenses < low_expenses)
  20. {
  21. low_expenses = expenses;
  22. low_year = year;
  23. low_mall = mall;
  24. }
  25. }
  26. cout << "Highest expenses are " << high_expenses << "at" << high_year << endl;
  27. cout << "Total mall that year are " << high_mall << endl ;
  28. cout << "Lowest expenses are " << low_expenses << "at" << low_year << endl;
  29. cout << "Total mall that year are " << low_mall << endl ;
  30. }
展开查看全部

相关问题