更新进度条C语言实现

来源:互联网 发布:slackware linux 编辑:程序博客网 时间:2024/06/10 03:35

受了李大神这篇文章的启发: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 ;} 

运行结果:




原创粉丝点击