如何输出小于跑马灯符号大小的跑马灯串?
How to output a marquee string that is smaller than the size of the marquee sign?
所以我的代码接受一串字母,然后以字幕的方式输出该字符串,符号的大小为 5。例如,如果我想输出 "Hello World!",输出是:
[Hello]
[ello ]
[llo W]
[lo Wo]
[o Wor]
[ Worl]
[World]
[orld!]
[rld! ]
[ld! H]
[d! He]
[! Hel]
[ Hell]
但问题是,如果有一串长度为 8 且跑马灯标志的大小为 10 的字母,那么我只想在跑马灯标志内显示一次该字符串。因此,如果字符串的大小小于指示的选取框符号,则只显示该字符串一次。例如:
Input:
Activist (the string that I want to output in a sign)
10 (the length of the sign)
Output:
[Activist ]
注意选取框符号中还有 10 个空格,它所做的只是将字符串本身输出一次。
这是我的代码。如果有指示,我不止一次将它制作成 运行:
#include <stdio.h>
#include <string.h>
void ignoreRestOfLine(FILE *fp) {
int c;
while ((c = fgetc(fp)) != EOF && c != '\n');
}
int main(void) {
int num_times, count = 0;
int marq_length, sign = 0;
scanf("%d ", &num_times);
char s[100];
for (count = 0; count < num_times; count++) {
if (fgets(s, sizeof(s), stdin) == NULL) {
// Deal with error.
}
if (scanf("%d", &marq_length) != 1) {
// Deal with error.
}
ignoreRestOfLine(stdin);
size_t n = strlen(s) - 1;
int i, j;
if (s[strlen(s) - 1] == '\n')
s[strlen(s) - 1] = '[=12=]';
printf("Sign #%d:\n", ++sign);
for (i = 0; i < n + 1; i++) {
putchar('[');
for (j = 0; j < marq_length; j++) {
char c = s[(i + j) % (n + 1)];
if (!c)
c = ' ';
putchar(c);
}
printf( "]\n" );
}
}
}
输入和输出如下:
Input:
3
Hello World!
5
Sign #1: (This is the output)
[Hello]
[ello ]
[llo W]
[lo Wo]
[o Wor]
[ Worl]
[World]
[orld!]
[rld! ]
[ld! H]
[d! He]
[! Hel]
[ Hell]
Activist
10
Sign #2: (This is the output)
[Activist A]
[ctivist Ac]
[tivist Act]
[ivist Acti]
[vist Activ]
[ist Activi]
[st Activis]
[t Activist]
[ Activist ]
LOL
2
Sign #3: (This is the output)
[LO]
[OL]
[L ]
[ L]
除了符号 #2 之外,一切正常。如果字符串的长度小于跑马灯的大小,如何在跑马灯中只输出一次字符串?
将循环改成这样:
if (n <= marq_length) {
printf("[%-*s]\n", marq_length, s);
} else {
for (i = 0; i < n + 1; i++) {
putchar('[');
for (j = 0; j < marq_length; j++) {
char c = s[(i + j) % (n + 1)];
if (!c)
c = ' ';
putchar(c);
}
printf("]\n");
}
}
所以我的代码接受一串字母,然后以字幕的方式输出该字符串,符号的大小为 5。例如,如果我想输出 "Hello World!",输出是:
[Hello]
[ello ]
[llo W]
[lo Wo]
[o Wor]
[ Worl]
[World]
[orld!]
[rld! ]
[ld! H]
[d! He]
[! Hel]
[ Hell]
但问题是,如果有一串长度为 8 且跑马灯标志的大小为 10 的字母,那么我只想在跑马灯标志内显示一次该字符串。因此,如果字符串的大小小于指示的选取框符号,则只显示该字符串一次。例如:
Input:
Activist (the string that I want to output in a sign)
10 (the length of the sign)
Output:
[Activist ]
注意选取框符号中还有 10 个空格,它所做的只是将字符串本身输出一次。
这是我的代码。如果有指示,我不止一次将它制作成 运行:
#include <stdio.h>
#include <string.h>
void ignoreRestOfLine(FILE *fp) {
int c;
while ((c = fgetc(fp)) != EOF && c != '\n');
}
int main(void) {
int num_times, count = 0;
int marq_length, sign = 0;
scanf("%d ", &num_times);
char s[100];
for (count = 0; count < num_times; count++) {
if (fgets(s, sizeof(s), stdin) == NULL) {
// Deal with error.
}
if (scanf("%d", &marq_length) != 1) {
// Deal with error.
}
ignoreRestOfLine(stdin);
size_t n = strlen(s) - 1;
int i, j;
if (s[strlen(s) - 1] == '\n')
s[strlen(s) - 1] = '[=12=]';
printf("Sign #%d:\n", ++sign);
for (i = 0; i < n + 1; i++) {
putchar('[');
for (j = 0; j < marq_length; j++) {
char c = s[(i + j) % (n + 1)];
if (!c)
c = ' ';
putchar(c);
}
printf( "]\n" );
}
}
}
输入和输出如下:
Input:
3
Hello World!
5
Sign #1: (This is the output)
[Hello]
[ello ]
[llo W]
[lo Wo]
[o Wor]
[ Worl]
[World]
[orld!]
[rld! ]
[ld! H]
[d! He]
[! Hel]
[ Hell]
Activist
10
Sign #2: (This is the output)
[Activist A]
[ctivist Ac]
[tivist Act]
[ivist Acti]
[vist Activ]
[ist Activi]
[st Activis]
[t Activist]
[ Activist ]
LOL
2
Sign #3: (This is the output)
[LO]
[OL]
[L ]
[ L]
除了符号 #2 之外,一切正常。如果字符串的长度小于跑马灯的大小,如何在跑马灯中只输出一次字符串?
将循环改成这样:
if (n <= marq_length) {
printf("[%-*s]\n", marq_length, s);
} else {
for (i = 0; i < n + 1; i++) {
putchar('[');
for (j = 0; j < marq_length; j++) {
char c = s[(i + j) % (n + 1)];
if (!c)
c = ' ';
putchar(c);
}
printf("]\n");
}
}