1. 程序流程图
![](https://i-blog.csdnimg.cn/blog_migrate/2daad49602b3c50b0c558ecaf137f089.png)
2. C++代码实现
#include <bits/stdc++.h>
using namespace std;
/// 函数声明
void getInput();
void continueOrExit();
string trim(string str);
void controller(string input);
bool isLegalStr(string str);
void checkController(string input);
void k_v_pair(string single);
void k_v_check(string key, string value);
int key_check(string key);
void value_check(string value);
/// 全局变量
int int_num;
int string_num;
int float_num;
int char_num;
int over_flag = 0; // 非法语句标志
const int DupError = -1; // 重名错误返回
const int True = 1;
const int False = 0;
vector<string> keys;
/// 开始了
int main()
{
getInput();
return 0;
}
/*
* 初始化并取得输入函数
*/
void getInput()
{
// 初始化
over_flag = 0;
int_num = 0;
string_num = 0;
float_num = 0;
char_num = 0;
vector <string>().swap(keys);
cout<<"Please input a string: "<<endl;
string input;
getline(cin, input);
controller(input);
}
/*
* 继续输入/结束函数
*/
void continueOrExit()
{
cout<<"-------------------------------------------------------"<<endl;
cout<<endl;
cout<<"Do you want to continue or quit? 1. Continue | 2. Exit"<<endl;
cout<<"Your choice: "<<endl;;
int op;
cin>>op;
cin.ignore();
switch(op)
{
case 1:
getInput();
break;
case 2:
exit(0);
break;
default:
cout<<"Error input! Please try again!"<<endl;
continueOrExit();
}
}
/*
* 滤首尾空格函数
*/
string trim(string str)
{
if(str.empty())
return str;
str.erase(0, str.find_first_not_of(" "));
str.erase(str.find_last_not_of(" ")+1);
return str;
}
/*
* 判断字符串是否为合法的常量说明语句
* 串首const 串尾;
*/
bool isLegalStr(string str)
{
bool legal = str.find("const")!=string::npos && str.find_last_of(';')==str.size()-1;
return legal;
}
/*
* 控制器-->处理字符串是否合法两个分支
*/
void controller(string input)
{
input = trim(input);
if(isLegalStr(input))
{
// 字符串合法
// 去const
input.erase(0, 5);
checkController(input);
}
else
{
// 字符串不合法
cout<<"It is not a constant declaration satement!"<<endl;
continueOrExit();
}
}
/*
* 控制器-->拆分单个赋值语句,并进行分析,统计各类型值的数量
*/
void checkController(string input)
{
string single = "";
for(int i=0; i<input.size(); i++)
{
if(over_flag ==1 )
{
cout<<"Illegal input!"<<endl;
break;
}
if(input[i]==',' || input[i]==';')
{
if(input[i]==';')
over_flag = 1;
k_v_pair(single);
single="";
continue;
}
single += input[i];
}
cout<<"int_num = "<<int_num<<"; ";
cout<<"float_num = "<<float_num<<"; ";
cout<<"char_num = "<<char_num<<"; ";
cout<<"string_num = "<<string_num<<"."<<endl;
continueOrExit();
}
/*
* key,value拆分
*/
void k_v_pair(string single)
{
cout<<"single是对的吗?"<<single<<endl;
string key = "";
string value = "";
int flag = 1;
for(int i=0; i<single.size(); i++)
{
if(flag==1 && single[i]=='=')
{
flag = 0;
continue;
}
if(flag==1)
key += single[i];
else
value += single[i];
}
k_v_check(key, value);
}
/*
* key,value检查
*/
void k_v_check(string key, string value)
{
key = trim(key);
value = trim(value);
int k_flag = key_check(key);
if(k_flag == False)
{
cout<<key<<"(Wrong! It is not a identifier!)"<<endl;
}
else if(k_flag == True)
{
value_check(value);
}
}
/*
* key检查
*/
int key_check(string key)
{
cout<<key;
regex r_key("[a-zA-Z_][a-zA-Z0-9_]*");
if(regex_match(key, r_key))
{
// key重名检查
vector<string>::iterator it = find(keys.begin(), keys.end(), key);
if(it!=keys.end())
{
cout<<"(Wrong!Duplication of name.)"<<endl;
return DupError;
}
else
{
keys.push_back(key);
return True;
}
}
return False;
}
/*
* value检查
*/
void value_check(string value)
{
regex r_num1("[+|-]?[0-9]*");
regex r_num2("[[1-9]{1}[e|E]{1}[+]?[0-9]*");
regex r_float1("[+|-]?[0-9]*[.][0-9]+");
regex r_float2("[+|-]?[0-9]*[.][0-9]+[e|E]{1}[+|-]?[0-9]*");
// 判断int
if(regex_match(value,r_num1) || regex_match(value, r_num2))
{
int_num++;
cout<<"(integer,"<<value<<")"<<endl;
}
// 判断float
else if(regex_match(value, r_float1) || regex_match(value, r_float2))
{
float_num++;
cout<<"(float,"<<value<<")"<<endl;
}
// 判断char
else if(value.find("'")==0 && value.find_last_of("'")==value.size()-1)
{
if(value.size()==3)
{
char_num++;
value.erase(0,1);
value.erase(value.size()-1, 1);
cout<<"(char,"<<value<<")"<<endl;
}
else
{
cout<<"(Wrong!There are more than one char in ''.)"<<endl;
}
}
// 判断string
else if(value.find("\"")==0 && value.find_last_of("\"")==value.size()-1)
{
string_num++;
value.erase(0,1);
value.erase(value.size()-1, 1);
cout<<"(string,"<<value<<")"<<endl;
}
// 其他错误情况
else
{
cout<<"(Wrong!The format of the value string is not correct!)"<<endl;
}
}
3. Bug总结
(1)判断由科学计数法表示的整数的正则表达式不完整,存在缺漏。
(2)以逗号分割单句表达式,当char或string语句中存在逗号时,会出错。
改进思路:当逗号存在于char或string中时,进行优先级判断。