2012-10-12 216 views
0

这应该是比较简单的。显示进度百分比

什么我有:

int progress; // iteration we are on, starts at 0, always incr by 1 
int percentage; // from 1 - 100, starts at 0 
int percentage_trip; // derived from total_results/100 
int total_results; // lets say its 200 (note that it can be anything, like 939049) 

我需要:

在上面的例子中,每个时间进展命中2,4,6,8等的百分比整数应该增加1(即百分比++;)

我的想法围绕着格式化嵌套的IF语句,我是looki这样做的正确方法。

+3

百分比不应该是'(progress/total_results)* 100'吗? – Danny

+3

@Danny:整数算术需要'(progress * 100/total_results)'(希望没有溢出) - 你拥有的总是零,直到progress = total_results。 –

+0

@AustinSalonen true。没有想到这一点。 – Danny

回答

3
int incrementAmount = total_results/100; 
if (progress % incrementAmount) { percentage++; } 
+0

假设total_results> 200并且不是一些可笑的大数字(或否定的,我们假设它不是),这是行得通的。我不确定模数评估,但是我会在条件语句中添加'== 0'。 如果总数少于200,则必须有一些自定义逻辑以避免奇怪的行为。 – Devin

+0

通常情况下,total_results将介于20,000 - 60,000之间 –

+0

在这种情况下,这可行。 – Devin