EE308Lab 1_2
| The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZU202201 |
|---|---|
| The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/608734907 |
| The Aim of This Assignment | Extract keywords of different levels from the C code files that are read in |
| MU STU ID and FZU STU ID | FZU:832002201 MU:20123825 |
· GitHub Repository
https://github.com/ClutchMasterzzz/Software-engineering/tree/main
· PSP form
| Personal Software Process Stages | Time (min) |
|---|---|
| Planning | 10 |
| Analysis (including learning new knowledge) | 10 |
| Development | 30 |
| Test | 5 |
| Postmortem & Process Improvement Plan | 20 |
·Description of problem-solving ideas
1.The first thing I need to think about is how do I read the c file into the program.
2.The requirements are divided into 4 levels, input the corresponding level can execute the corresponding code.
3.The third is to match the key words I'm looking for from the string I read and count them.
· Design and implementation process

· Code description
main method
int main() {
//"C:\\Users\\Mr.SupX\\Desktop\\Software Engineering\\test.cpp"
string address;
cout << "Please input the file address" << endl;
cin >> address;
cin.clear(); //flush the flag
cin.sync(); //clear the buffer
int level;
cout << "Please input the level you need" << endl;
scanf("%d",&level);
string str = readFileIntoString(address);
vector<string> ans = dealWithString(str);
// if - else & if -elseif -else
vector<int> answer = find_if_else(ans);
// switch-case
vector<int> answer_switch = findSwitch(ans);
//
smatch result;
regex pattern("void|signed|unsigned|short|long|int|float|double|char|enum|struct|union|typedef|Bool|Imaginary|Complex|const|volatile|restrict|inline|auto|static|extern|register|sizeof|goto|return|break|continue|if|else|switch|case|default|do\\s|while|for");
string::const_iterator iterStart = str.begin();
string::const_iterator iterEnd = str.end();
string temp;
int count = 0;
while (regex_search(iterStart, iterEnd, result, pattern))
{
count ++;
iterStart = result[0].second;
}
if(level == 1){
cout << "total num: " << count << endl;
}else if(level == 2){
cout << "total num: " << count << endl;
cout << "switch num: " << answer_switch.size() << endl;
cout << "case num: ";
for(int i=0;i<answer_switch.size();i++){
cout << answer_switch[i] << " ";
}cout << endl;
}else if(level == 3){
cout << "total num: " << count << endl;
cout << "switch num: " << answer_switch.size() << endl;
cout << "case num: ";
for(int i=0;i<answer_switch.size();i++){
cout << answer_switch[i] << " ";
}cout << endl;
cout << "if-else num: " << answer[0] << endl;
}else if(level == 4){
cout << "total num: " << count << endl;
cout << "switch num: " << answer_switch.size() << endl;
cout << "case num: ";
for(int i=0;i<answer_switch.size();i++){
cout << answer_switch[i] << " ";
}cout << endl;
cout << "if-else num: " << answer[0] << endl;
cout << "if-elseif-else num:" << answer[1] << endl;
}else{
cout << "wrong level" << endl;
}
return 0;
}
Read_File
string readFileIntoString(string filename) {
ifstream ifile(filename);
ostringstream buf;
char ch;
while (buf && ifile.get(ch))
buf.put(ch);
//返回与流对象buf关联的字符串
ifile.close();
return buf.str();
}
Find_switch
vector<int> findSwitch(vector<string> v1){
vector<int> answer;
stack<char> myStack;
bool judge = false;
int count = 0;
for(int i=0;i<v1.size();i++){
string strS = v1[i];
if(strS.find("switch") != strS.npos){
judge = true;
}
if(judge){
if(strS.find('{') != strS.npos){
myStack.push('{');
}else if(strS.find('}') != strS.npos){
myStack.pop();
}
}
if(myStack.empty() != true){
if(strS.find("case") != strS.npos){
count ++;
}
}else{
judge = false;
if(count != 0){
answer.push_back(count);
count = 0;
}
}
}
return answer;
}
Find_if-else and if-elseif-else
vector<int> find_if_else(vector<string> v){
stack<bool> mystack;
vector<int> answer;
int v1 = 0,v2 = 0;
int size = 0;
for(int i=0;i<v.size();i++){
string strS = v[i];
if(strS.find("else if") == strS.npos && strS.find("if") != strS.npos){
mystack.push(false);
size ++;
continue;
}else if(strS.find("else if") != strS.npos && mystack.empty() != true){
mystack.top() = true;
continue;
}else if(strS.find("else if") == strS.npos && strS.find("else") != strS.npos){
if(mystack.top() == true){
v2 ++;
}else{
v1 ++;
}
mystack.pop();
continue;
}
}
answer.push_back(v1);
answer.push_back(v2);
return answer;
}
string readFileIntoString(string filename) {
ifstream ifile(filename);
ostringstream buf;
char ch;
while (buf && ifile.get(ch))
buf.put(ch);
//返回与流对象buf关联的字符串
ifile.close();
return buf.str();
}
Count all Keywords
smatch result;
regex pattern("void|signed|unsigned|short|long|int|float|double|char|enum|struct|union|typedef|Bool|Imaginary|Complex|const|volatile|restrict|inline|auto|static|extern|register|sizeof|goto|return|break|continue|if|else|switch|case|default|do\\s|while|for");
string::const_iterator iterStart = str.begin();
string::const_iterator iterEnd = str.end();
string temp;
int count = 0;
while (regex_search(iterStart, iterEnd, result, pattern))
{
count ++;
iterStart = result[0].second;
}
· Unit test

·Summary
In this lab, I develop a c++ project and tried to advance it, I found it isreally important to have a plan and analysis before to do it. What's more I also tried to grasp the knowledge of markdown and how to upload project to github.
相关推荐
EE-308 LAB4 final version.7z
这是本次实验的压缩包
《模式识别与数据挖掘》配套PPT课件
《模式识别与数据挖掘》配套PPT课件
科技成果转化项目进场交易前,评估备案环节需要准备哪些材料?_8.docx
科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。
人工智能通识教育(中职版)课程标准+PPT课件+部分素材+电子活页+在线考核
人工智能通识教育(中职版)课程标准+PPT课件+部分素材+电子活页+在线考核
跨境电商供应链管理(AIGC版)课件+教案+参考答案
跨境电商供应链管理(AIGC版)课件+教案+参考答案


