285
社区成员




The Link Your Class | My class link |
---|---|
The Link of Requirement of This Assignment | LAB 1-2 |
The Aim of This Assignment | Keywords extraction and counting |
MU STU ID and FZU STU ID | 20122187 832001215 |
Github :link to Github
目录
2.Description of problem-solving ideas
3. Design and implementation process
PSP | ESTIMATE TIME(MINUTE) | ESTIMATE TIME(MINUTE) |
---|---|---|
Planning | 30 | 45 |
Estimate | 80 | 60 |
Development | 40 | 30 |
Analysis | 15 | 20 |
Design Spec | 10 | 10 |
Design Review | 10 | 5 |
Coding Standard | 10 | 10 |
Design | 60 | 30 |
Coding | 120 | 150 |
Code Review | 5 | 10 |
Test | 60 | 60 |
Reporting | 20 | 20 |
Test Report | 10 | 20 |
Size Measurement | 5 | 5 |
Postmortem & Process Improvement Plan | 5 | 5 |
Total | 480 | 490 |
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.
(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;
}
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.