我们在平时生活中都可以见到LED灯,商店上的流动字幕等
当然我们也可以用编程的方式制作流动的字幕显示
直接上代码:
#include<stdio.h>
#include<time.h>
#include<string.h>
//逐个显示字符,等到字符串显示完毕后,再逐个消去反复执行
//延时函数
int sleep(unsigned long x)
{
clock_t start = clock();
clock_t end;
do
{
if ((end = clock()) == (clock_t)-1)
{
printf("error!");
return 0;
}
} while (1000 * (end - start) / CLOCKS_PER_SEC < x);
return 1;
}
int main()
{
char name[] = "I LOVE C";
int name_len = strlen(name);
while (1)
{
for (int i = 0; i < name_len; i++)
{
putchar(name[i]);//输出单个字符
fflush(stdout);//清空输出缓冲区
sleep(500);//延时500毫秒
}
for (int i = 0; i < name_len; i++)
{
printf("\b \b");//退格
fflush(stdout);
sleep(500);
}
}
return 0;
}
程序展示:
字幕显示(滚动显示从左到右):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
//使字幕从左到右显示出来
//延时函数
int sleep(unsigned long x)
{
clock_t start = clock();
clock_t end;
do
{
if ((end = clock()) == (clock_t)-1)
{
printf("error!\n");
return 0;
}
} while (1000 * (end - start) / CLOCKS_PER_SEC < x);
return 1;
}
int main()
{
int cnt = 0;//第几个字符在最前面
char use[] = "I LOVE C ";//显示的字符串
int use_len = strlen(use);//获取长度
while (1)
{
putchar('\r');//移动光标至最前方注意单字符一定要用单引号
for (int i = 0; i < use_len; i++)
{
if (cnt + i < use_len)//使得程序正常输出字符串
{
putchar(use[cnt + i]);//每次输出单字符
}
else
{
putchar(use[cnt + i - use_len]);
}
}
fflush(stdout);
sleep(500);
if (cnt > 0)
{
cnt--;
}
else
{
cnt = use_len - 1;//倒序
}
}
return 0;
}
程序展示:
字幕显示(滚动显示从右到左):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
//使字幕从右到左显示出来
//延时函数
int sleep(unsigned long x)
{
clock_t start = clock();
clock_t end;
do
{
if ((end = clock()) == (clock_t)-1)
{
printf("error!\n");
return 0;
}
} while (1000 * (end - start) / CLOCKS_PER_SEC < x);
return 1;
}
int main()
{
int cnt = 0;//第几个字符在最前面
char use[] = "I LOVE C ";//显示的字符串
int use_len = strlen(use);//获取长度
while (1)
{
putchar('\r');//移动光标至最前方注意单字符一定要用单引号
for (int i = 0; i < use_len; i++)
{
if (cnt + i < use_len)//使得程序正常输出字符串
{
putchar(use[cnt + i]);//每次输出单字符
}
else
{
putchar(use[cnt + i - use_len]);
}
}
fflush(stdout);
sleep(500);
if (cnt < use_len - 1)
{
cnt++;
}
else
{
cnt = 0;
}
}
return 0;
}
程序展示:
接下来展示一下手算推理: