写了一个C++的入门项目点餐系统,用到的技能包括
正则表达式校验(int,double,数字是否在允许的输入范围)
vector (取值,存值)
map(取值,存值)
set(取值,存值)
time.h(time_t的日期时间操作)
字符串的操作(string转成double,int,char)
class 和 struct的一般用法(例如重载操作运算符)
标准库的一些用法,例如输入输出等等
操作环境是 centOS(linux)
想要下载代码可以到GitHub
https://github.com/howard789/OrderDishes
bool RegUtil::isValid(char *str, char *patterm)
{
char ebuff[256];
regex_t reg;
int cflag = REG_EXTENDED | REG_NEWLINE | REG_NOSUB;
int status = 0;
status = regcomp(®, patterm, cflag);
if (status != 0) {
regerror(status, ®, ebuff, 256);
cout << ebuff << endl;
return false;
}
status = regexec(®, str, 0, NULL, 0);
if (status == 0) {
return true;
}
else {
return false;
}
}
bool RegUtil::isPositiveDouble(char *str)
{
char *patterm = "^[+]?[0-9]+[.]?[0-9]+$";
return isValid(str, patterm);
}
bool RegUtil::isPositiveDouble(string str) {
return isPositiveDouble(StringUtils::stringToChar(str));
}
double RegUtil::returnDoubleIfValid(string num, double minNum, double maxNum)
{
string temp;
if (num.at(0) == '-') {
temp = num.substr(1, num.length());
}
else {
temp = num;
}
bool validDouble = isPositiveDouble(temp);
double result;
if (validDouble)
{
result= StringUtils::stringToDouble(num);
if (result ==0) {
cout << "can not be zero 不可为零!" << endl;
return 0;
}
if (result > maxNum&&maxNum != -1) {
cout << "bigger than maxNum allowed 输入的数字超过最大值" << endl;
return 0;
}
if (result < minNum&&minNum != -1) {
cout << "smaller than minNum allowed 输入的数字小于最小值" << endl;
return 0;
}
return result;
}
else
{
cout<<"illegal input 输入的字符不合法"<<endl;
return 0;
}
};