我有两个char
数组,包含一个用于1
的图像和一个用于0
的图像。
char a[] = " &&n"
" & &n"
" & &n"
" & &n"
" & &n"
" & &n"
"& &n"
" &n"
" &n"
" &n"
" &n"
" &n"
" &n";
char b[] = " & & & & n"
" & & n"
" & & n"
" & & n"
"& &n"
"& &n"
"& &n"
"& &n"
"& &n"
" & & n"
" & & n"
" & & n"
" & & & & n";
现在我想打印10
(在屏幕上水平以大写字母)。
我尝试使用它:
printf("%s %s", a, b);
但这没有用。 我应该怎么做?
像这样的事情呢:
char *a[] = { " &&",
" & &",
" & &",
" & &",
" & &",
" & &",
"& &",
" &",
" &",
" &",
" &",
" &",
" &"};
char *b[] = /* ... */;
for (size_t i = 0; i < sizeof a / sizeof *a; i++)
{
printf("%s %sn", a[i], b[i]);
}
你不能用一个printf来做到这一点,因为你必须交错。您所要做的就是逐行构建它。所以你把每个字符串拆分为 ,然后打印
for (int i=0;i<sizeof lines_a / sizeof char*;i++)
printf("%s %sn", lines_a[i], lines_b[i]);
如果你不能按照 ouah 在他的回答中建议的方式重新设计你的数据结构——这是处理问题的最佳方式(重组数据以使其易于处理通常是一个很好的方法)——那么你需要在每个"大字符"中找到行尾并依次打印每一行。
char *a_line = a;
char *b_line = b;
char *a_end;
char *b_end;
while ((a_end = strchr(a_line, 'n')) != 0 &&
(b_end = strchr(b_line, 'n')) != 0)
{
int a_len = a_end - a_line;
int b_len = b_end - b_line;
printf("%.*s %.*sn", a_len, a_line, b_len, b_line);
a_line = a_end + 1;
b_line = b_end + 1;
}
当a
或b
数据用完时,此循环将停止。 对于示例数组,它们具有相同的行数,因此没有问题。如果他们有不同的行数,也会有更多的工作要做。 同样,代码假定每个数组中的行长度都相同,与示例数据中的长度相同。 如果它们的长度不同,就会有更多的工作要做。
完整的测试代码
请注意,我已经删除了零之后多余的尾随空格。
#include <string.h>
#include <stdio.h>
int main(void)
{
char a[] = " &&n"
" & &n"
" & &n"
" & &n"
" & &n"
" & &n"
"& &n"
" &n"
" &n"
" &n"
" &n"
" &n"
" &n";
char b[] = " & & & & n"
" & & n"
" & & n"
" & & n"
"& &n"
"& &n"
"& &n"
"& &n"
"& &n"
" & & n"
" & & n"
" & & n"
" & & & & n";
char *a_line = a;
char *b_line = b;
char *a_end;
char *b_end;
while ((a_end = strchr(a_line, 'n')) != 0 &&
(b_end = strchr(b_line, 'n')) != 0)
{
int a_len = a_end - a_line;
int b_len = b_end - b_line;
printf("%.*s %.*sn", a_len, a_line, b_len, b_line);
a_line = a_end + 1;
b_line = b_end + 1;
}
return 0;
}
示例输出
&& & & & &
& & & &
& & & &
& & & &
& & & &
& & & &
& & & &
& & &
& & &
& & &
& & &
& & &
& & & & &
但是你可能不喜欢这个答案,但是如果你想让你的"1"被完全打印出来,然后你的"0"被打印出来,我建议这样:
#include<stdio.h>
#include<Windows.h>
//gotoxy sets the cursor in position of int x,int y
void gotoxy(int x , int y){
COORD newPosition={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),newPosition);
}
//getxy gets current position of cursor and returns a COORD(a struct with fields of X and Y)
COORD getxy(void){
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle( STD_OUTPUT_HANDLE ),&csbi);
return csbi.dwCursorPosition;
}
int main(){
char a[] = " &&n"
" & &n"
" & &n"
" & &n"
" & &n"
" & &n"
"& &n"
" &n"
" &n"
" &n"
" &n"
" &n"
" &";//I deleted 'n'
char b[] = " & & & & $"
" & & $"
" & & $"
" & & $"
"& &$"
"& &$"
"& &$"
"& &$"
"& &$"
" & & $"
" & & $"
" & & $"
" & & & & $";
//your codes
COORD first=getxy();//get position of first member of your "1"
printf("%s",a);
COORD last=getxy();//get position of last member of your "1"
gotoxy(last.X+1,first.Y);
COORD current=getxy();
int i=0;
while(b[i]!=' '){
if (b[i]=='$'){//this if statement does like 'n' but sets the cursor in your defined first_of_the_line
gotoxy(current.X,current.Y+1);//current.X is your defined first_of_the_line
current=getxy();
}
else
printf("%c",b[i]);
i++;
}
}