285
社区成员




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 |
https://github.com/ClutchMasterzzz/Software-engineering/tree/main
Personal Software Process Stages | Time (min) |
---|---|
Planning | 10 |
Analysis (including learning new knowledge) | 10 |
Development | 30 |
Test | 5 |
Postmortem & Process Improvement Plan | 20 |
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.
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;
}
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();
}
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;
}
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();
}
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;
}
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.