代码拉取完成,页面将自动刷新
//https://gitee.com/devcpp/cgames anbangli@foxmail.com GNU GPL v3
//cgame4-3(strings)v1.cpp 多种字符串动画
#include <iostream> //C++基本输入输出函数库头文件
#include <cstdlib> //C++标准函数库头文件(包括随机数相关函数)
#include <ctime> //C++时间相关函数库头文件
#include <windows.h> //非标准库函数 Sleep 所需的头文件
#include <conio.h> //非标准库函数 kbhit 和 getch 所需的头文件
using namespace std; //使用C++中的名字空间"std"
int main() {
cout << "多种字符串动画" << endl;
char str[120] = "Welcome to Wuhan, my friends! Have a good time here!";
int i, len = strlen(str); //求出字符串长度
cout << "字符串闪烁" << endl;
while (!kbhit()) {
cout << "\r" << str ; //光标回到行首,输出显示字符串
Sleep(250);
cout << "\r"; //光标回到行首
for (i = 0; i < len; i++)
cout << ' ';
Sleep(250);
}
getch(); //读取用户键入的字符,防止被用于后续输入
cout << "\r" << str << endl; //光标回到行首,输出显示字符串并换行
cout << "字符串逐渐显现和擦除" << endl;
i = -1;
while (!kbhit()) {
i = (i + 1) % (len * 2); //循环变量增1并对 len * 2 取余
if (i == 0 || i == len)
cout << "\r"; //光标回到行首
if (i < len) //[0, len-1]
cout << str[i]; //输出单个字符
else //[len, len*2-1]
cout << ' '; //输出字符
Sleep(50);
}
getch(); //读取用户键入的字符,防止被用于后续输入
cout << "\r" << str << endl; //光标回到行首,输出显示字符串并换行
cout << "字符串左右往复移动" << endl;
int width = 75; //一行内所能显示的最大字符数
len = strlen(str);
int left = 0;
int inc = 1; //左边空格字符每次增加量(increase)
while (!kbhit()) {
left += inc;
if (left == 0 || left == width - len) //左边空格数增长到最大值
inc = -1 * inc; //翻转字符增加量(1 <-> -1)
cout << '\r'; //光标回到行着
for (i = 0; i < left; i++) //输出左边空格
cout << ' ';
cout << str; //输出整个字符串
for (i = left + len; i < width; i++) //输出右边空格
cout << ' ';
Sleep(100);
}
getch();
cout << endl;
cout << "字符串从右向左滚动输出" << endl;
strcpy (str, "Welcome to Wuhan, my friends! ");
len = strlen(str); //字符串长度
width = 60; //屏幕上最大输出字符数
int pos = 0; //字符串起始输出点
while (!kbhit()) {
pos = (pos + 1) % len; //输出起始点增1,并对 len取余
cout << "\r";
for (i = 0; i < width; i++ )
cout << str[(pos + i) % len];
Sleep(150);
}
getch();
cout << endl;
cout << "字符串从右向左滚动输出(正确处理中英文混和字符串)" << endl;
strcpy (str, "Welcome to Wuhan! 欢迎来到英雄城市武汉! ");
len = strlen(str); //字符串长度
width = 40; //屏幕上最大输出字符数
pos = 0; //字符串起始输出点
while (!kbhit()) { //主循环
if (str[pos] > 0 && str[pos] < 127) //如果字符串当前元素为英文字符
pos = (pos + 1) % len; //起始点增1,并对 len取余
else //非英文字符
pos = (pos + 2) % len; //起始点增2 并对len取余
cout << "\r";
for (i = 0; i < width; i++ )
cout << str[(pos + i) % len];
Sleep(150);
}
getch();
cout << endl;
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。