EE308FZ_Lab1-2_832001302_20123108

qq_52981234 2022-10-23 00:39:37
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZU202201
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/608745426
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 IDFZU ID : 832001302  MU ID : 20123108

Content

1.PSP Table

2.Description of the problem-solving ideas

3.Design and implementation process

4.Code description

5.Unit test and performance testing

6.Summarize this assignment


1.PSP Table

PSPESTIMATE TIME(MINUTE)ESTIMATE TIME(MINUTE)
Planning4530
 Estimate55
Development3050
Analysis6070
Design Spec1010
 Design Review00
 Coding Standard2015
Design4060
Coding240300
Code Review1015
 Test6080
Reporting\\
 Test Report1020
Size Measurement1010
Postmortem & Process Improvement Plan2050
Total560715

2.Description of the problem-solving ideas

        When I first got this topic, the first thing that came to my mind was what I learned in object-oriented programming last semester about reading and writing C files.

 Requirement 1 : Basic requirement  i.e.  output "keyword" statistics

          For the Requirement 1 : to read the number of keywords in a file, my idea is to use getline to read the file line by line, read all the characters, then compare the read characters to the elements in the keyword array, increment the total number of keywords if included, and finally output the total number of keywords in the file.

Requirement 2 : output the number of "switch case" structures, and output the number of "case" corresponding to each group

         For the Requirement 2 : Actually, step 2 is easier when you've done step 1. After finding the switch, find the next switch and the case between the two switches, count the number of cases between the two switches, use two for loops (i, j), when j encounters the next switch, assign the value to i, and carry out the next iteration, Iterate all the way to the end.

Requirement 3 :  Uplifting requirement  i.e.  output the number of "if else" structures

Requirement 4 :   Ultimate requirement: output the number of "if, else if, else" structures

        I put the Requirement 3 and Requirement 4 together. In fact, I think the two questions are similar, and the answer to both questions can be calculated by an algorithm:

        When I first got this question, my mind was completely blank. I thought about dividing them into basic units. I thought that all complex structures are the combination of basic units, but later I thought it was not feasible.

         Next, it occurred to me that if does not necessarily need to be accompanied by else, but the presence of else and else if must be predicated on the existence of if. This idea gave me the initial enlightenment, and I had an idea of inferring if by else and thus introducing else if, but I did not give it a try either, but this idea gave me the inspiration to look for the relationship between if,else if,else relationship, which helped me to get the final algorithm:

          As with the previous solutions, I want to extract all the if,else, and else if in the file. (NOTE : Make sure that the else and if are not repeatedly extracted while the else if is present. The detailed code will be shown in the following section.)

  • Step 1: if the else is followed by an if, it can be considered an if-else structure. The number of if-else structures is increased by one, and the number of if and else is decreased by one。
  • Step 2: if the else is followed by the else if, then it might be an if-else if-else structure, but at this point we need to count the number of times the else if is repeated, and this is used to count how many times we need to reduce the number of else if (NOTE : if the else if is repeated n times, The else if is reduced by n+1.)
  • Step 3: Leave the first one in the else if of the repeating  unit, and determine whether this else if is followed by an if. if so, it is an if-else if-else structure, and the number of if-else if-else structures is increased by one.
  • Step 4: The rest of the else if must come from the if-else if-else structure, the number of if-else if-else structure is increased by one.

  • Step 5: The remaining if must come from the if-else structure, and the number of if-else structures is increased by one.

3.Design and implementation process

The Flow Chart is shown below

 4.Code description

All code including test file(named file1.cpp) is uploaded on GitHub

Click here to get the code!

Header File and Initialization

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{	
	string text;
	vector<string> nums; 
	string filename;
	string keywords[33]={"auto", "break", "case", "char", "const", "continue",
		"default", "do", "double", "else", "enum", "extern", "float",
		"for", "foto", "if", "int", "long", "main", "register", "return", "short",
		"signed", "sizeof", "static", "struct", "switch", "typeof", "union",
		"unsigned", "void", "volatile", "while"};
	string ifelsekeywords[3]={"else if","if","else"};
	int keywdcnt[33] = {0};
	int ifelsekeywdcnt[3] = {0};
	int keywordcount=0;	
	int switchcount=0;
	int casecount=0;
	int elseifcount=0;
	vector<int> casenum;
	vector<string> ifelsearr; 
	int ifelsecount=0;
	int ifelseifcount=0;
	int ifnum=0;
	int elseifnum=0;
	int elsenum=0;

Input File Name , Choose Processing Level  , Open File 

    ifstream inFile;
	cout<<"Please type the file name: ";
	cin>>filename;
	cout<<"Please choose the level(1,2,3,4): ";
	int level;
	cin>>level;
	inFile.open(filename.c_str());
	if(inFile.fail())
	{
		cout<<"The file was not successfully opend"<<endl;
		exit(1);
	}
	string buf;

 Requirement 1 : Basic requirement  i.e.  output "keyword" statistics

	//part 1
	while (getline(inFile,buf))
	{
		cout << buf << endl;
		int i = 0, j = 0;	
		while(i < int(buf.size()) && j < int(buf.size())) 
		{
			if (buf[i] < 'z' || buf[i] > 'a') 
			{
				if (buf[j] > 'z' || buf[j] < 'a') 
				{
					string s = buf.substr(i, j-i);
					nums.push_back(s);
					for (int k = 0; k < 33; k++) {
						if (s == keywords[k]) {
							keywordcount++;
							keywdcnt[k]+=1;
						}
					}
					i = j+1;j=i;
				}
				else
					j++;
			}
			else
				{i++;j=i;}
		}
	}
	if(level>=1)
	cout<<"total number of keywords is: "<<keywordcount<<endl;

Requirement 2 : output the number of "switch case" structures, and output the number of "case" corresponding to each group

	//part 2
	if(level>1)
	{
		for(int i=0;i<int(nums.size());i++)
		{
			if(nums[i]=="switch")
			{
				switchcount+=1;
				//cout<<switchcount<<endl;
				for(int j=i+1;j<int(nums.size());j++)
				{
					//cout<<"nums[j] is now: "<<nums[j]<<endl;
					if (nums[j]=="case")
						casecount+=1;
						//cout<<"casecount is now: "<<casecount<<endl;}
					else if(nums[j]=="switch" )
					{
						i=j-1;
						casenum.push_back(casecount);
						casecount=0;
						break;
					}
					else if(j==int(nums.size()-1))
					{
						i=j;
						casenum.push_back(casecount);
						break;
					}
				}
			}
		} 
			cout<<"total number of switch is: "<<switchcount<<endl;
			cout<<"total number of case is: ";
			for(int i=0;i<int(casenum.size());i++)
			{
				cout<<casenum[i]<<"  ";
			}
			cout<<endl;
			inFile.close();
	}

Requirement 3 :  Uplifting requirement  i.e.  output the number of "if else" structures

Requirement 4 :   Ultimate requirement: output the number of "if, else if, else" structures

		//part 3 & 4
		if(level>2)
		{
			inFile.open(filename.c_str());
			if(inFile.fail())
			{
				cout<<"The file was not successfully opend";
				exit(1);
			}
			while (getline(inFile,buf))
			{
				for(int i=0;i<3;i++)
				{
					if(int(buf.find(ifelsekeywords[i]))!=-1)
						ifelsekeywdcnt[i]=1;
					else
						ifelsekeywdcnt[i]=0;
				}	
				if(ifelsekeywdcnt[1]==1 && ifelsekeywdcnt[2]==1)
				{
					ifelsekeywdcnt[0]=1;
					ifelsekeywdcnt[1]=0;
					ifelsekeywdcnt[2]=0;
				}	
				for(int i=0;i<3;i++)
				{
					if(ifelsekeywdcnt[i]==1)
						ifelsearr.push_back(ifelsekeywords[i]);	
				}
			}
			for(int i=0;i<int(ifelsearr.size());i++)
			{
				if(ifelsearr[i]=="if")
					ifnum+=1;
				else if(ifelsearr[i]=="else if")
					elseifnum+=1;
				else if(ifelsearr[i]=="else")
					elsenum+=1;
			}
			for(int i=0;i<int(ifelsearr.size())-1;i++)
			{
				if(ifelsearr[i]=="if" && ifelsearr[i+1]=="else")
				{
					ifelsecount+=1;
					ifnum-=1;
					elsenum-=1;
				}	
				else if(ifelsearr[i]=="else if" && ifelsearr[i+1]=="else")
				{
					ifnum-=1;elsenum-=1;
					for(int j=i;j>0;j--)
					{
						if(ifelsearr[j]=="else if" && ifelsearr[j-1]=="else if")
						{
							elseifcount+=1;
						}
						else
						{
								ifelseifcount+=1;
							elseifnum-=(elseifcount+1);
							elseifcount=0;
							break;
						}
					}		
				}	
			}
			ifelseifcount+=elseifnum;
			ifnum-=elseifnum;
			ifelsecount+=ifnum;
			if(level>2)
				cout<<"total number of if-else structure is: "<<ifelsecount<<endl;
			if(level>3)
				cout<<"total number of if-else if-else structure is: "<<ifelseifcount<<endl;
			//finish
		}
		return 0;
}


 Sample Test File  &   Output

#include <stdio.h>
int main()
{
	int i = 1;
	double j = 0;
	long f;
	switch (i)
	{
	case 0:
		break;
	case 1:
		break;
	case 2:
		break;
		default:
			break;
	}
	switch(j)
	{
	case 0:
		break;
	case 1:
		break;
		default:
			break;
	}
	if(i < 0)
	{
		if(i < -1){};
		else{};
	}else if(i > 0)
	{
		if(i >2){};
		else if(i == 2){};
		else if(i>1){};
		else{};
	}else
	{
		if(j != 0){}
		else{}
	}
	return 0;
}

5.Unit test and performance testing

 

 

6.Summarize this assignment

         After this lab, I think it is not easy to complete a project, we have to choose our own suitable programming language to solve the corresponding problem. Moreover, the idea of solving the problem is very important, we need to do a complete idea description and preparation before the whole project. But, after all, I really benefited a lot and my programming level will be improved. 

 

 

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

285

社区成员

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

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