EE308 Lab 1-2

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

请添加图片描述

4. Code description

Get keyword’s number
string keyword[] = {"asm", "do","if","return","typedef","auto","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();
    ifstream ifs;
    ifs.open(path, ios::in);
    if (!ifs.is_open()) {
        cout << "fail to open the specified file" << endl;
        exit(0);
    }

    string buf;
    while (getline(ifs, buf)) {
        input += buf;
        input += "\n";
    }

    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--;
                    }
                }

            }
        }
    }


    int sw_num = 0;
    for (int i = 0; i < input.length(); i++) {
        if (input.substr(i, 6) == "switch") {
            get_switch(i, sw_num++);
        }
    }
    for (int i = 0; i < input.length(); i++) {
        if (input.substr(i, 2) == "if") {
            get_if(i);
        }
    }



    if (level >= 1) {
        cout << "total num: " << cnt << endl;
    }
    if (level >= 2) {
        cout << "switch num: " << sw_num << endl;
        cout << "case num: ";
        for (int i = 0; i < sw.size(); i++) {
            cout << sw[i] << " ";
        }
        cout << endl;
    }
    if (level >= 3) {
        cout << "if-else num: " << cnt_else << endl;
    }
    if (level >= 4) {
        cout << "if-elseif-else num: " << cnt_else_if << endl;
    }
//    double end = clock();
//    cout << "time cost: " << end - st << endl;
    return 0;
}

在这里插入图片描述

7.

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值