EE308 LAB1-2

m0_58044420 2022-10-27 22:15:00

LAB1-2

The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZ
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/608734907
The Aim of This AssignmentTo achieve a program function, it can extract keywords of different levels from the C or C++ code files that are read in.
MU STU ID and FZU STU ID20123949_832002115

目录

  • LAB1-2
  • guithub link
  • PSP table
  • Problem solving ideas
  • Design and implementation process
  • The key code description
  • Unit test
  • Performance Optimization Analysis
  • Summarize

https://github.com/GTXGTA/EE308_LAB1_2

PSP table

PSPPersonal Software Process tagesEstimated time/hourActual time/hour
Planningplan
EstimateEstimate how long the task will take0.50.75
Developmentexploitation
AnalysisRequirements analysis (including learning new technology)1.52
Design SpecGenerating design documentation12
Design Reviewdesign review11
Coding StandardCode specification (appropriate specification for current development)1.52.5
Designdetail design1.51
Codingabsolute coding38
Code Reviewcode review12
TestTest (self test, change code, commit changes)39
ReportingReporting2.52
Test ReportTest Report11
Size Measurementcomputational effort1.51.5
Postmortem & Process Improvement PlanSummarize afterwards1.52.5
total20.535.25

Problem solving ideas

  • File input
    • To read in a code file, we first need to figure out how to read the contents of the file.
    • I searched the Internet and learned about reading files.
  • Cut the string
    • When the file is read in, it is read in line, and the string needs to be cut into individual words to detect.
    • So I should know which ones should be used as cut identifiers in c++/c files.
  • Keyword detection
    • I might need an array of constants as a dictionary for my later code to look up in that dictionary.
  • On switch-case detection.
    • I can use a counter to calculate.
    • When a switch occurs, it means that the previous switch structure will not appear case again, case num is listed, and the counter is reset.
    • I think the queue size should be the number of switch structures.
  • On if-else and if-else if-else detection
    • When I think about it, I think there's an if-else structure or an if-else if-else structure when it ends with an else.
    • So I should use a stack to handle it.

Design and implementation process

The way I organize my code is around the Code_test class.

img

The key code description

  • class Code_test

    • My Code_test class encapsulates the main function, cut function, keyword detection,if-else structure and if-else if-else structure with count and output functions.
    class Code_test
    {
        public:
            Code_test(){}
            bool Divide(int &index);
            void T1(string divided_unit);
            void T2(int &index);
            void T_File(string filename);
            void Display(int level);
        private:
            string str_line,word;
            int Keyword_Count;
            ifstream Infile;
            ofstream OutFile;
            stack <int> if_else_Stack;
            queue <int> switch_Queue;
            bool Is_Previous_Else;
            
            int If_else_count;
            int If_elseif_else_count;
            int Switch_Count;
    }; 
    
    • Divide function

      bool Code_test::Divide(int &index)
      {
          if(str_line[index] == '"')
          {
              int j = index;
              for(index = index + 1; index < str_line.size(); index++)
              {
                  if(str_line[index] == '"') return true;
              }
              index = j;
              return true;
          }
          if(str_line[index] == '/' && str_line[++index] == '*')
          {
              for(index = index + 1; index < str_line.size(); index++)
              {
                  if(str_line[index] == '*' && str_line[++index] == '/') return true;
              }
              while(getline(Infile, str_line))
              {
                  for(index = 0; index < str_line.size(); index++)
                  { 
                      if(str_line[index] == '*' && str_line[++index] == '/') return true;
                  }        
              }
          }
          if(str_line[index] == '(' || str_line[index] == ')') return true;  
          else if(str_line[index] == '<' || str_line[index] == '>') return true;
          else if(str_line[index] == '&' || str_line[index] == '*') return true;   
          else if(str_line[index] == '{' || str_line[index] == '}') return true;  
           
          
          else if(str_line[index] == '\n'|| str_line[index] == '\0' || str_line[index] == '\t') return true;  
          else if(str_line[index] == ' ' || str_line[index] == ',' || str_line[index] == ';' || str_line[index] == ':') return true;  
          return false;
      }
      
    • T1 function

      void Code_test::T1(string divided_unit)
      {
          for(int i = 0; i < 32; i++) if(divided_unit == kKeyWordName[i]) Keyword_Count++;
          if(divided_unit == "switch" && Switch_Count)  
          {
              switch_Queue.push(Switch_Count);
              Switch_Count = 0;
          }
          if(divided_unit == "case") Switch_Count++;
          if(divided_unit == "if") 
          {
              if_else_Stack.push(-1);
              if_else_Stack.push(1);
          } 
          if(divided_unit == "else") Is_Previous_Else = true;
      }
      
    • T2 function

      void Code_test::T2(int &index)
      {
          for(index; index < str_line.size(); index++) if (!(str_line[index] == ' ' || str_line[index] == '\t')) break;
          if(index == str_line.size())
          {
              int flag = 0;
              while (getline(Infile, str_line))
              {
                  for(index = 0; index < str_line.size(); index++)
                  {
                      if (!(str_line[index] == ' ' || str_line[index] == '\t'))
                      {
                          flag = 1;
                          break;
                      }
                  }
                  if(flag) break;
              }    
          } 
          if(str_line[index] == 'i' && str_line[index+1] == 'f') 
          {
              if_else_Stack.push(1);
              index++;
              Keyword_Count++;
          }
          else
          {
              int ifelse_stack_count_ = 0;
              while(if_else_Stack.top() > 0 && !if_else_Stack.empty())
              {
                  ifelse_stack_count_ += if_else_Stack.top();
                  if_else_Stack.pop();
              }
              if(ifelse_stack_count_ > 1) If_elseif_else_count++;
              else if(ifelse_stack_count_ == 1) If_else_count++;
          }                
      }
      

Unit test

Below are the unit test results for levels 1,2,3, and 4, respectively.

  • Level 1 unit test results.

    img

  • Level 2 unit test results.

    img

  • Level 3 unit test results.

    img

  • Level 4 unit test results.

    img

Performance Optimization Analysis

As you can see from the figure below, the functions that occupy more resources are Divide function,T1 function,T_File function.

img

img

Summarize

  • Learning and Gain
    • In this practice, I learned to master some usage methods of github and csdn, and consolidated my markdown related syntax, so that I am more skilled in using them.
    • At the same time, I also practice the keyword detection of c++, for the programming, the if_else judgment, and the string cutting problem all let me benefit a lot.
  • Difficulties encountered and solutions
    • Very inexperienced with github
      • I tried to learn the related use methods of github on the Internet, and spent time simply learning and understanding some basic use methods of github.
    • Programming problems
      • When writing the c++ keyword recognition program, I have a lot of doubts about the method of identifying keywords. In the case of searching information online and asking surrounding students, I basically understand the solution and ideas. At the same time, I encountered many errors when trying to write the code. After my careful elimination, I successfully solved the problems I encountered.
...全文
48 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_52008789 2022-10-27
  • 打赏
  • 举报
回复 1

luyiniubi

285

社区成员

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

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