285
社区成员
发帖
与我相关
我的任务
分享| The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZ |
|---|---|
| The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/608734907 |
| The Aim of This Assignment | To 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 ID | 20123949_832002115 |
https://github.com/GTXGTA/EE308_LAB1_2
| PSP | Personal Software Process tages | Estimated time/hour | Actual time/hour |
|---|---|---|---|
| Planning | plan | ||
| Estimate | Estimate how long the task will take | 0.5 | 0.75 |
| Development | exploitation | ||
| Analysis | Requirements analysis (including learning new technology) | 1.5 | 2 |
| Design Spec | Generating design documentation | 1 | 2 |
| Design Review | design review | 1 | 1 |
| Coding Standard | Code specification (appropriate specification for current development) | 1.5 | 2.5 |
| Design | detail design | 1.5 | 1 |
| Coding | absolute coding | 3 | 8 |
| Code Review | code review | 1 | 2 |
| Test | Test (self test, change code, commit changes) | 3 | 9 |
| Reporting | Reporting | 2.5 | 2 |
| Test Report | Test Report | 1 | 1 |
| Size Measurement | computational effort | 1.5 | 1.5 |
| Postmortem & Process Improvement Plan | Summarize afterwards | 1.5 | 2.5 |
| total | 20.5 | 35.25 |
The way I organize my code is around the Code_test class.

class Code_test
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++;
}
}
Below are the unit test results for levels 1,2,3, and 4, respectively.




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


luyiniubi