183
社区成员




The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZ?category=0 |
---|---|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/600798588 |
The Aim of This Assignment | Git/GitHub Using & Keywords Capturing |
MU STU ID and FZU STU ID | 19104251_831901213 |
https://github.com/JiXianDaDa/EE308-LAB2
Personal Software Process Stages | Estimated time(min) | Time(min) |
---|---|---|
Planning | 20 | 20 |
Estimate | 10 | 10 |
Development | - | - |
Analysis | 180 | 200 |
Design Spec | 10 | 15 |
Design Review | 10 | 15 |
Coding Standard | 30 | 40 |
Design | 40 | 60 |
Coding | 300 | 440 |
Code Review | 60 | 110 |
Test | 300 | 1200 |
Test Report | 60 | 110 |
Postmortem & Process Improvement Plan· Design Review | 20 | 20 |
Summary | 1040 | 2240 |
In total, this problem can be broken down into five small questions:
(1).Read in a c/c++ code file.
(2).Count the number of keywords in the code.
(3).Count the number of "switch case" structures and the number of "case" corresponding to each group.
(4).Count the number of "if else" structures.
(5).Count the number of "if, else if, else" structures.
For (1), we can just use class "BufferedReader" and "FileInputStream" to realize.
And the first step to solve other four questions is to use some operations to modify the original content (c/c++ code which was read in). Then the modified content will be easier for me to solve these four questions.
So I use some operations (Regular Expression, String relevant operations, etc.) to modify the input code and I will show this process later in this blog.
After obtaining modified input, Then the main idea to resolve these four questions is to find the traits of each request:
For (2), it asks us to count the number of keywords, so we can just search the content and use a judge to realize this.
For (3), we should firstly count the number of keyword "switch" and this is the same as (2). And the number of case can be count .
For (4) and (5), we should find out how to convert the number of sturctures into the number of keywords "if", "else if" and "else".
To implement this program, I chose to use the C++ language I'm most familiar with
string key_word[32]=
{
"auto","break","case","char","const","continue","default","double",
"do","else","enum","extern","float","for","goto","if","int","long",
"register","return","short","signed","sizeof","static","struct",
"switch","typedef","union","unsigned","void","volatile","while"
};
vector<int> Case_num;
int switch_num=0;
int case_num=0;
int total_num=0;
int stack[1000]={0};
int ifelse_num=0;
int if_elseif_Else_num=0;
int top=-1;
int Judge_1 ( string x, string y )
{
int Judge_2( char str );
int position = x.find( y , 0 );
int len = y.length();
if( position != string::npos )
{
if( position == 0 )
{
if(Judge_2( x[position+len] ) == 0 )
{
return 1;
}
else
return 0;
}
else
{
if(Judge_2( x[position+len] ) == 0 && Judge_2( x[position-1] ) ==0 )
{
return 1;
}
else
return 0;
}
}
return 0;
}
int Judge_2( char str )//Check if it's a letter, return 1, not 0
{
if( ( str<='z' && str >= 'a' ) || ( str <= 'Z' && str >= 'A' ) )
return 1;
else
return 0;
}
void rank_12(string str)//Core character comparison, and count the number of switch-case
{
for(int i = 0; i < 32; i++ )
{
if(Judge_1( str , key_word[i] ) == 1)
{
if( Judge_1( str , "switch" ) )
{
Case_num.push_back( case_num );
switch_num++;
case_num = 0;
}
if( Judge_1( str , "case" ))
{
case_num++;
}
total_num++;
break;
}
}
}
void rank_34( string str )//if_else count and if_elseif_else nested count
{
if( Judge_1( str , "else if" ) )
{
top++;
stack[top] = 2;
}
else
{
if( Judge_1( str , "else" ) )
{
if( stack[top] == 1 )
{
ifelse_num++;
top--;
}
else
{
if( stack[top] == 2 )
{
if_elseif_Else_num++;
top--;
}
}
}
else
{
if( Judge_1( str ,"if" ) )
{
top++;
stack[top] = 1;
}
}
}
}
int main()
{
int Rank;
string Myfile,temp;
cout << "Please enter the file path:" << endl;
cin >> Myfile;
cout << "Please enter the query level:" << endl;
cout << "1--Totalkeyword query" << endl;
cout << "2--Switch-case query" << endl;
cout << "3--If_else query" << endl;
cout << "4--IF_elseif_Else query" << endl;
cin >> Rank;
ifstream myfile( Myfile.c_str() );
int Judge_1( string x,string y );
int judge_2( char str);
void rank_12( string str );
void rank_34( string str );
while ( getline( myfile ,temp ))
{
istringstream is ( temp );
string s;
if( Rank >= 3)
{
rank_34( temp );
}
while( is >> s )
{
rank_12( s );
}
}
if( Rank >= 1 )
{
cout << "Total num:" << total_num << endl;
}
if( Rank >= 2 )
{
cout << "Switch num:" << switch_num << endl;
if( !Case_num.empty() )
{
Case_num.push_back(case_num);
}
else
{
Case_num.push_back(0);
}
cout << "Case_num :";
for ( int i = 1; i <= switch_num; i++)
{
cout<< Case_num[i] << " ";
}
cout << endl;
}
if( Rank >=3 )
{
cout << "If-else num:" << ifelse_num << endl;
}
if( Rank >=4 )
{
cout << "If-elseif-else num:" <<if_elseif_Else_num;
}
}
This experiment was a little difficult for me. I could only finish it after consulting seniors from other colleges and getting replies.I want to learn a lot of things, I hope to be more skilled in the use of various codes, and post and GITHUB use can be more skilled.