EE308FZ LAB_1_2

EE308FZ_Lab_2

1.Basic Information

The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZU202201
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/608734907
The Aim of This Assignment concrete contentKeywords and function matching
MU STU ID and FZU STU ID20123337_832002126

Github Link:   https://github.com/LittleMatcher/EE308FZ_LAB

Specificatione.g.
Variable namingint price_count_reader;
Maximum number of characters per line80
Maximum number of function lines50
Function and class namingAddTableEntry() File
constantconst int kDaysInAWeek = 7;
Blank line ruleBetween the functions
Annotation rules//
Space before and after operatorYes

2.PSP

Personal Software Process Stageswhat should I dotime(min)
PlanningUnderstand the purpose of the experiment and plan the time10
AnalysisDraw the basic framework of the project10
Design ReviewWrite the key function70
DevelopmentReduced memory footprint and time optimization20
PostmortemFinish the report50

3.Problem-solving ideas

    As can be seen from the title, this experimental project involves text matching, so we can preliminarily decide to use Regular Expressions. After analyzing the test files given in the document, keyword matching can be performed through traversal. Since all related functions of if end with else, we can think of stack – first in, last out; switch has no good idea, the preliminary consideration is also to use the stack.

4.Design and implementation process

  1)Code framework

在这里插入图片描述

  2)Flow chart

   ( a )void countKey(String str)

在这里插入图片描述

   ( b )void countFunSwitch(stack switchCase)

在这里插入图片描述

   ( c )void countFunIf(stack str_stack)

在这里插入图片描述

   

5.Code

  1)Read files with functions used

  Push the string onto the stack.
  Converts each line in the file to a string.
  To avoid confusion between switch and if, two stacks were created to store the two functions separately

void File::LodeFile(const string& path)
{
    FILE* p = fopen(path.c_str(), "rb");
    if (!p)
        exit(0);
    char str[N + 1];
    strcpy_s(str, path.c_str());
    stack <string> str_stack; //Create a stack
    stack <string> switchCase;
    while (fgets(str, N, p) != NULL)
    {
        //printf("%s", str);
        //cout << str ;
        countKey(str);
        if (isEI(str)) {
           // cout << "ELSE IF" << endl;
            str_stack.push("else if");
        }
        else if (isElse(str)) {
           // cout << "ELSE" << endl;
            str_stack.push("else");
            countFunIf(str_stack);
        }
        else if (isIf(str)) {
           // cout << "IF" << endl;
            str_stack.push("if");
        }
        else if (isSwitch(str)) {
            //cout << "SWITCH" << endl;
            switchCase.push("switch");
        }
        else if (isCase(str)) {
           // cout << "CASE" << endl;
            switchCase.push("case");
        }
        else {
        }
    }
    countFunSwitch(switchCase);
    fclose(p);

}

  2)Keywords Counting

  This paragraph uses the simplest method - exhaustive. Store all the keywords in an array of strings and iterate through them to match the strings passed in.

void countKey(string s) {

    string keyword[32] = { "auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern",
                "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static",
                "struct", "switch", "typedef", "unsigned", "union", "void", "volatile", "while" };
    s = "   " + s;
    for (int i = 0; i < 32; i++) {
        regex pattern("[^a-zA-Z0-9_]" + keyword[i] + "[^a-zA-Z0-9_]");
        smatch result;
        bool ismatch = regex_search(s, result, pattern);
        if (ismatch) {
            countKeyNum++;
            //cout << keyword[i] << endl;
        }
    }
}

  3)Switch and case Counting

  This function runs after a loop that reads the file. If the file contains the switch function, all keywords have been pushed on the stack.Build an array to store the number of cases,index is the number of switch, when pop to “case”, then caseNum[switchNum]++; When pop to switch, SwitchNum+ +.

void countFunSwitch(stack <string> switchCase) {
    while (!switchCase.empty()) {
        while (switchCase.top() == "case") {
            caseNum[switchNum]++;
            switchCase.pop();
        }
        if (switchCase.top() == "switch") {
            switchNum++;
            switchCase.pop();
        }
    }
 
}

  4)If Counting

  Notice that all if-related functions start with if and end with else. When an “else” appears in the stack, pop can be started up to the nearest “if” . if “elseif” appears in the middle, ifelseif_num++; If not, ifelse_num++;

void countFunIf(stack <string> str_stack) {
    int count = 0;
    while (!str_stack.empty()&&str_stack.top() != "if") {
        if (str_stack.top() == "else if") {         
            count++;
        }
        
        str_stack.pop();
       
    }
    if (count == 0) {
        ifelse_num++;
    }
    else {
        ifelseif_num++;
    }
    if(!str_stack.empty()) {
        str_stack.pop();
    }
    

}

6.Unit test screenshots and description

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

7.Optimize

 There is too little code to have a significant impact on performance, so there is no optimization.
在这里插入图片描述

8.Summary

  I feel that the overall idea is correct and meets the experimental requirements. I haven’t written code for too long. I can’t realize the code quickly after I have ideas. Test case coverage may not be comprehensive, and you don’t know what bugs will occur.

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页
评论

打赏作者

小火柴x_x

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值