showprogress.c

来自「信道编解码中的turbo编解码程序」· C语言 代码 · 共 75 行

C
75
字号
/*
*******************************************************************************
* ansi c source code
* file name:
*	ShowProgress.c
* abstract:
*	to show the progress of a task
* reference:
*	
* author:
*	Wang Jiaheng	2004-10-13
* revision history:
*	Wang Jiaheng	2004-10-13	original version
*******************************************************************************
*/


/*
*******************************************************************************
*                               include files
*******************************************************************************
*/
#include <stdio.h>


/*
*******************************************************************************
*                            function definition
*******************************************************************************
*/
/*
******************************************************************************* 
* description:
*	show the progress of a task by means of drawing a progress bar
* input: 
*	present_num: current times of execution (1 to total_num)
*	total_num: total times of execution
* output:
*	none
* function reference:
* author:
*	Wang Jiaheng, 2004-10-13	created
*******************************************************************************
*/
void ShowProgress(int present_num, int total_num)
{
	int	 percentage;
	char progress_bar[51];
	int  bar_num;
	char end_figure[4] = {'\x5C', '\x7C', '\x2F', '\x2D'};
	int  m;

	if ( present_num == 1 )
	{
		printf("Mission Starting ...\n");
	}

	percentage = 100 * present_num / total_num;
	bar_num = percentage / 2;

	for (m = 0; m < bar_num; m++) progress_bar[m] = '\x10';
	for (; m < 50; m++) progress_bar[m] = ' ';
	progress_bar[50] = '\0';

	putchar('\r');
	printf("%s", progress_bar);
	putchar(end_figure[present_num%4]);
	printf(" %3d%%", percentage);

	if ( present_num == total_num )
	{
		printf("\nMission Accomplished\n");
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?