关于用C语言的数字打印行如:
当我第一眼看到时,想到的肯定是将一个个的数字先打印好,然后在组合起来,就形成了数字的组合。
首先,我想是不是可以用for循环来控制,毕竟数字在电脑上就是由梅花和空格组成,形如这样的代码
void print1() { int i, j; for (i = 0;i < 6; i++) printf(" *\n"); printf("\n"); } void print2() { int i, j; for (i = 0; i < 6; i++) printf("*"); for (i = 0; i < 4; i++) { printf("\n"); for (j = 0; j < 4 - i; j++) printf(" "); printf("*"); }printf("\n"); for (i = 0; i < 6; i++) printf("*"); printf("\n"); }
也可以完成一个个数字的输入,打印。但是却完成不了多个数字的联合输出。也就只能看向数组,一个数字就是一个二维数组,多个数字就形成了一个三维数组。于是代码就可以这样写!
#include<stdio.h> #include<string.h> int main() { char b[100], a[10][5][6] = { ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘,‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ *‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘*‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘*‘, ‘ ‘ }; int i, j, k, n = 0; gets_s(b); n = strlen(b); for (j = 0; j < 5; j++) { for (k = 0; k < n; k++) { if (b[k] == ‘0‘) for (i = 0; i < 6; i++) printf("%c", a[0][j][i]); if (b[k] == ‘1‘) for (i = 0; i < 6; i++) printf("%c", a[1][j][i]); if (b[k] == ‘2‘) for (i = 0; i < 6; i++) printf("%c", a[2][j][i]); if (b[k] == ‘3‘) for (i = 0; i < 6; i++) printf("%c", a[3][j][i]); if (b[k] == ‘4‘) for (i = 0; i < 6; i++) printf("%c", a[4][j][i]); if (b[k] == ‘5‘) for (i = 0; i < 6; i++) printf("%c", a[5][j][i]); if (b[k] == ‘6‘) for (i = 0; i < 6; i++) printf("%c", a[6][j][i]); if (b[k] == ‘7‘) for (i = 0; i < 6; i++) printf("%c", a[7][j][i]); if (b[k] == ‘8‘) for (i = 0; i < 6; i++) printf("%c", a[8][j][i]); if (b[k] == ‘9‘) for (i = 0; i < 6; i++) printf("%c", a[9][j][i]); } printf("\n"); } return 0; }
问题便迎刃而解!~
时间: 2024-10-07 02:29:13