更新进度条C语言实现

受了李大神这篇文章的启发:http://blog.csdn.net/absurd/article/details/761943

于是我也自己亲手实现了一个简易版本的更新进度条,只要传入一个大于100的整型数即可。

#include <stdio.h>
#include <Windows.h>
#include <unistd.h>
//定义设置光标结构体变量  
CONSOLE_CURSOR_INFO cci;   
//定义默认的坐标位置       
COORD pos = {0,0};       
HANDLE hOut;    
/*
	10000
	/10000%1000
	/1000%100
	/100%10
	%10
*/
#define AEF  0x8
#define exp  1.39
#define TITLE "进度条"
typedef void (*work_ptr)(int * , int *);
void show_process(int *cnt , int *num)
{
	int flag = 1 ;
	int tmp = *num ;
	int tmp1 = *cnt ;
	int multiple = 0;
	static int _flag  ;
	static int count = 0 ;
	if(tmp1 >= 100) {
		multiple = (int)(tmp1/100*100/100);
		flag = 1 ;
	}
	SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | 0x8);
	pos.X = 73 ;
	pos.Y = 15 ;
	SetConsoleCursorPosition(hOut,pos);
	if(flag)
	{
		printf("%d%%",tmp/multiple);
		
		SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | 0x8);
		pos.X = 76 ;
		pos.Y = 15 ;
		SetConsoleCursorPosition(hOut,pos);
		switch(_flag)
		{
			case 0: printf("[%c]",'\\'); _flag = 1 ; break ;
			case 1: printf("[%c]",'/');  _flag = 0 ; break ;
		}
		SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | AEF);
		pos.X = (int)((tmp/multiple)/exp);
		pos.Y = 15 ;
		SetConsoleCursorPosition(hOut,pos);
		printf("=>") ;
	}
	return ; 
} 

void run_go(int cnt ,work_ptr ptr)
{
	int i ;
	int count = 0 ;
	cnt = cnt/100*100;
	if(cnt < 100) return ;
	for(i = 0 ; i < cnt ; i++)
	{
		count++ ;
		ptr(&cnt,&count);
		sleep(1);
		//usleep(1111000);
	}
}

int main(void)
{
	hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
	GetConsoleCursorInfo(hOut, &cci); 
	SetConsoleTitleA(TITLE);  
	cci.bVisible =  0;    
	SetConsoleCursorInfo(hOut, &cci); 
	run_go(111,show_process);
	system("cls");
	run_go(132,show_process);
	system("cls");
	run_go(198,show_process);
	system("cls");
	run_go(325,show_process);
	system("cls");
	run_go(546,show_process);
	return 0 ;	
} 

运行结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值