183
社区成员




The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZ |
---|---|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/600798588 |
The Aim of This Assignment | Achieve the program function |
MU STU ID and FZU STU ID | 19103310_831901314 |
GitHub Repository: https://github.com/lanshannn/EE308_LAB2
1. PSP FORM
Personal Software Process Stage | Estimated Time/hour | Real Time/hour |
---|---|---|
Planning | 0.5 | 0.5 |
Estimate | 2 | 3 |
Development | 1 | 1 |
Analysis | 4 | 12 |
Design Spec | 10 | 12 |
Design Review | 5 | 7 |
Coding Standard | 5 | 7 |
Design | 5 | 5 |
Coding | 12 | 28 |
Code Review | 1 | 1 |
Test | 2 | 3 |
Reporting | 1 | 1 |
Test Report | 1 | 2 |
Size Measurement | - | - |
Postmortem&Process Improvement | 0.5 | 0.5 |
Summary | 50 | 83 |
2. Problem-Solving Ideas
In this lab, I chose C++ to extract different levels of keywords from the read C or C + + code files. In my opinion, we can determine the number of required keywords through the creation of string array and operations which includes erase(), replace(), find() and so on.
3. Design and Implementation Porcess
4. Code Description
(1)I store the 32 keywords to be identified into an array and I read the contents of the code file line by line through fstream and getline(). Then I use the find () to judge whether each line of code read contains the keywords existing in the array.
string key[32]={"auto","break","case","char","const","continue","default","double","do",
"else","enum","extern","float","for","goto","if","int","long","register",
"return","short","signed","sizeof","static","struct","switch","typedef",
"union","unsigned","void","volatile","while"};
// store the 32 keywords into an array
ifstream out("test.cpp");
while (!out.eof()){
getline(out,str);
// read the file line by line
for(int i=0;i<=32;i++){
string::size_type position;
position=str.find(key[i]);
if(position!=str.npos){
total_num++;
if(i==25){ // find the keyword "switch"
switch_num++;
case_num[switch_num]=0;
}
if(i==2){ // find the keyword "case"
case_num[switch_num]++;
}
if(i==7){ //double & do will repeat
total_num--;
}
}
}
(2) We need to achieve the if_else_num and if_else_if_num. This is difficult for me and I often found my code will out of range in the process of replace the string. At first, I change "else if" into "elif" by erase().
while (!out.eof()){
getline(out,str);
string strr="else if";
string::size_type idx;
idx=str.find(strr);
if(idx!=str.npos){
str.erase(str.find(strr)+2,3);
}
(3) Then in the loop, we extract all "else" "if" "else if (elif)" and save them as an new array "data".
string m[3]={"if","else","elif"};
for(int i=0;i<3;i++){
string::size_type id;
id=str.find(m[i]);
if(id!=str.npos){
data.append(m[i]);
number++;
}
}
(4) Now we will find the number of "if_else_if_num" is equal to the number of discontinuous "elif". After determining the "if_else_if_num", we also use erase to delete all "elif" from this new array "data".
for(int i=0;i<number;i++){
string::size_type pos;
pos=data.find("elif");
//cout<<pos<<endl;
if(pos!=data.npos){
if_else_if_num++;
total_num++;
data.erase(data.find("elif"),4);
if(data[data.find("elif")]==data[data.find("elif")+4]){
if_else_if_num--;
}
}
}
(5) We determine the "if_else_num" in data by finding the number of "else" minus the number of "elif".
for(int i=0;i<number;i++){
string::size_type poss;
poss=data.find("else");
if(poss!=data.npos){
if_else_num++;
data.erase(data.find("else"),4);
}
}
(6) Finally, we can print the output.
if(level>=1) {
printf("total num:%d\n", total_num);
if(level>=2) {
printf("switch num:%d\n", switch_num);
printf("case num:");
for(int i=1; i<=switch_num;i++){
printf("%d", case_num[i]);
if(i==switch_num){
printf("\n");
}
else{
printf(" ");
}
}
if(level>=3) {
printf("if_else num:%d\n", if_else_num-if_else_if_num);
if(level==4) {
printf("if_else_if num:%d\n", if_else_if_num);
}
}
}
}
The output results as following:
5. Unit test screenshots and description
6. Unit test coverage optimization and performance testing, performance optimization
7. Summary
In this assignment, I spent a long time to overcome the difficulties, and finally basically completed the project requirements.
In the process of completion, I learned the complex operations of stack (although it not be used) and string. In addition, I tried to count the number of parentheses or stack to solve the problem of determining "if_else_num" and "if_else_if_num". Besides, I tried to use dev C++, Visual Studio and GCC to test the coverage of the code, and optimized it on the basis of the test results. It all had trained my ability to build complex programs.