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 | Achive a program function which can extract keywords |
GitHub repository | https://github.com/BlueP0118/Git-hub1 |
MU STU ID and FZU STU ID |
20124287_832002122 |
1.PSP FORM
Personal Software Process Stages | ESTIMATE TIME/MINUTES | REAL TIME(MINUTE) |
Planning | 30 | 40 |
Estimate |
20 | 20 |
Development | 30 | 20 |
Analysis | 60 | 50 |
Design Spec | 40 | 40 |
Design Review | 50 | 40 |
Coding Standard | 60 | 30 |
Design | 60 | 60 |
Coding | 60 | 120 |
Code Review | 60 | 30 |
Test | 30 | 20 |
Reporting | 60 | 40 |
Test Report | 20 | 20 |
Size Measurement | 40 | 40 |
Postmortem&Process Improvement Plan | 30 | 30 |
Total | 650 | 600 |
2.Problem-solving ideas
Since I am familiar with C++, I chose C++ as the programming language for this lab. After getting the questions, we could write a method for each level due to the grading requirements.
3.Design and implementation process
4.Code description
Level 1:
void level_1(vector <string> s) {
int total_num = 0;
for (int i = 0; i < int(s.size()); i++) {
for (int j = 0; j < 32; j++) {
if (s[i] == keyword[j])
total_num++;
}
}
cout << "total num: " << total_num << endl;
}
Level 2:
void level_2(vector <string> s) {
int switch_num = 0;
int case_num = 0;
vector <int> case1;
for (int i = 0; i < int(s.size()); i++) {
if (s[i] == "switch") {
switch_num += 1;
for (int j = i + 1; j < int(s.size()); j++) {
if (s[j] == "case") {
case_num += 1;
} else if (s[j] == "switch" ) {
i = j - 1;
case1.push_back(case_num);
case_num = 0;
break;
} else if (j == int(s.size() - 1)) {
i = j;
case1.push_back(case_num);
break;
}
}
}
}
cout << "switch num: " << switch_num << endl;
cout << "case num: ";
for (int i = 0; i < (int)case1.size(); i++) {
cout << case1[i] << " ";
}
cout << endl;
}
Level 3:
void level_3(vector <string> s) {
int if_else_num = 0;
int if_elseif_else_num = 0;
bool c = true;//Check if the following content is else if or if
for (int i = 0; i < (int)s.size(); i++) {
if (s[i] == "if") {
for (int j = i + 1; j < (int)s.size(); j++) {
if (s[j] == "if") {
i = j;
} else if (s[j] == "else" && s[j + 2] == "if") {
if (c == true)
c = false;
} else if (s[j] == "else" && s[j + 2] != "if") {
if (c == true)
if_else_num++;
else if (c == false)
if_elseif_else_num++;
}
}
}
}
cout << "if-else num: " << if_else_num << endl;
}
Level 4:
void level_4(vector <string> s) {
int if_else_num = 0;
int if_elseif_else_num = 0;
bool c = true;//Check if the following content is else if or if
for (int i = 0; i < (int)s.size(); i++) {
if (s[i] == "if") {
for (int j = i + 1; j < (int)s.size(); j++) {
if (s[j] == "if") {
i = j;
} else if (s[j] == "else" && s[j + 2] == "if") {
if (c == true)
c = false;
} else if (s[j] == "else" && s[j + 2] != "if") {
if (c == true)
if_else_num++;
else if (c == false)
if_elseif_else_num++;
}
}
}
}
cout << "if-else num: " << if_elseif_else_num << endl;
}
keyword and main function:
vector<string> keyword = {"char", "double", "enum", "float", "int", "long",
"short", "signed", "struct", "union", "unsigned", "void", "for",
"do", "while", "break", "continue", "if", "else", "goto", "switch",
"case", "default", "return", "auto", "extern", "register", "static",
"const", "sizeof", "typedef", "volatile"
};
int main() {
int level;
string filename;
ifstream inFile;
cout << "Please input the path of the code file:" << endl;
cin >> filename;
cout << "Please input the completion level(from 1 to 4):" << endl;
cin >> level;
inFile.open(filename.c_str());
if (inFile.fail()) {
cout << "The file can not open rightly" << endl;
exit(0);
}
vector <string> str;
string str1;
while (getline(inFile, str1)) {
int i = 1;
int j = 1;
while (i < int(str1.size()) && j < int(str1.size())) {
if (isalpha(str1[i])) {
if (!isalpha(str1[j] )) {
string s = str1.substr(i, j - i);
str.push_back(s);
i = j + 1;
j = i;
} else {
j++;
}
} else {
i++;
j = i;
}
}
}
if (level == 1) {
level_1(str);
}
if (level == 2) {
level_1(str);
level_2(str);
}
if (level == 3) {
level_1(str);
level_2(str);
level_3(str);
}
if (level == 4) {
level_1(str);
level_2(str);
level_3(str);
level_4(str);
}
}
5.TEST
Level 1:
Level 2:
Level 3:
Level 4:
Fail to read:
6.Summarize
In fact, my code is very complicated, and there are many parts that can be simplified. For example, level3 and 4 can be merged and output separately, but unfortunately I haven't done too much optimization due to time. At the same time, my relatively weak basic knowledge led to frequent errors in my code, and I spent a lot of time to revise and correct it. For my harvest, I used github to build a repository and uploaded my code.