EE308Lab 1_2

ClutchMaster 2022-10-25 18:31:44
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZU202201
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/608734907
The Aim of This AssignmentExtract keywords of different levels from the C code files that are read in
MU STU ID and FZU STU IDFZU:832002201 MU:20123825

· GitHub Repository

https://github.com/ClutchMasterzzz/Software-engineering/tree/main

· PSP form

Personal Software Process StagesTime (min)
Planning10
Analysis (including learning new knowledge)10
Development30
Test5
Postmortem & Process Improvement Plan20

·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

img

· 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

img

·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.

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

285

社区成员

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

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