我的C++学习笔记-FirstProject-RecordYourInformation.cpp
“Hello大家好哦,我是刚学习C++的初中生萌新lzhbhlr今天带大家捋一捋我的第一个CPP项目中的逻辑关系哦!那我们开始吧!”
众所周知,python是一门非常具有封装性的语言,所以我在C++里面将python的语言习惯融入了进去,还请大佬们谅解哦~~毕竟是个很der的中学生嘿嘿~
1.我们的封装!
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
#include <time.h>
using namespace std;
typedef struct Person
{
string name;
double age;
string job;
long unsigned int familyMembers;
} person;
typedef struct modeChecker
{
string mode;
string command;
string Users;
long unsigned int password;
string EnterUsers;
long unsigned int EnterPassword;
string data;
char dataTime[32] {0};
} modeChecker;
前面我导入了五个头文件用来做处理的内容,因为本人没有打注释的习惯所以不对这些头文件的作用做一一解释。
这里我定义了两个数据类型:“Person"和"modeChecker”,Person的作用是储存一些个人的信息,其实这是属于一种python封装的习惯,而modeChecker是储存一些开发者模式的信息的。(大佬们请指点更好的解决方案)
2.定义部分
person person1;
modeChecker modes;
long unsigned int timeDelay=500l;
modes.Users="Root";
modes.password=111111l;
定义person1对象,啊呸呸呸,变量,以及我们的Root账户和密码(注:为确保时间复杂度的尽可能低,我用了无符号长整形作为密码)和时间延迟值。
3.开发者模式
if (modes.mode=="ProGrammers")
{
cout<<"Username?";
cin>>modes.EnterUsers;
Sleep(timeDelay);
cout<<"password?";
cin>>modes.EnterPassword;
Sleep(timeDelay);
if (modes.EnterPassword==modes.password&&modes.EnterUsers==modes.Users)
{
cout<<"Successful sign-in!"<<endl;
Sleep(timeDelay);
cout<<"Command Here>>>";
cin>>modes.command;
Sleep(timeDelay);
if (modes.command=="ReadAll")
{
ifstream file("InfomationOfUsers.ifmto");
while (getline(file,modes.data))
{
cout<<modes.data<<endl;
Sleep(timeDelay);
}
}
else if (modes.command=="GetNowTime")
{
time_t timeNow=time(NULL);
strncpy(modes.dataTime,ctime(&timeNow),sizeof(modes.dataTime));
cout<<modes.dataTime<<endl;
}
else
{
cout<<"Error:Undefind Command in System."<<endl;
}
}
else
{
cout<<"Error:Uncorrect Username or Password"<<endl;
}
}
感觉这一段蛮简单的,就是一个验证一个读取一个时间两个Error。不过大小写还是敏感的,大佬们有没有办法能快速识别同一拼写下的所有可能性呢,最好不要一个一个“||”。
3.User模式
else
{
cout<<"Hello,What's your name?(Please No Space!)";
cin>>person1.name;
Sleep(timeDelay);
cout<<"Oh,Hello "<<person1.name<<"."<<endl;
Sleep(timeDelay);
cout<<"How old are you???";
cin>>person1.age;
Sleep(timeDelay);
cout<<"OK,you're "<<person1.age<<" years old!Glad to meet you!"<<endl;
Sleep(timeDelay);
if (person1.age>=18)
{
cout<<"emmmm...You're a adult!You can play 'Hero Honor' in long time!"<<endl;
Sleep(timeDelay);
}
else
{
cout<<"You can't play 'Hero Honor' for more than 2 hours!"<<endl;
Sleep(timeDelay);
}
cout<<"What's your job?";
cin>>person1.job;
Sleep(timeDelay);
cout<<"OK,Iknow."<<endl;
Sleep(timeDelay);
cout<<"How many FamilyMembers in your famliy?";
cin>>person1.familyMembers;
Sleep(timeDelay);
cout<<"Thank you!!I will write your infomation on the NOTEBOOK"<<endl;
Sleep(timeDelay);
ofstream OutFile("InfomationOfUsers.ifmto",ios::app);
OutFile<<"Using Language:C++"<<endl;
Sleep(timeDelay);
OutFile<<"Author:Barry Liu"<<endl;
Sleep(timeDelay);
OutFile<<"Name:"<<person1.name<<endl;
Sleep(timeDelay);
OutFile<<"Age:"<<person1.age<<endl;
Sleep(timeDelay);
OutFile<<"Job:"<<person1.job<<endl;
Sleep(timeDelay);
OutFile<<"Family'sMember num:"<<person1.familyMembers<<endl;
Sleep(timeDelay);
OutFile<<"--------END--------"<<endl;
OutFile.close();
}
return 0;
嘿嘿写入文件以及一个小小的判断。(Sleep是windows.h里的。)
OK🆗放出全部源代码,欢迎进群(QQ群:838914009)谈论编程哦。
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
#include <time.h>
using namespace std;
typedef struct Person
{
string name;
double age;
string job;
long unsigned int familyMembers;
} person;
typedef struct modeChecker
{
string mode;
string command;
string Users;
long unsigned int password;
string EnterUsers;
long unsigned int EnterPassword;
string data;
char dataTime[32] {0};
} modeChecker;
int main()
{
person person1;
modeChecker modes;
long unsigned int timeDelay=500l;
modes.Users="Root";
modes.password=111111l;
cout<<"Mode(ProGrammers/User)";
cin>>modes.mode;
Sleep(timeDelay);
if (modes.mode=="ProGrammers")
{
cout<<"Username?";
cin>>modes.EnterUsers;
Sleep(timeDelay);
cout<<"password?";
cin>>modes.EnterPassword;
Sleep(timeDelay);
if (modes.EnterPassword==modes.password&&modes.EnterUsers==modes.Users)
{
cout<<"Successful sign-in!"<<endl;
Sleep(timeDelay);
cout<<"Command Here>>>";
cin>>modes.command;
Sleep(timeDelay);
if (modes.command=="ReadAll")
{
ifstream file("InfomationOfUsers.ifmto");
while (getline(file,modes.data))
{
cout<<modes.data<<endl;
Sleep(timeDelay);
}
}
else if (modes.command=="GetNowTime")
{
time_t timeNow=time(NULL);
strncpy(modes.dataTime,ctime(&timeNow),sizeof(modes.dataTime));
cout<<modes.dataTime<<endl;
}
else
{
cout<<"Error:Undefind Command in System."<<endl;
}
}
else
{
cout<<"Error:Uncorrect Username or Password"<<endl;
}
}
else
{
cout<<"Hello,What's your name?(Please No Space!)";
cin>>person1.name;
Sleep(timeDelay);
cout<<"Oh,Hello "<<person1.name<<"."<<endl;
Sleep(timeDelay);
cout<<"How old are you???";
cin>>person1.age;
Sleep(timeDelay);
cout<<"OK,you're "<<person1.age<<" years old!Glad to meet you!"<<endl;
Sleep(timeDelay);
if (person1.age>=18)
{
cout<<"emmmm...You're a adult!You can play 'Hero Honor' in long time!"<<endl;
Sleep(timeDelay);
}
else
{
cout<<"You can't play 'Hero Honor' for more than 2 hours!"<<endl;
Sleep(timeDelay);
}
cout<<"What's your job?";
cin>>person1.job;
Sleep(timeDelay);
cout<<"OK,Iknow."<<endl;
Sleep(timeDelay);
cout<<"How many FamilyMembers in your famliy?";
cin>>person1.familyMembers;
Sleep(timeDelay);
cout<<"Thank you!!I will write your infomation on the NOTEBOOK"<<endl;
Sleep(timeDelay);
ofstream OutFile("InfomationOfUsers.ifmto",ios::app);
OutFile<<"Using Language:C++"<<endl;
Sleep(timeDelay);
OutFile<<"Author:Barry Liu"<<endl;
Sleep(timeDelay);
OutFile<<"Name:"<<person1.name<<endl;
Sleep(timeDelay);
OutFile<<"Age:"<<person1.age<<endl;
Sleep(timeDelay);
OutFile<<"Job:"<<person1.job<<endl;
Sleep(timeDelay);
OutFile<<"Family'sMember num:"<<person1.familyMembers<<endl;
Sleep(timeDelay);
OutFile<<"--------END--------"<<endl;
OutFile.close();
}
return 0;
}