程序代码:
#include <stdio.h>
#include <windows.h>
#define USER_INPUT_MAX_LENGTH 64
typedef struct
{
char* unitLine[7];
} Digit;
static Digit digit[10];
void printfDigit(Digit _digit, HANDLE _handle, COORD _pos)
{
for (int i=0; i<7;i++)
{
//纵向打印。X 坐标固定,Y 坐标递增
printf("%s", _digit.unitLine[i]);
_pos.Y++;
SetConsoleCursorPosition(_handle, _pos);
Sleep(10);
}
}
void InitDigit()
{
//0
digit[0].unitLine[0] = "****";
digit[0].unitLine[1] = "* *";
digit[0].unitLine[2] = "* *";
digit[0].unitLine[3] = "* *";
digit[0].unitLine[4] = "* *";
digit[0].unitLine[5] = "* *";
digit[0].unitLine[6] = "****";
//1
digit[1].unitLine[0] = " *";
digit[1].unitLine[1] = " *";
digit[1].unitLine[2] = " *";
digit[1].unitLine[3] = " *";
digit[1].unitLine[4] = " *";
digit[1].unitLine[5] = " *";
digit[1].unitLine[6] = " *";
//2
digit[2].unitLine[0] = "****";
digit[2].unitLine[1] = " *";
digit[2].unitLine[2] = " *";
digit[2].unitLine[3] = "****";
digit[2].unitLine[4] = "* ";
digit[2].unitLine[5] = "* ";
digit[2].unitLine[6] = "****";
//3
digit[3].unitLine[0] = "****";
digit[3].unitLine[1] = " *";
digit[3].unitLine[2] = " *";
digit[3].unitLine[3] = "****";
digit[3].unitLine[4] = " *";
digit[3].unitLine[5] = " *";
digit[3].unitLine[6] = "****";
//4
digit[4].unitLine[0] = "* *";
digit[4].unitLine[1] = "* *";
digit[4].unitLine[2] = "* *";
digit[4].unitLine[3] = "****";
digit[4].unitLine[4] = " *";
digit[4].unitLine[5] = " *";
digit[4].unitLine[6] = " *";
//5
digit[5].unitLine[0] = "****";
digit[5].unitLine[1] = "* ";
digit[5].unitLine[2] = "* ";
digit[5].unitLine[3] = "****";
digit[5].unitLine[4] = " *";
digit[5].unitLine[5] = " *";
digit[5].unitLine[6] = "****";
//6
digit[6].unitLine[0] = "****";
digit[6].unitLine[1] = "* ";
digit[6].unitLine[2] = "* ";
digit[6].unitLine[3] = "****";
digit[6].unitLine[4] = "* *";
digit[6].unitLine[5] = "* *";
digit[6].unitLine[6] = "****";
//7
digit[7].unitLine[0] = "****";
digit[7].unitLine[1] = " *";
digit[7].unitLine[2] = " *";
digit[7].unitLine[3] = " *";
digit[7].unitLine[4] = " *";
digit[7].unitLine[5] = " *";
digit[7].unitLine[6] = " *";
//8
digit[8].unitLine[0] = "****";
digit[8].unitLine[1] = "* *";
digit[8].unitLine[2] = "* *";
digit[8].unitLine[3] = "****";
digit[8].unitLine[4] = "* *";
digit[8].unitLine[5] = "* *";
digit[8].unitLine[6] = "****";
//9
digit[9].unitLine[0] = "****";
digit[9].unitLine[1] = "* *";
digit[9].unitLine[2] = "* *";
digit[9].unitLine[3] = "****";
digit[9].unitLine[4] = " *";
digit[9].unitLine[5] = " *";
digit[9].unitLine[6] = "****";
}
int main(int argc, char** argv)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
CONSOLE_SCREEN_BUFFER_INFO* posInfo = (CONSOLE_SCREEN_BUFFER_INFO*) malloc(sizeof(CONSOLE_SCREEN_BUFFER_INFO));
char usrInput[USER_INPUT_MAX_LENGTH];
InitDigit();
GetConsoleScreenBufferInfo(hOut, posInfo);
printf("请输入数字串(最大长度:%d):", posInfo->dwSize.X / 6);
scanf("%s", usrInput);
GetConsoleScreenBufferInfo(hOut, posInfo);
pos = posInfo->dwCursorPosition; //获取打印基点(当前光标位置)
for (int i = 0; i< posInfo->dwSize.X / 6 ; i++)
{
if (strcmp(&usrInput[i], "") == 0)
{
break;
}
if ((usrInput[i] >= 48) && (usrInput[i] <= 57)) //只打印数字(0-9)
{
//横向打印。Y 坐标固定,X 坐标右移一列(数字宽度:4 + 间隔宽度:2)
printfDigit(digit[usrInput[i] - 48], hOut, pos);
pos.X += 6;
SetConsoleCursorPosition (hOut, pos);
}
}
pos.X = 0;
pos.Y += 7;
SetConsoleCursorPosition (hOut, pos);
free(posInfo);
if (argc > 1) system("pause");
return 0;
}