📜  如何在C / C++中创建命令行进度条

📅  最后修改于: 2021-05-28 03:34:42             🧑  作者: Mango

任务是编写一个C / C++程序来绘制命令行进度条。

方法:创建进度条的想法是使用system()函数,该函数将提供彩色输出。下面是如何使用system()函数。

系统函数接受以下参数为输出屏幕着色:

  1. 关键字:颜色
  2. 背景颜色
  3. 前景色

颜色代码:

Color Color Code
BLACK                    0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHT GRAY 7
DARK GRAY 8
LIGHT BLUE 9
LIGHT GREEN 10
LIGHT CYAN 11
LIGHT RED 12
LIGHT MAGENTA 13
YELLOW 14
WHITE 15
BRIGHT GREEN A
BRIGHT CYAN B
BRIGHT RED C
BRIGHT MAGENTA D
BRIGHT YELLOW E
WHITE F

句法:

下面是在C / C++中的命令行中绘制进度条的程序:

C
// C program to create loading bar
#include 
#include 
  
// Function to creating loading bar
void loadingBar()
{
    // 0 - black background,
    // A - Green Foreground
    system("color 0A");
  
    // Initialize char for printing
    // loading bar
    char a = 177, b = 219;
  
    printf("\n\n\n\n");
    printf("\n\n\n\n\t\t\t\t\t"
           + "Loading...\n\n");
    printf("\t\t\t\t\t");
  
    // Print initial loading bar
    for (int i = 0; i < 26; i++)
        printf("%c", a);
  
    // Set the cursor again starting
    // point of loading bar
    printf("\r");
    printf("\t\t\t\t\t");
  
    // Print loading bar progress
    for (int i = 0; i < 26; i++) {
        printf("%c", b);
  
        // Sleep for 1 second
        Sleep(1000);
    }
}
  
// Driver Code
int main()
{
    // Function Call
    loadingBar();
    return 0;
}


C++
// C++ program to create loading bar
#include 
#include 
using namespace std;
  
// Function to creating loading bar
void loadingBar()
{
    // 0 - black background,
    // A - Green Foreground
    system("color 0A");
  
    // Initialize char for printing
    // loading bar
    char a = 177, b = 219;
  
    printf("\n\n\n\n");
    printf("\n\n\n\n\t\t\t\t\t"
           + "Loading...\n\n");
    printf("\t\t\t\t\t");
  
    // Print initial loading bar
    for (int i = 0; i < 26; i++)
        printf("%c", a);
  
    // Set the cursor again starting
    // point of loading bar
    printf("\r");
    printf("\t\t\t\t\t");
  
    // Print loading bar progress
    for (int i = 0; i < 26; i++) {
        printf("%c", b);
  
        // Sleep for 1 second
        Sleep(1000);
    }
}
  
// Driver Code
int main()
{
    // Function Call
    loadingBar();
    return 0;
}


输出:
以下是上述程序的输出:

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。