EE308 Lab2

zzz 2021-09-23 15:01:23
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZ?category=0
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/600798588
The Aim of This AssignmentCode personally & learn git and github & learn the process of writing a project & learn unit test and performance test
MU STU ID and FZU STU ID19105886       &831902204

    My Github Library

 https://github.com/zxc123asd456/nova

 

PSP FROM

Personal Software Process StagesEstimated Time (minutes)Actual Time (minutes)
Planning2015
Estimate2020
Development00
Analysis240360
Design Spec00
Design Review00
Coding Standard3030
Design6050
Coding120150
Code Review6060
Test6060
Test Report00
Size Measurement2015
Postmortem & Process Improvement Plan2020
Total650780

Idea of solving problem

The reconsider and be prepared to think about it in terms of text matching. According to the requirements, due to the characteristics of THE C language, the format of the code is changeable, the requirements of the code only meet the basic condition can be compiled, so in the white space and so on the processing should be very careful. Therefore, after reading in the function, we need to do some special formatting for the function. Firstly, we need to remove the content of the comment part, and secondly, we need to process the string. All the strings in the code are replaced to prevent the comment or the content in the string from being matched.

Keyword judgment
It took a long time to reintroduce the DFA I had thought of before, mainly learning the ideas and code implementation. The pre-processed code section is passed in, and a DFA tree is built through DFA, followed by keyword matching.

The switch to determine
After discovering that the code block is formatted, you only need to match switch() {through regular expression, match the corresponding position of the close parenthesis through parenthesis matching, and then count the number of case occurrences in the code segment in parenthesis using regular expression again to achieve the function.

if-else if-else if-else
This part is the most difficult part in this homework, also need to consider the details of the most part, I find some relevant information about the compiler code, but because the compiler is service for compilation, so in the process of dealing with many other factors, far more than the requirements of the job, so you need to make their own design. In the process of thinking, referring to the idea of state matching in DFA, that is, through the transfer of state between if, else if and else to complete the statistical work of the code, I designed the following state transition diagram based on the examples given:

 

Design implementation process

First we read the file, it is easy so let's just do it in the main method.

cout<<"输入文件路径:";    
    cin>>filename;    
    ifstream fin(filename.c_str());   
    if ( ! fin)
        {
            cout << "file open" error <<endl;
        }
    else
        {
            while (getline(fin,temp))
                {      
                Keywords(temp);              
                }
            fin.close();  
        }

Second Keyword record
Because only keywords and {} symbols are needed, only these are recorded. If the character is a letter, continue reading until it is a space, and then compare it with the keyword table. If it is one of the characters, note it down.

 keyword[32] = { "auto","break","case","char","const", "continue","default","do", 
                 "double","else","enum","float","for","goto","if","int",            

"long","register","while","reuturn","short","signed","sizeof","static",  

"struct","switch","typedef","union","unsigned","void","volatile","while",};

 

third output

void Print()
{
    int sum;//total num
    for (int i = 0; i < 32; i++)
    {
        if (p[i].number != 0)
        {
            sum += p[i].number;
        }
    }
    cout << "total num: " << sum << endl;
        bool Case_flag = false;
        cout << "switch num: " << switch_number << endl;
        cout << "case num: ";
        for (int i = 0; i < 50; i++)
        {
            if (case_number[i] != 0)
            {
                cout << case_number[i] << " ";
                Case_flag = true;
            }
        }
        if (Case_flag == false)
        {
            cout << "0";
        }
        cout << endl;
        cout << "if-else num: " << ifelse_number << endl;
        cout << "if-elseif-else num: " << ifelseifelse_number << endl;
}

The test results

 

Summary

For this assignment, at first look at the words a little confused, after all, seems to have not played code for a long time. After thinking, I came up with an idea, but thinking is not the same as making it. Practice is a hard process, which is a good help for training my ability and mentality. I tried many methods, but encountered many difficulties.I hope to make the soft engineering practice of this semester more meaningful and exercise my ability in continuous learning.

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

183

社区成员

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

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