
在線求助C++大佬,可否幫忙敲一個這個程式。
uj5u.com熱心網友回復:
#include<iostream>#include<string>
#include<iomanip>
using namespace std;
#define WIDTH 10
struct EpidemicSituation
{
string nation; //地區
int add; //新增病例
int cumulative; //累計確診
int cure; //治愈數
int death; //死亡數
string getNation() { return nation; }
int getAdd() { return add; }
int getCumulative() { return cumulative; }
int getCure() { return cure; }
int getDeath() { return death; }
double getCureRate() { return cure * 1.0 / cumulative * 100; }
double getDeathRate() { return death * 1.0 / cumulative * 100; }
EpidemicSituation() = default;
~EpidemicSituation() = default;
EpidemicSituation(string s,int add,int cumu,int cure,int death)
:nation(s),add(add),cumulative(cumu),cure(cure),death(death)
{}
EpidemicSituation(const EpidemicSituation& es) = default;
ostream& readNation(ostream &os) //輸出地區病例資訊
{
os << setw(WIDTH) << nation
<< setw(WIDTH) << add
<< setw(WIDTH) << cumulative
<< setw(WIDTH) << cure
<< setw(WIDTH) << fixed << setprecision(2) << cure * 1.0 / cumulative * 100
<< setw(WIDTH) << death
<< setw(WIDTH) << death * 1.0 / cumulative * 100;
os.unsetf(ios::fixed);
os << setprecision(6);
return os;
}
};
ostream& printNations(ostream &os, EpidemicSituation *p,size_t n)
{
os << setw(WIDTH) << "地區"
<< setw(WIDTH) << "新增確診"
<< setw(WIDTH) << "累計確診"
<< setw(WIDTH) << "治愈"
<< setw(WIDTH) << "治愈率(%)"
<< setw(WIDTH) << "死亡"
<< setw(WIDTH) << "死亡率(%)" << endl;
for (size_t i = 0; i < n; ++i)
{
p->readNation(os) << endl;
++p;
}
return os;
}
//交換
void swap(EpidemicSituation &pn1, EpidemicSituation &pn2)
{
EpidemicSituation pn = pn1;
pn1 = pn2;
pn2 = pn;
}
//根據選擇的欄位進行排序
template<typename T>
void sortNation(EpidemicSituation *pse, size_t n, T (EpidemicSituation::*f)()) //f指向成員函式
{
//使用冒泡排序
for (size_t i = 0; i < n - 1; i++)
{
for (size_t j = n - 1; j > i; j--)
{
if ((pse[j].*f)() > (pse[j - 1].*f)())
{
swap(pse[j], pse[j - 1]);
}
}
}
}
int main()
{
EpidemicSituation CONV19[100];
size_t number = 0; //地區數
cout << "請按下列順序輸入各地疫情,輸入結束在地區中輸入“null”并回車" << endl;
cout << setw(WIDTH) << "地區"
<< setw(WIDTH) << "新增確診"
<< setw(WIDTH) << "累計確診"
<< setw(WIDTH) << "治愈"
<< setw(WIDTH) << "死亡"<< endl;
string nation; //地區
int add; //新增病例
int cumulative; //累計確診
int cure; //治愈數
int death; //死亡數
//輸入各地疫情
while (cin >> nation )
{
if (nation == "null")
break;
cin >> add >> cumulative >> cure >> death;
CONV19[number].nation = nation;
CONV19[number].add = add;
CONV19[number].cumulative = cumulative;
CONV19[number].cure = cure;
CONV19[number].death = death;
++number;
}
cout << "按照哪個欄位進行排序?請輸入相應欄位名,可選:" <<
"新增確診、累計確診、治愈、治愈率、死亡、死亡率" << endl;
string str;
cin >> str;
if (str == "新增確診")
sortNation(CONV19, number, &EpidemicSituation::getAdd);
else if (str == "累計確診")
sortNation(CONV19, number, &EpidemicSituation::getCumulative);
else if (str == "治愈")
sortNation(CONV19, number, &EpidemicSituation::getCure);
else if (str == "治愈率")
sortNation(CONV19, number, &EpidemicSituation::getCureRate);
else if (str == "死亡")
sortNation(CONV19, number, &EpidemicSituation::getDeath);
else if (str == "死亡率")
sortNation(CONV19, number, &EpidemicSituation::getDeathRate);
else
cout << "error" << endl;
printNations(cout, CONV19, number);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/74511.html
標籤:C++ 語言
上一篇:麻煩大佬幫我看一下我這個代碼,主函式的第一個for回圈為什么運行不了
下一篇:多行程管道通信
