183
社区成员




The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZ?category=0 |
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/600870907 |
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 | 19104529-831901217 |
[GitHub Websites](https://github.com/wwwppxsw/EE308-Lab2)
[Code specification](https://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/contents/)
Contents
2.Ideas of solving the problem
3. Design and implementation process
Uplifting requirement & Ultimate requirement:
4.Unit test screenshots and description:
5.Unit test coverage optimization and performance testing:
Personal Software Process Stage | Estimated time (min) | Actual time(min) |
Planning | 20 | 20 |
Estimate | - | - |
Development | - | - |
Analysis | 120 | 180 |
Design Spec | 10 | 20 |
Design Review | 10 | 10 |
Coding Standard | 30 | 40 |
Design | 40 | 65 |
Coding | 240 | 300 |
Code Review | 30 | 60 |
Test | 60 | 120 |
Reporting | - | - |
Test Report | 60 | 120 |
Size Measurement | - | - |
Postmortem & Process Improvement | 20 | 20 |
Total | 640 | 955 |
In order to meet the need of the above process, C++ is chosen as the programming language to search and solve due to its compatibility, efficiency and also I am most familiar with.
According to the test requirements of the course,we need to count the occurrence frequency of Specific Words and Program Structure, which I thought the whole process is to scan the total file target, then count the eligible phrases and finally output the total result. So I review the C++ textbook about the i-o stream and file; search the GitHub, cnblogs, CSDN 's blog. Then I saw a blog which is about using the substr and find function which are my key steps in processing information.
The substr gives the program the selected part of the string line, and the find function in order to jump the pointer to the first letter of the eligible word. With these two function mature and encapsulated
c++
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
string keyword[]={"auto","break","case","char","const","continue","default","do",
"double","else","enum","extern","float","for","goto","if",
"int","long","register","return","short","signed","sizeof","stastic",
"struct","switch","typedef","union","unsigned","void","volatile","while"};
int main(){
int con=0;//输入level
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;
}
The program obtains the file stream of the .cpp file through the command line parameters and sends it to the corresponding string variable, which is convenient for the function to read and call the file. When calling function statistics, directly compare the file stream with string array keyword, record the number of words in count, and record the total number of words in int variable numt.
c++
while (getline (inFile, str)){//一行一行读取
for(int i=0;i<total;i++){
int pos;//单词的位置
for(;;){
if(str.find(keyword[i]) != -1){//返回-1表示句子里没有该单词
pos = str.find(keyword[i]);//找到首次出现单词位置
count[i]++;
str = str.substr(pos + keyword[i].length(),str.length() - pos - keyword[i].length());//截取子串存入substr
}
else break;
}
}
}
Based on the string headfile, the pricinple of the counting loop is shown below:
In order to express this process in a more straightforward way. While the file stream isn't reaching the bottum,get the whole line of the .cpp file, then using the find() function to search the word that is qualified; delete the word in the sentence by substr() until there is nothing satisfied the keyword array; finally jump to the next line. This occurs serval time till file ends.
This kind of Screening Logic is the universal template of total program. From the Advanced requirement to the Ultimate requirement. Are all the variant of template.
int num[5]={25,2,6,15,9},x[3]={0,0,-1};// num:在keyword数组中的单词代码;x:由需处理int型数据组成的数组
int struc[count[num[0]]]={0};//2.数case数; 将每个switch的case数存入对应数组元素
string pick[3]={"else if", "if","else"};//要求3、4的判断依据
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]++;//确认总共有几个有效if
break;
}
x[1]++;
break;
}
else break;
}
}
the pricinple of the counting loop is shown below:
By the fact that in every Switch Structure includes case and default, so I use "default" as the conditional statement to terminate case counting.
string pick[3]={"else if", "if","else"};//要求3、4的判断依据
while (getline (s1, str)){//一行一行读取
int pos;//单词的位置
for(;;){
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]++;//确认总共有几个有效if
break;
}
}
else break;
}
}
int divide[x[1]]={0}, ely=0,eln=0;//3.4:将每个if-else的else if数存入对应数组元素,0就只是if-else
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++;
}
}
the pricinple of the counting loop is shown below:
In this system, all of the decision structures are been recognize as the "if-else if-else" structure, which I think the "if-else structure is a more special type where else-if equal to zero." Also, in this experiment, we only consider whether or not it is a if-else structure. So an array is used to collect data. 0 means if else, none 0 mean else if structure. As for the special case, one is the the else if without the if below, it will be cut as the file stream ends; two is the first written if, to solve this I set the initial number is -1. Using these operations, the program functions surprisingly well.
where sample.cpp is:
#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;
}
Maybe there are some errors in my C++ programer for the blank below🤔:
The total experiment is quite chanllenging, but I learn a lot from it. Although the program manage to meet the requirement of the lab, but there still some of short backs like if a variable named aifb, it will be read and counted in the program. So there are still some space for improvement and development. Hope it won't scared me after 3 month's time.