EE308 Lab 1-2

Double__Q 2022-10-27 22:35:40

1. PSP form

Personal Software Process StagesEstimated Time/minutesCompleted Time/minutes
Analysis and Planing2025
Program Development3535
Program Review1015
Performance Test1010
Blog Writing3035
Github Preparation1520
Total120140

2. Idea to solve the problems

After downloading the problems, I'm trying to understanding what's the main task. It gives me a finished code and requires me to count keyword and analyse the code's structure. This is a simple but cumbersome programming tasks. It needs patience to deal with all the detail point. The toughest part in this task is to spilt the characters in the paragraph.

3. Pseudo code and flow chart

getKeywordNum()
    cnt ← 0
    for i in code
        if substr(code, i, length(keyword)) = keyword then
            cnt ← cnt + 1

getSwitchNum()
    cntSwitch ← 0
    cntCase ← 0
    for i in code
        if substr(code, i, length('switch')) = switch then
            getCaseNum
    return cntSwitch, cntCase

getIfElseNum()
    cntIfElse ← 0
    cntIfElseIfElse ← 0
    for i in code
        if substr(code, i, length('if')) = if then
            if else_if next to if then
                cntIfElse ← cntIfElse + 1
            else then
                cntIfElseIfElse ← CntIfElseIfElse + 1
    return cntIfElse, cntIfElseIfElse
    
main()
    input code, level
    if level >= 1 then getKeywordNum()
    if level >= 2 then getSwitchNum()
    if level >= 3 then getIfElseNum() and print cntIfElse
    if level >= 4 then getIfElseNum and print cntIfElseIfElse

img

4. Code description

Get keyword's number

string keyword[] = {"asm", "do","if","return","typedef","autohttps://img-community.csdnimg.cn/images/72dec4246a964445bfd2b3e1b9cefd23.png "#left")
","double","inline","short","typeid","bool","dynamic_cast","int","signed","typename","break","else","long","sizeof","union","case","enum","mutable","static","unsigned","catch","explicit","namespace","static_cast","using","char","export","new","struct","virtual","class","extern","operator","switch","void","const","false","private","template","volatile","const_cast","float","protected","this","wchar_t","continue","for","public","throw","while","default","friend","register","true", "delete","goto","reinterpret_cast","try"};

int cnt = 0;
    for (int i = 0; i < 63; i++) {
        int len = keyword[i].length();
        for (int j = 0; j < input.length(); j++) {
            if (input.substr(j, len) == keyword[i]) {
                cnt++;
                if (input.substr(j, len) == "do") {
                    if (input.substr(j, 6) == "double") {
                        cnt--;
                    }
                }

            }
        }
    }

Get switch number

void get_switch(int& st, int id) {
    sw.emplace_back(0);
    int cnt = 0;
    for (int i = st; i < input.length(); i++) {
        if (input[i] == '{') {
            cnt++;
        } else if (input[i] == '}') {
            cnt--;
            if (!cnt) {
                st = i;
                return;
            }
        }

        if (input.substr(i, 4) == "case") {
            sw[id]++;
        }
    }
}

Get if else number


void get_if(int& st) {
    bool is_else_if = 0;
    bool flag = 0;
    int cnt = 0;
    for (int i = st + 2; i < input.length(); i++) {
        if (flag == 1) {
            if (input[i] == '{') {
                cnt++;
            }
            if (input[i] == '}') {
                cnt--;
                if (!cnt) {
                    st = i + 1;
                    return;
                }
            }
        }

        if (input.substr(i, 2) == "if") {
            get_if(i);
        }
        if (input.substr(i, 7) == "else if") {
            if (!is_else_if) {
                is_else_if = 1;
                cnt_else_if++;
            }
            i += 7;
        } else if (input.substr(i, 4) == "else") {
            if (!is_else_if) {
                cnt_else++;
            }
            flag = 1;
            i += 4;
        }
    }
}

5. Unit test

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述

6. Perfomance analyse

We can take advantage of clock() function to analyze the running time.
We can just add 3 lines in the code.

int main() {
    string path;
    cout << "Please input the file's path: ";
    cin >> path;
    cout << "Please input completion level: ";
    int level;
    cin >> level;

    double st = clock();
    ...
    double end = clock();
    cout << "time cost: " << end - st << endl;
    return 0;
}

在这里插入图片描述

7. Summarize

In this lab, I've learnt how to commit files and push that to GitHub.
Here is my Github link:https://github.com/DoubleQ666/EE308_Lab1

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

285

社区成员

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

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