The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZ |
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/600798588 |
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 | 19104669_831901311 |
Here is the GitHub repository link for this experiment:
https://github.com/hs1215/hs1215/tree/main |
I.PSP Form
PSP | Estimated time(min) | Time(min) |
---|---|---|
Planning | ||
Estimate | 30 | 30 |
Development | ||
Analysis | 45 | 60 |
Design Spec | 75 | 90 |
Design Review | 10 | 10 |
Coding Standard | 60 | 60 |
Design | 60 | 60 |
Coding | 1000 | 1200 |
Code Review | 30 | 60 |
Test | 30 | 60 |
Reporting | ||
Test Report | 30 | 30 |
Size Measurement | 20 | 30 |
Postmortem&Process Improvement | 60 | 60 |
total | 1450 | 1750 |
II.Description of Problem-Solving Ideas
Program Requirements:
1..Read in a c/c++ code file.
2. Basic requirement: output "keyword" statistics
3. Advanced requirement: output the number of "switch case" structures, and output the number of "case" corresponding to each group
4. Uplifting requirement: output the number of "if else" structures
5. Ultimate requirement: output the number of "if, else if, else" structures
Use the fopen function to open a file and read its contentsCombine letters in the file into words, check whether they are keywords, and count the number of keywordsNumber of switch case structures. The number of switches and cases is traversed using switches as the markIf-else and if-elseif-else structures are counted using a stack, referring to the parenthesis matching we did earlier.Count a string, iterate over the string "if" or "elseif" and push it onto the stack. If it's "else" and the stack is not empty, (1) the top element of the stack is "if",Calculate an if-else structure and delete this element;(2) If the top element of the stack is "elseif", delete the top element of the stack until the top element of the stack is "if", calculate an if-elseif-else structure.
III.Design and Implementation Process
1. Read files
fp = fopen(file_path,"r");
while((c = fgetc(fp))!=EOF){
2. Total number of keywords and number of Switch cases
fp = fopen(file_path,"r");
while((c = fgetc(fp))!=EOF){
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
str+=c;
}
else{
if(str!=""){
for(i=0;i<35;i++){
if(str==a[i]){
cnt++;
if(i==25){
cnt1++;
if(s.empty()){
s.push('s');
}
}
if(i==2&&s.top()=='{'){
cnt2++;
}
}
}
str="";
}
else{
if((c=='{')&&(!s.empty())){
s.push(c);
}
if((c=='}')&&(!s.empty())){
s.pop();
s.pop();
ss[j++]=cnt2;
cnt2=0;
}
}
}
}
cout<<"total num: "<<cnt<<endl;
cout<<"switch num: "<<cnt1<<endl;
cout<<"case num: ";
for(i=0;i<j;i++){
cout<<ss[i]<<" ";
}
cout<<endl;
fclose(fp);
3. The number of if else and if elseif else constructs
fp = fopen("data.txt","r");
while((c = fgetc(fp))!=EOF){
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
str+=c;
}
else{
if(str!=""){
if(str=="else"&&c==' '){
continue;
}
else{
ans[l++]=str;
str="";
}
}
}
}
int g=0,b=0;
stack<string> m;
for(i=0;i<l;i++){
if(ans[i]=="if"||ans[i]=="elseif"){
m.push(ans[i]);
}
if(ans[i]=="else"&&!m.empty()){
if(m.top()=="if"){
g++;
m.pop();
}
if(m.top()=="elseif"){
while(m.top()=="if"){
m.pop();
}
b++;
m.pop();
}
}
}
cout<<"if-else num: "<<g<<endl;
cout<<"if-elseif-else num: "<<b<<endl;
fclose(fp);
Test
Output
Summary
Way to complete this assignment is a little difficult, their thinking is a bit better, I haven't got the code, a code card, in the process of solving the a point, and stuck behind, the code is the most uncomfortable part of into dead circulation, see code feel have no problem, but that's not a test, spend a lot of time looking at the wrong is infinite loop problem is discovered,I'm glad we took care of the rest.
The biggest gain of this assignment is that I have learned some new knowledge, such as how to use some new functions and how to use Github and Git.
Secondly, this assignment also made me realize my shortcomings, such as algorithm problems. I feel that the code can be optimized but the ability is limited, so I still need to work hard.