很多学生问过我这个问题,要求设计一个程序,输入为数字,输出为符号化的数字,类似于数码管显示数字的功能。我随便写了一个,也没什么深奥的东西,只不过是变量控制罢了。
代码:
#define N 10 //最多可以输入10个数字
#i nclude <stdio.h>
int check(char array[],int n)
{ //检查输入是否全为数字
int i;
for(i=0;i<n;i++)
if(array<'0'||array>'9')
return -1;
return 1;
}
main()
{
char alpha[70][4]={{'*','*','*','*'},{'*',' ',' ','*'},{'*',' ',' ','*'},{'*',' ',' ','*'},
{'*',' ',' ','*'},{'*',' ',' ','*'},{'*','*','*','*'},{' ',' ',' ','*'},
{' ',' ',' ','*'},{' ',' ',' ','*'},{' ',' ',' ','*'},{' ',' ',' ','*'},
{' ',' ',' ','*'},{' ',' ',' ','*'},{'*','*','*','*'},{' ',' ',' ','*'},
{' ',' ',' ','*'},{'*','*','*','*'},{'*',' ',' ',' '},{'*',' ',' ',' '},
{'*','*','*','*'},{'*','*','*','*'},{' ',' ',' ','*'},{' ',' ',' ','*'},
{'*','*','*','*'},{' ',' ',' ','*'},{' ',' ',' ','*'},{'*','*','*','*'},
{' ','*',' ',' '},{'*',' ','*',' '},{'*',' ','*',' '},{'*','*','*','*'},
{' ',' ','*',' '},{' ',' ','*',' '},{' ',' ','*',' '},{'*','*','*','*'},
{'*',' ',' ',' '},{'*',' ',' ',' '},{'*','*','*','*'},{' ',' ',' ','*'},
{' ',' ',' ','*'},{'*','*','*','*'},{'*',' ',' ',' '},{'*',' ',' ',' '},
{'*',' ',' ',' '},{'*','*','*','*'},{'*',' ',' ','*'},{'*',' ',' ','*'},
{'*','*','*','*'},{'*','*','*','*'},{' ',' ',' ','*'},{' ',' ',' ','*'},
{' ',' ',' ','*'},{' ',' ',' ','*'},{' ',' ',' ','*'},{' ',' ',' ','*'},
{'*','*','*','*'},{'*',' ',' ','*'},{'*',' ',' ','*'},{'*','*','*','*'},
{'*',' ',' ','*'},{'*',' ',' ','*'},{'*','*','*','*'},{'*','*','*','*'},
{'*',' ',' ','*'},{'*',' ',' ','*'},{'*','*','*','*'},{' ',' ',' ','*'},
{' ',' ',' ','*'},{' ',' ',' ','*'}}; //把10个数字符号化,7行4列为一个数字
int row,count,col,n; //
char input[N+1];
int length;
printf("\nPlease input at most 10 digital to display\n");
scanf("%s",input);
length=strlen(input); //length用来记录实际输入的数字个数
if(check(input,length)==-1)
{
printf("\ninput error,exit!");
return;
}
for(row=0;row<7;row++)
{ //7行
printf("\n");
for(count=0;count<length;count++)
{ //控制每个数字
n=input[count]-'0';
for(col=0;col<4;col++)
{ //每个数字4列
printf("%c",alpha[n*7+row][col]);
}
printf(" "); //每行中数字间隔
}
}
}
运行效果可以在我blog上看到 |