我的函数中的百分比计算不起作用
percentage calculation in my function is not working
所以我想做的是获取一个数字向量,找出每个元素出现的次数,然后计算其在向量中出现的次数的等效百分比。
它在只有一个数字的几个元素的向量上工作得很好,但在多次出现不同元素(准确地说是 2 个)的向量上则完全没有。
这里有一个详细的输出:
count = 6
pushed back 21
total = 6
percent = 6 / 6* 100
pushed back 100
Number:
21
Percent Chance
100
count = 5
pushed back 42
count = 5
pushed back 21
total = 5
total = 10
percent = 5 / 10* 100
pushed back 0
percent = 5 / 10* 100
pushed back 0
Number:
42
Percent Chance
0 // SHOULD BE 50
Number:
21
Percent Chance
0 // SHOULD BE 50
代码如下:(输出来自此函数 运行 两次,使用不同的 x 向量)
std::vector <std::string> findpercentages(std::vector <std::string> x)
{
int count{}; //for amount of times element occurs
std::vector <std::string> tempvec{}; //to hold the element/s
std::vector <int> counts{}; //to hold count for each element in tempvec
std::vector <std::string> finalvec{}; //elements and counts of elements
for (size_t i = 0; i < x.size(); i++)
{
//look for x[element] in tempvec, if not in tempvec, count occurances in x and add element to
// tempvec, add count to counts
std::vector <std::string>::iterator point { std::find(tempvec.begin(), tempvec.end(), x[i]) };
if (point == tempvec.end())
{
count = std::count(x.begin(), x.end(), x[i]);
std::cout << "count = " << count << '\n';
counts.push_back(count);
std::cout << "pushed back " << x[i] << '\n';
tempvec.push_back(x[i]);
}
}
int total{};
for (size_t n = 0; n < counts.size(); n++)
{
total += counts[n]; //total for percentage calculation
std::cout << "total = " << total << '\n';
}
for (size_t y = 0; y < tempvec.size(); y++)
{
finalvec.push_back(tempvec[y]);
//percent calculation. used unsigned_int64 because got arithmatic overflow warning if i just use
// int
unsigned __int64 percentage = static_cast <unsigned __int64> (round((counts[y] / total) * 100));
std::cout << "percent = " << counts[y] << " / " << total << "* 100" << '\n';
finalvec.push_back(std::to_string(percentage));
std::cout << "pushed back " << percentage << '\n';
}
return finalvec;
}
unsigned __int64 percentage = static_cast <unsigned __int64> (round((counts[y] / total) * 100));
应该是这样的
double percentage = round((100.0 * counts[y]) / total);
你的问题是,当你用一个整数除以另一个整数时,你总是得到一个整数。所以 (counts[y] / total)
总是 0
或 1
.
所以最简单的解决办法就是在计算中引入一个double
值100.0
。这确保您得到浮点除法而不是整数除法。
所以我想做的是获取一个数字向量,找出每个元素出现的次数,然后计算其在向量中出现的次数的等效百分比。
它在只有一个数字的几个元素的向量上工作得很好,但在多次出现不同元素(准确地说是 2 个)的向量上则完全没有。
这里有一个详细的输出:
count = 6
pushed back 21
total = 6
percent = 6 / 6* 100
pushed back 100
Number:
21
Percent Chance
100
count = 5
pushed back 42
count = 5
pushed back 21
total = 5
total = 10
percent = 5 / 10* 100
pushed back 0
percent = 5 / 10* 100
pushed back 0
Number:
42
Percent Chance
0 // SHOULD BE 50
Number:
21
Percent Chance
0 // SHOULD BE 50
代码如下:(输出来自此函数 运行 两次,使用不同的 x 向量)
std::vector <std::string> findpercentages(std::vector <std::string> x)
{
int count{}; //for amount of times element occurs
std::vector <std::string> tempvec{}; //to hold the element/s
std::vector <int> counts{}; //to hold count for each element in tempvec
std::vector <std::string> finalvec{}; //elements and counts of elements
for (size_t i = 0; i < x.size(); i++)
{
//look for x[element] in tempvec, if not in tempvec, count occurances in x and add element to
// tempvec, add count to counts
std::vector <std::string>::iterator point { std::find(tempvec.begin(), tempvec.end(), x[i]) };
if (point == tempvec.end())
{
count = std::count(x.begin(), x.end(), x[i]);
std::cout << "count = " << count << '\n';
counts.push_back(count);
std::cout << "pushed back " << x[i] << '\n';
tempvec.push_back(x[i]);
}
}
int total{};
for (size_t n = 0; n < counts.size(); n++)
{
total += counts[n]; //total for percentage calculation
std::cout << "total = " << total << '\n';
}
for (size_t y = 0; y < tempvec.size(); y++)
{
finalvec.push_back(tempvec[y]);
//percent calculation. used unsigned_int64 because got arithmatic overflow warning if i just use
// int
unsigned __int64 percentage = static_cast <unsigned __int64> (round((counts[y] / total) * 100));
std::cout << "percent = " << counts[y] << " / " << total << "* 100" << '\n';
finalvec.push_back(std::to_string(percentage));
std::cout << "pushed back " << percentage << '\n';
}
return finalvec;
}
unsigned __int64 percentage = static_cast <unsigned __int64> (round((counts[y] / total) * 100));
应该是这样的
double percentage = round((100.0 * counts[y]) / total);
你的问题是,当你用一个整数除以另一个整数时,你总是得到一个整数。所以 (counts[y] / total)
总是 0
或 1
.
所以最简单的解决办法就是在计算中引入一个double
值100.0
。这确保您得到浮点除法而不是整数除法。