Hello,我是老六工作室,今天我们优化一下代码。(以后每五集就会优化一次)
原代码:
#define n1 532
#define D 250
#include<iostream>
#include<windows.h>
using namespace std;
void sound()
{
Beep(n1,D);
}
int setting()
{
cout<<"设置 Setting"<<endl;
cout<<"1:停止运行Windows C++"<<endl;
cout<<"2:参数"<<endl;
cout<<"3:关闭设置"<<endl;
int a;
cin>>a;
switch (a)
{
case 1:
{
cout<<"停止Windows C++运行中...";
_sleep(1000);
return 0;
}
case 2:cout<<"【老六工作室】制作"<<endl;
}
return 0;
}
void user_handbook()
{
cout<<"O键打开,W键关闭,C键复制,V键粘贴。"<<endl;
_sleep(2000);
}
int main()
{
system("title Windows C++");
cout<<"Opening";
cout<<".";
_sleep(100);
cout<<".";
_sleep(100);
cout<<".";
_sleep(100);
string username;
cout<<endl;
cout<<"欢迎使用~"<<endl;
cout<<"[初始化1/2]请输入用户名:";
cin>>username;
string password;
cout<<"[初始化2/2]请输入密码:";
cin>>password;
int len=password.length();
while(len<6){
cout<<"请重新输入(6位以上):";
cin>>password;
len=password.length();
}
//system("start https://baidu.com"); 浏览器
return 0;
}
优化1-使用手册
更改:
①输出“使用手册”字样
②按任意键继续:用getch()实现,同时增加conio.h头文件
void user_handbook()
{
cout<<"使用手册:O键打开,W键关闭,C键复制,V键粘贴。"<<endl;
_sleep(2000);
cout<<"请按任意键继续...";
getch();
cout<<endl;
}
优化2-开机效果
更改:
①开机动画:新建函数openshow
②初始化前清屏
void openshow()
{
for(float i=0;i<=100;i+=0.3)
{
if(i<=25)
cout<<"系统设置中("<<i<<"%)..."<<endl;
else if(i<=50)
cout<<"数据加载中("<<i<<"%)..."<<endl;
else if(i<=75)
cout<<"文件解压中("<<i<<"%)..."<<endl;
else cout<<"设备调试中("<<i<<"%)..."<<endl;
_sleep(50);
system("cls");
}
}
优化3-初始化
更改:
①将初始化放入函数
②初始化后清屏
void initialize()
{
cout<<endl;
cout<<"欢迎使用~"<<endl;
cout<<"[初始化1/2]请输入用户名:";
cin>>username;
cout<<"[初始化2/2]请输入密码:";
cin>>password;
int len=password.length();
while(len<6){
cout<<"请重新输入(6位以上):";
cin>>password;
len=password.length();
}
system("cls");
}
现在的代码如下:
#define n1 532
#define D 250
#include<iostream>
#include<windows.h>
#include<conio.h>
using namespace std;
string username;
string password;
void openshow()
{
for(float i=0;i<=100;i+=0.3)
{
if(i<=25)
cout<<"系统设置中("<<i<<"%)..."<<endl;
else if(i<=50)
cout<<"数据加载中("<<i<<"%)..."<<endl;
else if(i<=75)
cout<<"文件解压中("<<i<<"%)..."<<endl;
else cout<<"设备调试中("<<i<<"%)..."<<endl;
_sleep(50);
system("cls");
}
}
void sound()
{
Beep(n1,D);
}
int setting()
{
cout<<"设置 Setting"<<endl;
cout<<"1:停止运行Windows C++"<<endl;
cout<<"2:参数"<<endl;
cout<<"3:关闭设置"<<endl;
int a;
cin>>a;
switch (a)
{
case 1:
{
cout<<"停止Windows C++运行中...";
_sleep(1000);
return 0;
}
case 2:cout<<"【老六工作室】制作"<<endl;
}
return 0;
}
void initialize()
{
cout<<endl;
cout<<"欢迎使用~"<<endl;
cout<<"[初始化1/2]请输入用户名:";
cin>>username;
cout<<"[初始化2/2]请输入密码:";
cin>>password;
int len=password.length();
while(len<6){
cout<<"请重新输入(6位以上):";
cin>>password;
len=password.length();
}
system("cls");
}
void user_handbook()
{
cout<<"使用说明:O键打开,W键关闭,C键复制,V键粘贴。"<<endl;
_sleep(2000);
cout<<"请按任意键继续...";
getch();
cout<<endl;
}
int main()
{
system("title Windows C++");
openshow();
initialize();
user_handbook();
//system("start https://baidu.com"); 浏览器
return 0;
}
今天的C++ Windows就结束了,我们下期再见!
(阅读量满100加速更新哦)