【EE308 Lab1-2】

The Link Your Classhttps://bbs.youkuaiyun.com/forums/MUEE308FZU202201
The Link of Requirement of This Assignmenthttps://bbs.youkuaiyun.com/topics/608734907
The Aim of This AssignmentKeywords Extraction & Counting
MU STU ID and FZU STU ID20124007_832002128

1.PSP FORM 🧾

Personal Software Process StagesEstimated Time/minutesCompleted Time/minutes
Planning--
Estimate2035
Development--
Analysis8090
Design Spec5055
Design Review1515
Coding Standard3020
Design9080
Coding11001200
Code Review Planning8070
Test2020
Reporting--
Test Report5050
Size Measurement1010
Postmortem&Process Improvement12060
total16651615

2.Problem-Solving Ideas

Programming Language:Python
Because the problem is mainly object-oriented, and I systematically learned object-oriented C++ programming on the basis of Python, so I use Python to do this laboratory.


3.Design and implementation process

we need to know the keywords about C++:

autobreakcasecharlevelstleveltinuedefaultdo
doubleelseenumexternfloatforgotoif
intlongregisterreturnshortsignedsizeofstastic
structswitchtypedefunionunsignedvoidvolatilewhile

-The file is read in the specified address
-Find the keywords
-Calculate the number of keywords and the times of switches and cases
-Use pointer function probably


4.Design and implementation process

Five modules, each encapsulated in a function, and as the level of completion increases, the implementation of each functional level depends on the implementation of the previous functional level. The diagram below shows how the code is organized, the arrows indicate the level of completion, all the functions defined in the code in the flowchart.

Created with Raphaël 2.3.0 read() preprosessing solve Count UserInterfaceHiddenCode Count Keywords(Level 1) Count Switch-Case(Level 2) Count Selection Total(Level 3) Count If-Else(Level 3) Count If-Else if-Else(Level 4)

5.Code Description

(1)create a test-file:

The code is as follows :

#include <stdio.h>  
int main(){
	int i=1;
    double j=0; 
	switch(i){  
		case 0:
			break;  
		case 1:  
		    break;  
		case 2:  
		    break;  
		default:  
		    break;  
	}  
	switch(i){  
		case 0:
			break;
		case 1:
		    break;  
		default:  
		    break;  
	}  
	if(i<0){  
		if(i<-1){}  
		else{}  
    }  
	else if(i>0){  
	    if (i>2){}  
		else if (i==2) {}  
		else if (i>1) {}  
		else {}  
	}  
	else{  
		if(j!=0){}  
		else{}  
	}  
	return 0;  
} 

(2)entire code:

The code is as follows :

import numpy as np


KeyWords=["auto","break","case","char","levelst","leveltinue","default","do",
	"double","else","enum","extern","float","for","goto","if",
	"int","long","register","return","short","signed","sizeof","stastic",
	"struct","switch","typedef","union","unsigned","void","volatile","while"]


def main():
    # input
    path = input("file path: ")
    level = input("level of stastic (from 1 to 4): ")
    with open(path) as f:  # read a File
        code = [line.strip("\n") for line in f.readlines()]
        
    KeyWords_len = 32
    cnt = 0
    count = np.zeros(KeyWords_len, dtype=int)
    for lines in range(len(code)):
        str = code[lines]
        for i in range(KeyWords_len):
            while 1:
                if str.find(KeyWords[i]) != -1:
                    if str.find("//") != -1:
                        break
                    else:
                        pos = str.find(KeyWords[i])
                        count[i]+=1
                        str = str[pos+len(KeyWords[i]):len(str)]
                else:
                    break

    for i in range(KeyWords_len):
        if count[i] != 0:
            cnt += count[i]

    num = [25, 2, 6, 15, 9]
    x = [0, 0, -1]
    struc = np.zeros(count[num[0]], dtype=int)
    dir = ["else if", "if", "else"]
    for lines in range(len(code)):
        str = code[lines]
        while 1:
            if str.find(KeyWords[num[1]]) != -1:
                if str.find("//") != -1:
                    break
                else:
                    struc[x[0]] += 1
                    break
            if str.find(KeyWords[num[2]]) != -1:
                if str.find("//") != -1:
                    break
                else:
                    x[0]+=1
                    break
            if str.find(dir[1]) != -1:
                if str.find("//") != -1:
                    break
                else:
                    x[1]+=1
                    break
            else:
                break

    divide = np.zeros(x[1], dtype=int)
    s1 = 0
    s2 = 0
    for lines in range(len(code)):
        str = code[lines]
        while 1:
            if str.find(dir[0]) != -1:
                if str.find("//") != -1:
                    break
                else:
                    divide[x[2]]+=1
                    break
            if str.find(KeyWords[num[3]]) != -1:
                if str.find("//") != -1:
                    break
                else:
                    x[2]+=1
                    break
            else:
                break

    for m in range(x[1]):
        if divide[m] == 0:
            s2+=1
        else:
            s1+=1

    if int(level) >= 1:
        print("KeyWords_len num is: ", cnt)
        for j in range(KeyWords_len):
            if count[j] != 0:
                print(KeyWords[j], " num: ", count[j])
    if int(level) >= 2:
        if count[num[0]] != count[num[2]]:
            print("wrong structure")
        else:
            print("The number of switch structure is:",count[num[0]])
        for k in range(count[num[0]]):
            print("switch ", k+1 ," number of case is: ", struc[k])
    if int(level) >= 3:
        print("the number of if-else: ", s2)
    if int(level) >= 4:
        print("the number of if-else if-else: ",s1)
        default: print("wrong")


if __name__ == '__main__':
    main()

(3)output

在这里插入图片描述


Summary

Through this experiment homework, I’ve gained a lot:
-How to use Github
-Planning before coding is so important
-Coding in the process of consolidating and reviewing previously learned professional courses.

The knowledge points of the document process need to be further strengthened.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数学:人类精神虐待(゚Д゚)ノ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值