EE308_Lab1

砍柴人。 2021-09-24 03:39:03
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZ?category=0
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/600798588
The Aim of This AssignmentGit/GitHub Using & Keywords Capturing
MU STU ID and FZU STU ID19104251_831901213

GitHub Repository

https://github.com/JiXianDaDa/EE308-LAB2

 

PSP

Personal Software Process StagesEstimated time(min)Time(min)
Planning2020
Estimate1010
Development--
Analysis180200
Design Spec1015
Design Review1015
Coding Standard3040
Design4060
Coding300440
Code Review60110
Test3001200
Test Report60110
Postmortem & Process Improvement Plan· Design Review2020
Summary10402240

 

Program Requirements

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

 

Problem-Solving Ideas

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



Create an array

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"
};

Create a function 

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;
}

     

    Check for letter

    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;
    }

    Compare

    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;
    			}
    		}
    	}
    }
    
    

    work

    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;
    	}
    }

    result

     

    In Summary

    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.

    ...全文
    281 回复 打赏 收藏 转发到动态 举报
    AI 作业
    写回复
    用AI写文章
    回复
    切换为时间正序
    请发表友善的回复…
    发表回复

    183

    社区成员

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

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