285
社区成员
The Link Your Class | |
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/608734907 |
The Aim of This Assignmen | Extract keywords of different levels from the C or C++ code files that are read in. |
MU STU ID and FZU STU ID | 20122217_832001320 |
GitHub Repository: git@github.com:xiwwww/EE308FZ.git
目录
2.Program Requirements
3.Code Description:
Personal Software Process Stages |
Estimated time(min) |
Time(min) |
Planning |
15 |
15 |
Estimate |
20 |
30 |
Analysis |
120 |
150 |
Design Spec |
30 |
45 |
Design Review |
30 |
45 |
Coding Standard |
20 |
30 |
Design |
60 |
90 |
Coding |
400 |
500 |
Test |
30 |
60 |
Reporting |
45 |
60 |
Size Measurement |
30 |
30 |
Postmortem&Process Improvement |
40 |
50 |
total |
810 |
1105 |
1. Basic requirement: output "keyword" statistics
2. Advanced requirement: output the number of "switch case" structures, and output the number of "case" corresponding to each group
3. Uplifting requirement: output the number of "if else" structures
4. Ultimate requirement: output the number of "if, else if, else" structures
for basic level 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using std::string;
void Count_Keywork_Switch_Case(string inputfile,int level) {//TODO(JZ)
std::ifstream inFile(inputfile, std::ios::in); //open file
string x;
string y;
int Keyword_num = 0;
string keyword[32] = {"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"};
for (int i = 0;i < x.length(); i++) {
if (x[i] >= 'A' && x[i] <= 'z')
{
y += x[i];
}
}
for (int a = 0; a < 32; a++) {
if (keyword[a] == y) {
Keyword_num += 1;
}
}
}
Level 2: output the number of "switch case" structures, and output the number of "case" corresponding to each group.
if (y == "switch") {
Count += 1;
Switch_Loop += 1;
}
if (Switch_Loop > 0 && y == "case") {
Case_Number += 1;
}
if (Switch_Loop > 0 && y == "default") {
Case_Num += std::to_string(Case_Number);
Case_Number = 0;
Switch_Loop = 0;
}
for level 3 and 4,output the number of "if else" structures and the number of "if, else if, else" structures. Before completing the more difficult requirements, you need to complete the Lower requirements.
here is brief struc of my code, detail will be show in Github.result:
void Count_If_Elseif(string inputfile, int level) {
while (getline(inFile, x)) {
for (int i = 0; i < x.length(); i++) {
if (x[i] >= 'A' && x[i] <= 'z')
{
if (x[i] == 'e' || x[i] == 'f' || x[i] == 'l' || x[i] == 's' || x[i] == 'i')
{
z += x[i];
}
}
}
if (z.length() >= 2 && z.substr(0, 2) == "if") {
line.push_back(z.substr(0, 2));
}
if (z.length() >= 2 && z.length() <= 6 && z.substr(0, 4) == "else") {
line.push_back(z.substr(0, 4));
}
if (z.length() >= 6 && z.substr(0, 6) == "elseif") {
line.push_back(z.substr(0, 6));
}
z = "";
}
inFile.close();
for (int a = 0; a < line.size(); a++) {//if else elseif
if (line[a] == "if") {
Sec_else += 1;
Sec_elseif += 1;
}
if (line[a] == "else" && Sec_else > 0) {
Count_Ifelse += 1;
Sec_else -= 1;
Sec_elseif -= 1;
}
if (line[a] == "elseif" && Sec_elseif > 0) {
Sec_else -= 1;
Sec_elseif -= 1;
Count_Elseif += 1;
}
}
result:
total num: 35
switch num: 2
case num: 3 2
if-else num: 2
if -else if- else num: 2
I think this assignment has gone through a lot of hardships. At the beginning, I was very confused when I saw the topic and didn't know how to start, so I consulted my classmates and inquired a lot of information. After writing the code, I kept debugging. Through this experiment, I have deepened my understanding of Python. At the same time, I have realized that my code ability is relatively weak and I need to keep practicing. I hope the next experiment will not be so hasty and confused.