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 stackstack <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.