Description
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int main()
{
int start, end;
cout << "请输入开始数: ";
cin >> start;
cout << "请输入结束数: ";
cin >> end;
// 确定移动的方向
int step = (end > start) ? 1 : -1;
int totalSteps = abs(end - start);
// 绘制起点和终点
for (int i = 0; i <= totalSteps; ++i)
{
if (i == 0)
{
cout << "S";
// 起点
}
else if (i == totalSteps)
{
cout << "E";
// 终点
}
else
{
cout << " ";
}
}
cout << endl;
// 模拟小车移动
for (int pos = 0; pos <= totalSteps; ++pos)
{
// 清除当前行
cout << "\r";
// 绘制当前位置的小车和其他元素
for (int i = 0; i <= totalSteps; ++i)
{
if (i == 0)
{
cout << "S";
}
else if (i == totalSteps)
{
cout << "E";
}
else if (i == pos)
{
cout << "*";
}
else
{
cout << " ";
}
}
// 刷新输出
cout.flush();
// 暂停一下以观察移动效果
Sleep(300);
}
cout << endl;
return 0;
}