[英]How to find the highest and smallest value
我想在一個函數中找到最大值和最小值。 我試過了,但是我只得到了最小值。 這是因為代碼不是從第一行讀取的,並且第一行包含最高值。 這是我的代碼:
void price (ifstream & infile)
{
infile.clear();
infile.seekg(0);
int year, mall, cafe, expenses;
infile >> year >> mall >> cafe >> expenses;
int high_mall {} , high_year {}, high_expenses {};
int low_mall {} , low_year {}, low_expenses {};
low_mall = mall;
low_year = year;
low_expenses = expenses;
for (int year {}, mall {}, cafe {}, expenses {};
nameFile >> year >> mall >> cafe >> expenses; )
{
if (expenses > high_expenses)
{
high_expenses = expenses;
high_year = year;
high_mall = mall;
}
if (expenses < low_expenses)
{
low_expenses = expenses;
low_year = year;
low_mall = mall;
}
}
cout << "Highest expenses are " << high_expenses << "at" << high_year << endl;
cout << "Total mall that year are " << high_mall << endl ;
cout << "Lowest expenses are " << low_expenses << "at" << low_year << endl;
cout << "Total mall that year are " << low_mall << endl ;
}
這是我的代碼。 我試圖刪除第一個內聯並獲得最高值但最小值變為 0。有誰知道如何解決它? 我在考慮std::numeric_limits
。 但是我可以不使用它來解決這個問題嗎?
由於low_expenses
的初始值為零,您將永遠找不到任何費用較低的年份,除非費用為負數。 您應該將low_expenses
初始化為最大可能的整數,如下所示:
low_expenses{ std::numeric_limits<int>::max() };
如果費用可能是負數,將high_expenses
初始化為盡可能低的值也有幫助:
high_expenses{ std::numeric_limits<int>::min() };
您需要包含<limits>
標頭才能使其工作。
此外,正如 WhozCraig 指出的那樣,這條線
infile >> year >> mall >> cafe >> expenses;
將丟棄一行輸入。 沒有必要。 您可以將low_...
和high_...
初始化為第一行的值,但是您可能還需要檢查第一個 I/O 操作是否成功。 (如果沒有行)
在進入循環讀取其余內容之前,在初始讀取后初始化最高和最低內容。
例子:
void price(ifstream &infile)
{
infile.clear();
infile.seekg(0);
int year, mall, cafe, expenses;
if (infile >> year >> mall >> cafe >> expenses)
{
int high_mall{mall}, high_year{year}, high_expenses{expenses};
int low_mall{mall}, low_year{year}, low_expenses{expenses};
while (infile >> year >> mall >> cafe >> expenses)
{
if (expenses > high_expenses)
{
high_expenses = expenses;
high_year = year;
high_mall = mall;
}
else if (expenses < low_expenses)
{
low_expenses = expenses;
low_year = year;
low_mall = mall;
}
}
cout << "Highest expenses are " << high_expenses << "at" << high_year << endl;
cout << "Total mall that year are " << high_mall << endl;
cout << "Lowest expenses are " << low_expenses << "at" << low_year << endl;
cout << "Total mall that year are " << low_mall << endl;
}
}
一個好的做法是使用標准庫已經提供的 Thins。 有std::minmax_element可以完全滿足您的需求。
也可以將流轉換為某些特定數據的迭代器: std::istream_iterator 。
這兩件事可以組合成以下代碼:
struct Data
{
int year, mall, cafe, expenses;
};
std::istream& operator>>(std::istream& in, Data& data)
{
return in >> data.year >> data.mall >> data.cafe >> data.expenses;
}
std::ostream& operator<<(std::ostream& out, const Data& data)
{
return out << data.year << ", " << data.mall << ", " << data.cafe << ", " << data.expenses;
}
int main()
{
auto result = std::minmax_element(std::istream_iterator<Data>{std::cin}, {},
[](const auto& a, const auto& b) { return a.expenses < b.expenses; });
std::cout << *result.first << '\n';
std::cout << *result.second << '\n';
return 0;
}
我試試這個,它起作用了
void price (ifstream & infile)
{
infile.clear();
infile.seekg(0);
int year, mall, cafe, expenses;
int high_mall {} , high_year {}, high_expenses {};
int low_mall;
int low_year;
int low_expenses;
for (int year {}, mall {}, cafe {}, expenses {};
nameFile >> year >> mall >> cafe >> expenses; )
{
if (expenses > high_expenses)
{
high_expenses = expenses;
high_year = year;
high_mall = mall;
}
if (expenses < low_expenses)
{
low_expenses = expenses;
low_year = year;
low_mall = mall;
}
}
cout << "Highest expenses are " << high_expenses << "at" << high_year << endl;
cout << "Total mall that year are " << high_mall << endl ;
cout << "Lowest expenses are " << low_expenses << "at" << low_year << endl;
cout << "Total mall that year are " << low_mall << endl ;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.