EE308_LAB2

Wei_Liu_19103832 Wei_Liu_19103832 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
The Link Your Class https://bbs.csdn.net/forums/MUEE308FZ
The Link of Requirement of This Assignment https://bbs.csdn.net/topics/600798588
The Aim of This AssignmentExtract keywords of different levels from the C or C++ code files that are read in.
MU STU ID and FZU STU ID19103832_831901226

The whole code in my Github: https://github.com/WeiLiu1677994513/EE308_LAB2

PSP form

Personal Software Process StagesEstimated Time(minutes)Completed Time(minutes)
Planning--
Estimate510
Development--
Analysis6090
Design Spec6080
Design Review3030
Coding Standard3020
Design2030
Coding14401440
Code Review Planning12090
Test5030
Reporting--
Test Report6060
Size Measurement3030
Postmortem&Process Improvement6030
Total19651940

Problem-solving ideas

Program Requirements

  1. Basic requirement: output "keyword" statistics
  2. Advanced requirement: output the number of "switch case" structures, and output the number of "case" corresponding to each group
  3. Uplifting requirement: output the number of "if else" structures
  4. Ultimate requirement: output the number of "if, else if, else" structures
    Before completing the more difficult requirements, you need to complete the Lower requirements.

My minds

  1. Read sample file
  2. Write each of these levels as a function
  3. Connect these functions in main function
  • I chose to write this program in C++ by Dec-C++, but the software has no functions in test coverage optimization and performance testing.

Design and implementation process

Read the file

  • The first and the most basic thing is read the file, but I can only read it line by line, which is not good for later operations, so after reading them, I store them in a vector.

  • function:

void file_read(string filename){
    ifstream inFile;
    string line;
    inFile.open(filename.c_str());
    if (!inFile.is_open()){
        cout << "\nThe file was not successfully opened"
        << "\n Please check that the file currently exists."
        << endl;
        exit(1);
    }          // check for successful open
    while (getline(inFile, line)) {
        file_content.push_back(line);
    }
    inFile.close();     //close the file
}

Delete the useless contents

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

  • function:

void delete_useless_content(){
    string Line,words;
    int index;
    size_t tabs_location,annotation_location,quot_location,star1_location,star2_location;
    for(int i=0;i<file_content.size(); i++){
        Line=file_content[i];
        if(Line.find("\t")!=Line.npos&&Line.find("\t")==0){
            do{
                tabs_location=Line.find("\t")+1;
                Line=Line.substr(tabs_location,Line.length()-tabs_location);
            }while(Line.find("\t") != Line.npos && Line.find("\t") == 0);
            file_content[i] = Line;
        }
        if (Line.find("//") != Line.npos) {    
            annotation_location=Line.find("//");       
            if (annotation_location==0) {
                file_content.erase(file_content.begin()+i);      
                i--;
            } else {
                words = Line.substr(0,annotation_location);       
                file_content[i] = words;
            }
        }else if(Line.find("\"") !=Line.npos){
            size_t temp[50]={0};
            tabs_location=0;
            index=0;
            while ((quot_location=Line.find("\"",tabs_location)) != Line.npos) {
                temp[index]=quot_location;
                index++;
                tabs_location=quot_location+1;
            }
            temp[index]=Line.length();
            words= Line.substr(0,temp[0]);
            for (int j=1; temp[j]!=0; j+=2) {
                words += Line.substr(temp[j]+1,temp[j+1]-temp[j]-1);      
            }
            file_content[i]=words;    
        } else if(Line.find("/*") != Line.npos){
            star1_location=Line.find("/*");
            star2_location= Line.find("*/");
            if (star2_location!= Line.npos) {   
                if (star1_location== 0) {
                    file_content[i] = Line.substr(star2_location+2,Line.length());
                } else {
                    file_content[i] = Line.substr(0,star1_location);
                }
                i--;
            }else{
                file_content[i] = Line.substr(0,star1_location);
                i += 1;
                Line = file_content[i];
                while (Line.find("*/") == Line.npos) {
                    file_content.erase(file_content.begin()+i);     
                    Line = file_content[i];
                }
                star2_location = Line.find("*/")+2;
                file_content[i] = Line.substr(star2_location,Line.length()-star2_location);
            }
            
        }
    }
}

Judge keywords

  • In order to prevent the error recognition caused by keywords in non-keyword fields, it is necessary to check whether the keywords are independent.I referred to the ideas of other students, using ASCII code to distinguish

  • function:

bool independence_judge(string line,long i){
    if (line[i] < 48 || (line[i] > 57 && line[i] < 65) || (line[i] > 90 && line[i] < 97) ||line[i] >122) {
        return true;
    } else {
        return false;
    }
}
bool involve_judge(string line, string word){
    size_t word_location;
    word_location=line.find(word);
    if (word_location == 0) {
        if (independence_judge(line, word.length()) || word.length() == line.length()) {
            return true;
        } else {
            return false;
        }
    } else {
        if (independence_judge(line, word_location-1) && (independence_judge(line, word_location+word.length()) || word_location+word.length() == line.length())) {
            return true;
        } else {
            return false;
        }
    }
    
}

Calculate the keywords number

void cal_keywords_num(){
    int keywords_num=0;
    string line;
    for (int i = 0; i<file_content.size(); i++) {
        line = file_content[i];
        for (int j=0; j<32; j++) {
            size_t keywords_location = line.find(keywords[j], 0);
            while (keywords_location!=line.npos && involve_judge(line,keywords[j])) {
                keywords_num++;
                keywords_location = line.find(keywords[j],keywords_location + 1);
            }
        }
    }
    cout<<"total num: "<<keywords_num<<endl;
}

Calculate switch and case numbers

void cal_switch_case_num(){
    int switch_num = 0,last = -1,case_num[200]={0};
    string line;
    for (int i = 0; i<file_content.size(); i++) {
        line = file_content[i];
        if (line.find("switch") != line.npos && involve_judge(line, "switch")) {        
            switch_num += 1;
            last += 1;
        }
        if (line.find("case") != line.npos && involve_judge(line, "case")) {        /*查找case*/
            case_num[last] += 1;
        }
    }
    cout<<"switch num: "<<switch_num<<endl;
    cout<<"case num:";
    for (int j = 0; j<=last; j++) {
        cout<<" "<<case_num[j];
    }
    cout<<endl;    
}

Calculate if-else and if-elseif-else number

  • I'm using stack matching to push 1 when I encounter if, 2 when I encounter if_else, and off when I encounter else, if the top of the stack is 1 it matches if else, if the top of the stack is 2 it matches if else_if else.
void cal_if_else_num(int level) {
    int if_else_num = 0,if_elseif_else_num= 0;
    stack<int> s;
    string line;
    for (int i = 0; i<file_content.size(); i++) {
        line = file_content[i];
        if (line.find("if") != line.npos && line.find("else") == line.npos && involve_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) {
                if_else_num++;    
            } else {
                if_elseif_else_num++;    
            }
            s.pop();       
            
        }else  if (line.find("if") != line.npos && line.find("else") != line.npos && involve_judge(line, "if")) {
            s.push(2);    
        }
    }
    if (level == 3) {
        cout<<"if-else num: "<<if_else_num<<endl;
    } else if (level == 4) {
        cout<<"if-else num: "<<if_else_num<<endl;
        cout<<"if-else-if num: "<<if_elseif_else_num<<endl;
    }
}

Unit test screenshots and description.

The program worked correctly on the sample

img

performance optimization screenshots and descriptions.

The software I use does not have this function. I tried several methods, but all failed. Due to time problem, this experiment was not carried out this part.

In summary

In this experiment, I encountered many difficulties, including the formulation of methods in the early stage, but these problems were solved by referring to other people's methods.The biggest regret is that I didn't learn how to test and optimize performance. I will try to learn this knowledge in the future.

...全文

相关推荐

EE-308 LAB4 final version.7z

这是本次实验的压缩包

晨起剪桃花

YOLO26算法煤气罐识别+训练好的模型+417张数据集+pyqt可视化界面.zip

附训练好的 YOLO 权重与 PyQt 可视化检测界面,页面底部含可视化效果预览,毕设/课设开箱即用。 【数据集概况】 · 检测类别(中文):[()] 【训练曲线与评估图】 【模型训练配置】 参数 | 值 模型 | yolo26n 训练轮数 | 100 epochs 输入尺寸 | 640x640 批次大小 | 24 优化器 | auto 初始学习率 | 0.01 训练设备 【关键指标汇总】 训练了 97 个 epoch,最终轮指标: 指标 | 数值 mAP50 | **0.8213** mAP50-95 | 0.6707 Precision | 0.8858 Recall | 0.7370 train/box_loss | 0.5927 train/cls_loss | 0.4692 val/box_loss | 0.7097 val/cls_loss | 0.8245 【训练过程分析】 训练过程中,模型在前几轮迅速收敛,loss 值从 0.5927 降到 0.7097,表明初期训练效果显著。loss 在第 10 轮降至最低点 0.5867,然后在接下来的 80 轮左右保持平稳,仅微幅波动在 0.5927 到 0.6000 之间,显示训练过程较为均衡,没有明显的过拟合迹象。整体来看,模型在训练后期趋于稳定,mAP50 从 0.6845 增长至 0.8213,提升较为平滑,显示出训练效果的稳健性。 模型在第 97 轮时,loss 稳定在较低水平,表明训练过程较为稳定,没有明显的过拟合迹象。从 PR 曲线可见,模型在检测精度和召回率方面表现均衡,特别是在高召回率时保持较高的精度。mAP50 为 0.8213,表明模型在中等难度的检测任务上表现良好。然而,mAP50-95 为 0.6707,说明在高难度检测任务... 使用场景:毕业设计、课程设计、深度学习入门、工业场景落地验证。

guo_xiao_xiao_

问:高校内部制定职务科技成果包干制管理办法,需要遵循哪些法规依据和国家标准?.docx

问:高校内部制定职务科技成果包干制管理办法,需要遵循哪些法规依据和国家标准?

BIZKEEN

SpringBoot和Vue食品安全抽检任务与结果公示平台源码

包含前后端源码、SQL初始化脚本和启动说明。JDK 17、Spring Boot、Vue;默认H2演示数据库。

毕设小屋vx ylw282426

ortoiseSVN-1.9.4.27285-x64-svn-1.9.4

- TortoiseSVN:Windows 图形外壳客户端(小乌龟) - `1.9.4.27285`:TortoiseSVN 自身编译版本号 - `x64`:64 位安装包 - `svn-1.9.4`:底层绑定 Apache Subversion 核心版本 **1.9.4**(仓库核心版本)

SimoneTao
评论
收藏 已收藏
分享
返回顶部