285
社区成员




The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZU202201 |
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/608745426 |
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 | FZU ID : 832001302 MU ID : 20123108 |
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
PSP | ESTIMATE TIME(MINUTE) | ESTIMATE TIME(MINUTE) |
Planning | 45 | 30 |
Estimate | 5 | 5 |
Development | 30 | 50 |
Analysis | 60 | 70 |
Design Spec | 10 | 10 |
Design Review | 0 | 0 |
Coding Standard | 20 | 15 |
Design | 40 | 60 |
Coding | 240 | 300 |
Code Review | 10 | 15 |
Test | 60 | 80 |
Reporting | \ | \ |
Test Report | 10 | 20 |
Size Measurement | 10 | 10 |
Postmortem & Process Improvement Plan | 20 | 50 |
Total | 560 | 715 |
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 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.
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;
}
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.