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/600798588 |
The Aim of This Assignment | Learn how to use git and github & Keywords Capturing |
MU STU ID and FZU STU ID | 19104847 & 831902119 |
My Github address: https://github.com/AllenABCDE/EE308/tree/master/LAB2
Personal Software Process Stages | Estimated Time(min) | Completed Time(min) |
---|---|---|
Planning | - | - |
Estimate | 30 | 20 |
Development | - | - |
Analysis | 90 | 120 |
Design Spec | 20 | 30 |
Design Review | 10 | 10 |
Coding Standard | 30 | 40 |
Design | 30 | 60 |
Coding | 300 | 540 |
Code Review | 60 | 90 |
Test | 60 | 60 |
Reporting | 120 | 90 |
Test Report | 60 | 90 |
Size Measurement | 20 | 30 |
Postmortem&Process Improvement | 30 | 20 |
total | 860 | 1200 |
Before completing the more difficult requirements, you need to complete the Lower requirements.
*File reading
The entire file needs to be read first. Use getLine() to read the contents of the file line by line and store them in a vector.
void input (string path) {
ifstream fp;
string text;
fp.open(path,ios::in);
if (!fp.is_open()) {
cout<<"Error!"<<endl;
return;
}
while (getline(fp, text)) {
content.push_back(text);
}
fp.close();
Handling of comments and strings
To prevent the possible keywords in the comment statement and string from interfering with the extraction results, it is necessary to delete the comment statement and string. Here I learned and used the find() function and the copy substr() function.
void handle () {
string text,line;
size_t j=0,k=0,t=0;
int i=0,f=0,r=0;
for (i=0; i<content.size(); i++) {
line = content[i];
if (line.find("\t") != line.npos && line.find("\t") == 0) {
do {
k = line.find("\t")+1;
line = line.substr(k,line.length()-k);
} while (line.find("\t") != line.npos && line.find("\t") == 0);
content[i] = line;
}
if (line.find("//") != line.npos) {
j = line.find("//");
if (j==0) {
content.erase(content.begin()+i);
i--;
} else {
text = line.substr(0,j);
content[i] = text;
}
} else if (line.find("\"") != line.npos) {
size_t p[50] = {0};
k = 0;
f = 0;
while ((t=line.find("\"",k)) != line.npos) {
p[f] = t;
f++;
k=t+1;
}
p[f] = line.length();
text = line.substr(0,p[0]);
for (r=1; p[r]!=0; r+=2) {
text += line.substr(p[r]+1,p[r+1]-p[r]-1);
}
content[i] = text;
} else if (line.find("/*") != line.npos) {
j = line.find("/*");
k = line.find("*/");
if (k != line.npos) {
if (j == 0) {
content[i] = line.substr(k+2,line.length());
} else {
content[i] = line.substr(0,j);
}
i--;
} else {
content[i] = line.substr(0,j);
i += 1;
line = content[i];
while (line.find("*/") == line.npos) {
content.erase(content.begin()+i);
line = content[i];
}
k = line.find("*/")+2;
content[i] = line.substr(k,line.length()-k);
}
}
}
}
Keyword statistics*
First, build a key word group to store the keywords. You then use the find() function to find and count keywords line by line.
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","static","struct","switch","typedef","union","unsigned","void","volatile","while"};
void Keyword () {
int num = 0;
string line;
for (int i = 0; i<content.size(); i++) {
line = content[i];
for (int j=0; j<32; j++) {
size_t fi = line.find(keyword[j], 0);
while (fi!=line.npos && judge(line,keyword[j])) {
num++;
fi = line.find(keyword[j], fi + 1);
}
}
}
cout<<"total num: "<<num<<endl;
The group number is determined by looking for the number of switches, and the number of cases before the next switch appears is counted as the number of cases in the group.
void SwitchCase () {
int snum = 0,f = -1,cnum[1000]={0};
string line;
for (int i = 0; i<content.size(); i++) {
line = content[i];
if (line.find("switch") != line.npos && judge(line, "switch")) {
snum += 1;
f += 1;
}
if (line.find("case") != line.npos && judge(line, "case")) {
cnum[f] += 1;
}
}
cout<<"switch num: "<<snum<<endl;
cout<<"case num:";
for (int i = 0; i<=f; i++) {
cout<<" "<<cnum[i];
}
cout<<endl;
Find if else and if else if else
Use stacks for matching. 1 is pushed when if is encountered, and 2 when if_else is encountered. When else is encountered, the stack is unmatched with the if else structure if the top of the stack is 1, and the if else_if else structure if the top of the stack is 2. However, the current method will be matched incorrectly in some special cases, such as if else_if structure nested in the if part of the if else structure, will be matched incorrectly into an if else_if else structure. For the time being, we have not come up with a new solution.
void IfElse (int level) {
int sum1 = 0,sum2 = 0;
stack<int> s;
string line;
for (int i = 0; i<content.size(); i++) {
line = content[i];
if (line.find("if") != line.npos && line.find("else") == line.npos && judge(line, "if")) {
s.push(1);
} else if (line.find("if") == line.npos && line.find("else") != line.npos && s.empty() == false) {
if (s.top() == 1) {
sum1++;
} else {
sum2++;
}
s.pop();
}else if (line.find("if") != line.npos && line.find("else") != line.npos && judge(line, "if")) {
s.push(2);
}
}
if (level == 3) {
cout<<"if-else num: "<<sum1<<endl;
} else if (level == 4) {
cout<<"if-else num: "<<sum1<<endl;
cout<<"if-else-if num: "<<sum2<<endl;
}
The requirements of this assignment seemed very logical at the beginning and should be changed quickly. But when it comes to actually writing code to implement the requirements, it turns out to be quite complicated, with lots of details to pay attention to. There's always going to be all kinds of bugs when you write a program and you think you're done and you run it. Only by improving the code again and again can the requirements be successfully implemented. Through this assignment, I touched and learned Github usage, code specification, performance testing and other things I had never touched before. I felt that I gained a lot. In general, I still need to learn and practice more, and practice makes perfect.