EE308 LAB1-2

激动的小马劈叉 2022-10-28 00:08:00
The Link Your ClassMy class link
The Link of Requirement of This AssignmentLAB 1-2
The Aim of This AssignmentKeywords extraction and counting
MU STU ID and FZU STU ID20122187 832001215

Github :link to Github

目录

1.PSP form

2.Description of problem-solving ideas

3. Design and implementation process

4.code

5.output

6.summary


 

1.PSP form

PSPESTIMATE TIME(MINUTE)ESTIMATE TIME(MINUTE)
Planning3045
Estimate8060
Development4030
Analysis1520
Design Spec1010
Design Review105
Coding Standard1010
Design6030
Coding120150
Code Review510
Test6060
Reporting2020
Test Report1020
Size Measurement55
Postmortem & Process Improvement Plan55
Total480490

2.Description of problem-solving ideas

       Because I am familiar with c++ , I plan to use c++ to solve this problem 

       I plan to divide this project into the following five parts.

       1.Read the file and save it in a string array.

       2.Using the getline function  to spilt each word.

       3.Using the find function to determine the keywords and count keywords.

       4.Determine the output result based on the conditions.

       5.Do unit testing.

3. Design and implementation process

 

4.code

 

(1)initialize and enter keywords

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    int count_keyword=0;//the number of keyword
    int count_switch=0;//the number of switch
    int count_switch_case[100]={0};//to store the number of case in each switch
    int count_if_else=0;//the number of if-else structure
    int count_if_elseif_else=0;//the number of if-elseif-else structure
    int flag[100]={0};
    int level;
    string line;
    string filename;
    ifstream inFile;
    cout<<"Please input the file name: ";
    cin>>filename;
    cout<<"Please input the level: ";
    cin>>level;
    inFile.open(filename.c_str());
    //test if the file opens
    if(!inFile.is_open())
    {
        cout<<"The file was not successfully opened"<<endl;
        return 0;
    }
    string keyword[32]={"void","char","int","float","double","short","long","signed","unsigned",
        "struct","union","enum","typedef","sizeof","auto","static","register","extern","const","volatile",
        "return","continue","break","goto","if","else","switch","case","default","for","do","while"};

2. Main function realization

 

string str;
	int total=32,numt=0; ;
	int count[total] = {0}; 
	while (getline (inFile, str)){
		for(int i=0;i<total;i++){
		int pos;
		for(;;){
				if(str.find(keyword[i]) != -1){
					if(str.find("//") != -1) break;
					else{
						pos = str.find(keyword[i]);
						count[i]++;
						str = str.substr(pos + keyword[i].length(),str.length() - pos - keyword[i].length());
					}
				} 
				else break;
			}	
		}
	}
	for(int j=0;j<total;j++){
		if(count[j]!=0){
			numt+=count[j];	
		}
	}
	int num[5]={25,2,6,15,9},x[3]={0,0,-1};
	int struc[count[num[0]]]={0};
	string pick[3]={"else if", "if","else"};
	while (getline (s1, str)){
		int pos;
		for(;;){
			if(str.find(keyword[num[1]]) != -1){
				if(str.find("//") != -1) break;
				else{
					struc[x[0]]++;
					break;
				}
			}
			if(str.find(keyword[num[2]]) != -1){
				if(str.find("//") != -1) break;
				else{
					x[0]++;
					break;
				}
			}
			if(str.find(pick[0]) != -1){
				pos = str.find(pick[0]);
				str = str.substr(pos + pick[0].length(),str.length() - pos - pick[0].length());
			}
			if(str.find(pick[1]) != -1){
				if(str.find("//") != -1) break;
				else{
					x[1]++;
					break;
				}
			}
			else break;
		}	
	}
	int divide[x[1]]={0}, ely=0,eln=0;
	while (getline (s2, str)){
		int pos;
		for(;;){
			if(str.find(pick[0]) != -1){
				if(str.find("//") != -1) break;
				else{
					divide[x[2]]++;
					break;
				} 
			}
			if(str.find(keyword[num[3]]) != -1){
				if(str.find("//") != -1) break;
				else{
					x[2]++;
					break;
				}
			}
			else break;
		}
	}
	for(int m=0;m<x[1];m++){
		if(divide[m]==0){
			eln++;
		}
		else{
			ely++;				
		}	
	}
	for(int o=1; o<con+1; o++){
		switch(o){
		case 1:
			cout<<"total num is: "<<numt<<endl;
			for(int j=0;j<total;j++){	
				if(count[j]!=0){
				cout<<keyword[j]<<" num: "<<count[j]<<endl;
				}
			}
			break;
		case 2:
			if(count[num[0]]!=count[num[2]]){
				cout<<" wrong switch structure"<<endl;
			}
			else{
				cout<<"The number of switch structure is:"<<count[num[0]]<<endl;
				for(int k=0;k<count[num[0]];k++){
				cout<<"switch "<<k+1<<" number of case is: "<<struc[k]<<endl;
			}
			}
			break;
		case 3:
			cout<<"the number of if-else: "<<eln<<endl;
			break;
		case 4:
			cout<<"the number of if-else if-else: "<<ely<<endl;
			break;
		default:
			cout<<"wrong"<<endl;
		}
	}
	inFile.close();
	s1.close();
	s2.close();
    return 0;
}

3.entire code

#include <string>
#include <iostream>
#include <fstream>
using namespace std;
string keyword[32]={"void","char","int","float","double","short","long","signed","unsigned",
        "struct","union","enum","typedef","sizeof","auto","static","register","extern","const","volatile",
        "return","continue","break","goto","if","else","switch","case","default","for","do","while"};
 
int main(){
	int con=0;
	string root; 
	cout<<"the .cpp file name you wanna in the same folder"<<endl;
	cin>>root;
	cout<<"what level of stastic you want(from 1 to 4)"<<endl;
	cin>> con;
	ifstream inFile,s1,s2;
	inFile.open(root.c_str(),ios::in);
	s1.open(root.c_str(),ios::in);
	s2.open(root.c_str(),ios::in);
	if(!inFile|| !s1 ||!s2){
        cout <<"Check the Path of File !" << endl;
        exit(0); 
    }
	string str;
	int total=32,numt=0; ;
	int count[total] = {0}; 
	while (getline (inFile, str)){
		for(int i=0;i<total;i++){
		int pos;
		for(;;){
				if(str.find(keyword[i]) != -1){
					if(str.find("//") != -1) break;
					else{
						pos = str.find(keyword[i]);
						count[i]++;
						str = str.substr(pos + keyword[i].length(),str.length() - pos - keyword[i].length());
					}
				} 
				else break;
			}	
		}
	}
	for(int j=0;j<total;j++){
		if(count[j]!=0){
			numt+=count[j];	
		}
	}
	int num[5]={25,2,6,15,9},x[3]={0,0,-1};
	int struc[count[num[0]]]={0};
	string pick[3]={"else if", "if","else"};
	while (getline (s1, str)){
		int pos;
		for(;;){
			if(str.find(keyword[num[1]]) != -1){
				if(str.find("//") != -1) break;
				else{
					struc[x[0]]++;
					break;
				}
			}
			if(str.find(keyword[num[2]]) != -1){
				if(str.find("//") != -1) break;
				else{
					x[0]++;
					break;
				}
			}
			if(str.find(pick[0]) != -1){
				pos = str.find(pick[0]);
				str = str.substr(pos + pick[0].length(),str.length() - pos - pick[0].length());
			}
			if(str.find(pick[1]) != -1){
				if(str.find("//") != -1) break;
				else{
					x[1]++;
					break;
				}
			}
			else break;
		}	
	}
	int divide[x[1]]={0}, ely=0,eln=0;
	while (getline (s2, str)){
		int pos;
		for(;;){
			if(str.find(pick[0]) != -1){
				if(str.find("//") != -1) break;
				else{
					divide[x[2]]++;
					break;
				} 
			}
			if(str.find(keyword[num[3]]) != -1){
				if(str.find("//") != -1) break;
				else{
					x[2]++;
					break;
				}
			}
			else break;
		}
	}
	for(int m=0;m<x[1];m++){
		if(divide[m]==0){
			eln++;
		}
		else{
			ely++;				
		}	
	}
	for(int o=1; o<con+1; o++){
		switch(o){
		case 1:
			cout<<"total num is: "<<numt<<endl;
			for(int j=0;j<total;j++){	
				if(count[j]!=0){
				cout<<keyword[j]<<" num: "<<count[j]<<endl;
				}
			}
			break;
		case 2:
			if(count[num[0]]!=count[num[2]]){
				cout<<" wrong switch structure"<<endl;
			}
			else{
				cout<<"The number of switch structure is:"<<count[num[0]]<<endl;
				for(int k=0;k<count[num[0]];k++){
				cout<<"switch "<<k+1<<" number of case is: "<<struc[k]<<endl;
			}
			}
			break;
		case 3:
			cout<<"the number of if-else: "<<eln<<endl;
			break;
		case 4:
			cout<<"the number of if-else if-else: "<<ely<<endl;
			break;
		default:
			cout<<"wrong"<<endl;
		}
	}
	inFile.close();
	s1.close();
	s2.close();
    return 0;
}

3.test code

#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(i){  
        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.output

 

6.summary

   Through this experiment, I am more familiar with the writing method of c++ code, and have a deeper understanding of logical statements. In addition, I learned more about how to use Github and CSDN. I realize that planning is very important. At the same time, I realize that I have some shortcomings in programming. I will improve myself in the future.

  

 

 

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

285

社区成员

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

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