我需要帮助。我正在编写这个程序,需要计算。csv文件中两个int之间的差异。找到差异很容易,但当我计算差异列表时,它们没有特定的顺序。我想挑出最大的差值,并具体计算出来。我该怎么做呢?
double diff;
int astate;
string line8;
ifstream myfile8 ("elect12.csv");
//cout << endl << "Total Popular Votes for Other Candidates: ";
while (getline (myfile8, line))
{
istringstream iss(line);
int a, b, c, d, e;
if (!(iss >> a >> b >> c >> d >> e )) //{ break; }
diff = (a-b);
diff = diff/d;
diff = diff*100;
astate = diff;
/*cout << "Obama's best state was " << line8 << ", where he won by " << diff << " points." << endl;*/
cout << astate << endl;
差异被转换成百分比。现在,a &B正在被退回。我如何只返回最大的差异?
如果这没有意义,我很抱歉。
将当前差值与之前的最大差值(存储在初始化的变量astate
中)进行比较,如果比之前的最大差值大,则赋值。在循环结束后打印astate
的值