285
社区成员




The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZU202201 |
---|---|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/608734907 |
The Aim of This Assignment | Extract keywords of different levels from the C or C++ code files that are read in |
MU STU ID and FZU STU ID | 20122586_832001315 |
🛑 Link to my Github code website
Personal Software Process Stages | Estimated Time Consumption (mins) | Completed Time(mins) |
---|---|---|
Planning | -- | -- |
Estimate | 20 | 15 |
Development | -- | -- |
Analysis | 90 | 100 |
Design Specification | 50 | 50 |
Design Review | 25 | 20 |
Coding Standard | 20 | 25 |
Design | 80 | 90 |
Coding | 1200 | 1250 |
Code Review | 40 | 45 |
Test | 100 | 90 |
Reporting | -- | -- |
Test Report | 90 | 80 |
Size Measurement | 20 | 20 |
Postmortem & Process Improvement Plan | 60 | 55 |
Total | 1795 | 1840 |
💻 Programming Language :C++, (Java, Python)
Although I can choose from C++, Java, Python three programming languages, I finally choose C++. C++ has the advantages of rich function, strong expression ability, flexibility and convenience, and wide application 👍. I systematically learned object-oriented programming based on C++, so I used C++ for this experiment.
🚀 Design four levels to realize the requirement function.
🎲 Create the main function to read the path of a specific C file and the desired level to extract keywords.
💎 We should first know the 32 keywords.
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<sstream>
#include<stack>
using namespace std;
string keywords[32]={"auto","double","int","struct","break","else","long","switch",
"case","enum","register","typedef","char","extern","return","union",
"const","float","short","unsigned","continue","for","signed","void",
"default","goto","sizeof","volatile","do","if","while","static"
};
int peak =-1;
int container[600]={0};
int Total_Num=0;
int Switch_Num=0;
int Case_Num=0;
int if_else_Num=0;
int if_elseif_else_Num=0;
vector<int> Case; //Creates a one-dimensional vector with no specified length
②The main function 🪁:
int main()
{
string C_file;
string interim;
int Level_Num;
cout << "Print the path of C/C++ file: " << endl;
cin >> C_file;
cout << "Print the level of extract keywords: " << endl;
cout << "Level1: total keywords" << endl;
cout << "Level2: switch case and case" << endl;
cout << "Level3: if else" << endl;
cout << "Level4: if, else if, else" << endl;
cin >> Level_Num;
ifstream readfile(C_file.c_str()); //read in the C/C++ file
void First_Level(string cha);
void Second_Level(string cha);
int assess_fun1(string A, string B);
int assess_fun2(char cha);
while(getline(readfile,interim)){
istringstream strl (interim);
string z;
if(Level_Num >=3){
Second_Level(interim);
}
while (strl >>z){
First_Level(z);
}
}
if(Level_Num >=1){
cout << "total num: " << Total_Num << endl;
}
if(Level_Num >=2){
cout << "switch num: " << Switch_Num << endl;
if(Case.empty()){
Case.push_back(0);
}
else{
Case.push_back(Case_Num);
}
cout << "case num: ";
for(int x =1; x<=Switch_Num; x++){
cout << Case[x] << " ";
}
cout << endl;
}
if(Level_Num >=3){
cout << "if-else num: " << if_else_Num << endl;
}
if(Level_Num >=4){
cout << "if-elseif-else num: " << if_elseif_else_Num << endl;
}
}
③The function to find the number of keywords 🔍:
int assess_fun1(string A, string B){
int pos = A.find(B,0);
int Len = B.length();
int assess_fun2(char cha);
if(pos != string::npos){
if(pos == 0){
if(assess_fun2(A[pos+Len]) == 0 && assess_fun2(A[pos-1]) == 0){
return 1;
}
else{
return 0;
}
}
else{
if(assess_fun2(A[pos+Len]) == 0){
return 1;
}
else{
return 0;
}
}
}
return 0;
}
int assess_fun2(char cha){
if((cha >='A' && cha <='Z') || (cha >='a' && cha <='z')){
return 1;
}
else{
return 0;
}
}
④The function to calculate the number of keywords depend on different level 💡:
void First_Level(string cha){ //stand for the first two level 1 and 2;
for (int i =0; i<32; i++){
if(assess_fun1(cha,keywords[i])==1){
if(assess_fun1(cha,"switch")){
Case.push_back(Case_Num);
Switch_Num++;
Case_Num = 0;
}
if(assess_fun1(cha,"case")){
Case_Num++;
}
Total_Num++;
break;
}
}
}
void Second_Level(string cha){ //stand for the second two level 3 and 4;
if(assess_fun1(cha,"else if")){
peak++;
container[peak] =2;
}
else{
if(assess_fun1(cha,"else")){
if(container[peak]==1){
if_else_Num++;
peak--;
}
else{
if(container[peak]==2){
if_elseif_else_Num++;
peak--;
}
}
}
else{
if(assess_fun1(cha,"if")){
peak++;
container[peak]=1;
}
}
}
}
🤩The test C file:
#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;
}
Level1 👓 :
Level2 🌈 :
Level3 📸 :
Level4 👀 :
This assignment made me more familiar with C++ and made me realize that I must pay attention to time management in the process of software development. This assignment also made me more aware of my lack of ability, so I need to strengthen my programming skills before the big task 🙂.