EE308_LAB2

....allen.... 2021-09-23 23:28:02
The Link Your Class https://bbs.csdn.net/forums/MUEE308FZ?category=0
The Link of Requirement of This Assignment https://bbs.csdn.net/topics/600798588
The Aim of This AssignmentLearn how to use git and github & Keywords Capturing
MU STU ID and FZU STU ID19104847 & 831902119

img

My Github address: https://github.com/AllenABCDE/EE308/tree/master/LAB2

  • PSP From of this LAB

Personal Software Process StagesEstimated Time(min)Completed Time(min)
Planning--
Estimate3020
Development--
Analysis90120
Design Spec2030
Design Review1010
Coding Standard3040
Design3060
Coding300540
Code Review6090
Test6060
Reporting12090
Test Report6090
Size Measurement2030
Postmortem&Process Improvement3020
total8601200
  • Problem-solving ideas

img

Before completing the more difficult requirements, you need to complete the Lower requirements.

  • Step of analysis

img

  • Code Implementation

  • *File reading

    ​ The entire file needs to be read first. Use getLine() to read the contents of the file line by line and store them in a vector.

void input (string path) {
    ifstream fp;
    string text;
    fp.open(path,ios::in);     
    if (!fp.is_open()) {
        cout<<"Error!"<<endl;
        return;
    }
    while (getline(fp, text)) {     
        content.push_back(text);
    }
    fp.close();     
  • Handling of comments and strings

    ​ To prevent the possible keywords in the comment statement and string from interfering with the extraction results, it is necessary to delete the comment statement and string. Here I learned and used the find() function and the copy substr() function.

void handle () {
    string text,line;
    size_t j=0,k=0,t=0;
    int i=0,f=0,r=0;
    for (i=0; i<content.size(); i++) {
        line = content[i];
        if (line.find("\t") != line.npos && line.find("\t") == 0) {   
            do {
                k = line.find("\t")+1;
                line = line.substr(k,line.length()-k);
            } while (line.find("\t") != line.npos && line.find("\t") == 0);
            content[i] = line;
        }
        if (line.find("//") != line.npos) {    
            j = line.find("//");     
            if (j==0) {
                content.erase(content.begin()+i);     
                i--;
            } else {
                text = line.substr(0,j);       
                content[i] = text;
            }
        } else if (line.find("\"") != line.npos) {     
            size_t p[50] = {0};
            k = 0;
            f = 0;
            while ((t=line.find("\"",k)) != line.npos) {        
                p[f] = t;
                f++;
                k=t+1;
            }
 
            p[f] = line.length();
            text = line.substr(0,p[0]);
            
            for (r=1; p[r]!=0; r+=2) {
                text += line.substr(p[r]+1,p[r+1]-p[r]-1);     
            }
            content[i] = text;
        } else if (line.find("/*") != line.npos) {      
            j = line.find("/*");
            k = line.find("*/");
            
            if (k != line.npos) {   
                if (j == 0) {
                    content[i] = line.substr(k+2,line.length());
                } else {
                    content[i] = line.substr(0,j);
                }
                i--;
            } else {
                content[i] = line.substr(0,j);
                i += 1;
                line = content[i];
                while (line.find("*/") == line.npos) {
                    content.erase(content.begin()+i);       
                    line = content[i];
                }
                k = line.find("*/")+2;
                content[i] = line.substr(k,line.length()-k);
            }
        }
    }
}
  • Keyword statistics*

    ​ First, build a key word group to store the keywords. You then use the find() function to find and count keywords line by line.

string keyword[] = {"auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"};

void Keyword () {
    int num = 0;
    string line;
    for (int i = 0; i<content.size(); i++) {
        line = content[i];
        for (int j=0; j<32; j++) {
            size_t fi = line.find(keyword[j], 0);
            while (fi!=line.npos && judge(line,keyword[j])) {
                num++;
                fi = line.find(keyword[j], fi + 1);
            }
        }
    }
    cout<<"total num: "<<num<<endl;
  • Find the number of switch case groups and print the number of cases in each group

    ​ The group number is determined by looking for the number of switches, and the number of cases before the next switch appears is counted as the number of cases in the group.

void SwitchCase () {
    int snum = 0,f = -1,cnum[1000]={0};
    string line;
    for (int i = 0; i<content.size(); i++) {
        line = content[i];
        if (line.find("switch") != line.npos && judge(line, "switch")) {
            snum += 1;
            f += 1;
        }
        if (line.find("case") != line.npos && judge(line, "case")) { 
            cnum[f] += 1;
        }
    }
    cout<<"switch num: "<<snum<<endl;
    cout<<"case num:";
    for (int i = 0; i<=f; i++) {
        cout<<" "<<cnum[i];
    }
    cout<<endl;
  • Find if else and if else if else

    ​ Use stacks for matching. 1 is pushed when if is encountered, and 2 when if_else is encountered. When else is encountered, the stack is unmatched with the if else structure if the top of the stack is 1, and the if else_if else structure if the top of the stack is 2. However, the current method will be matched incorrectly in some special cases, such as if else_if structure nested in the if part of the if else structure, will be matched incorrectly into an if else_if else structure. For the time being, we have not come up with a new solution.

void IfElse (int level) {
    int sum1 = 0,sum2 = 0;
    stack<int> s;
    string line;
    for (int i = 0; i<content.size(); i++) {
        line = content[i];
        if (line.find("if") != line.npos && line.find("else") == line.npos && judge(line, "if")) {     
            s.push(1);
        } else if (line.find("if") == line.npos && line.find("else") != line.npos &&  s.empty() == false) {
            if (s.top() == 1) {
                sum1++;  
            } else {
                sum2++;   
            }
            s.pop();    
            
        }else  if (line.find("if") != line.npos && line.find("else") != line.npos && judge(line, "if")) {
            s.push(2);   
        }
    }
    if (level == 3) {
        cout<<"if-else num: "<<sum1<<endl;
    } else if (level == 4) {
        cout<<"if-else num: "<<sum1<<endl;
        cout<<"if-else-if num: "<<sum2<<endl;
    }
  • Result

    img

  • Unit test screenshots and description.

    img


    img

  • Summary

​ The requirements of this assignment seemed very logical at the beginning and should be changed quickly. But when it comes to actually writing code to implement the requirements, it turns out to be quite complicated, with lots of details to pay attention to. There's always going to be all kinds of bugs when you write a program and you think you're done and you run it. Only by improving the code again and again can the requirements be successfully implemented. Through this assignment, I touched and learned Github usage, code specification, performance testing and other things I had never touched before. I felt that I gained a lot. In general, I still need to learn and practice more, and practice makes perfect.

...全文
186 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

183

社区成员

发帖
与我相关
我的任务
社区描述
福州大学 梅努斯国际工程学院 软件工程 教学
软件工程 高校
社区管理员
  • 单步调试
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧