当输入小于10时,如何设计进度条逻辑?

huangapple go评论154阅读模式
英文:

How to design a progress bar logic when inputs are less than 10?

问题

我正在解析一个数组中的字符串,并在解析过程中显示进度。这是我的逻辑,但对于少于10个输入的情况,它不可扩展。

在函数的初始部分已经处理了除以零的情况,即100*i/(lineLen-1)。

进度 := 0
for i := 0; i < lineLen; i++ {
//.. lineLen = 数组长度
//.....字符串处理...
if (100*i/(lineLen-1)) >= progress {
fmt.Printf("--%d%s--", progress, "%")
progress += 10
}
}

英文:

I am parsing strings from an array and showing progress as the strings are parsed. This is my logic but it doesn't scale for inputs less than 10.

Divide by zero is already taken care of during the initial part of the function for 100*i/(lineLen-1)

progress := 0
for i:= 0; i&lt;lineLen;i++ {
 //.. lineLen = array length
//.....String processing...
if (100*i/(lineLen-1)) &gt;= progress {
     fmt.Printf(&quot;--%d%s--&quot;, progress, &quot;%&quot;)
     progress += 10
}
}

答案1

得分: 1

我理解你需要将所有百分比向下取整为10的倍数。

你可以尝试以下代码:

lineLen := 4
progress := 0
for i := 0; i < lineLen; i++ {
	// 向下取整到最接近的10的倍数。
	actualProgress := (100 * (i+1) / lineLen)
	if actualProgress >= progress {
		roundedProgress := (actualProgress / 10) * 10
		// 条件确保前一个进度百分比不会重复。
		if roundedProgress != progress{
			progress = roundedProgress
			fmt.Printf("--%d%s--", progress, "%")
		}
	}
}

链接

英文:

I understand that you need to floor all the percentages to a multiple of 10.

You could try something like the following.

lineLen := 4
progress := 0
for i := 0; i &lt; lineLen; i++ {
	// Rounding down to the nearest multiple of 10.
	actualProgress := (100 * (i+1) / lineLen)
	if actualProgress &gt;= progress {
		roundedProgress := (actualProgress / 10) * 10
		// Condition to make sure the previous progress percentage is not repeated.
		if roundedProgress != progress{
			progress = roundedProgress
			fmt.Printf(&quot;--%d%s--&quot;, progress, &quot;%&quot;)
		}
	}
}

Link

答案2

得分: 1

请看 https://play.golang.org/p/xtRtk1T_ZW(下面是代码):

func main() {
    // outputMax 是要打印的进度条项数,不包括完成度为100%的项。
    // 总会至少输出两个项:0% 和 100%。
    outputMax := 10

    for lineLen := 1; lineLen < 200; lineLen++ {
        fmt.Printf("lineLen=%-3d    ", lineLen)
        printProgress(lineLen, outputMax)
    }
}

// 计算当前进度。
func progress(current, max int) int {
    return 100 * current / max
}

// 计算组中的项数。
func groupItems(total, limit int) int {
    v := total / limit
    if total%limit != 0 {
        v++
    }
    return v
}

// 打印进度条。
func printProgress(lineLen, outputMax int) {
    itemsPerGroup := groupItems(lineLen, outputMax)
    for i := 0; i < lineLen; i++ {
        if i%itemsPerGroup == 0 {
            fmt.Printf("--%d%%--", progress(i, lineLen))
        }
    }
    fmt.Println("--100%--")
}

如果你愿意,你可以使用 https://play.golang.org/p/aR6coeLhAkoutputMaxlineLen 的各种值进行循环,以查看你喜欢的 outputMax 值(我认为 8 <= outputMax < 13 看起来最好)。默认情况下,禁用了进度条的输出,但你可以在 main 函数中轻松启用它。

英文:

Have a look at https://play.golang.org/p/xtRtk1T_ZW (code reproduced below):

func main() {
	// outputMax is the number of progress items to print, excluding the 100% completion item.
	// There will always be at least 2 items output: 0% and 100%.
	outputMax := 10

	for lineLen := 1; lineLen &lt; 200; lineLen++ {
		fmt.Printf(&quot;lineLen=%-3d    &quot;, lineLen)
		printProgress(lineLen, outputMax)
	}
}

// Calculate the current progress.
func progress(current, max int) int {
	return 100 * current / max
}

// Calculate the number of items in a group.
func groupItems(total, limit int) int {
	v := total / limit
	if total%limit != 0 {
		v++
	}
	return v
}

// Print the progress bar.
func printProgress(lineLen, outputMax int) {
	itemsPerGroup := groupItems(lineLen, outputMax)
	for i := 0; i &lt; lineLen; i++ {
		if i%itemsPerGroup == 0 {
			fmt.Printf(&quot;--%d%%--&quot;, progress(i, lineLen))
		}
	}
	fmt.Println(&quot;--100%--&quot;)
}

If you want, you can execute a loop over various values of outputMax and lineLen using https://play.golang.org/p/aR6coeLhAk to see which value for outputMax you like (8 &lt;= outputMax &lt; 13 looks best to me). Output of the progress bar is disabled by default, but you can easily enable it in main.

huangapple
  • 本文由 发表于 2017年7月9日 02:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/44989533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定