285
社区成员




Personal Software Process Stages | Estimated Time/minutes | Completed Time/minutes |
---|---|---|
Analysis and Planing | 20 | 25 |
Program Development | 35 | 35 |
Program Review | 10 | 15 |
Performance Test | 10 | 10 |
Blog Writing | 30 | 35 |
Github Preparation | 15 | 20 |
Total | 120 | 140 |
After downloading the problems, I'm trying to understanding what's the main task. It gives me a finished code and requires me to count keyword and analyse the code's structure. This is a simple but cumbersome programming tasks. It needs patience to deal with all the detail point. The toughest part in this task is to spilt the characters in the paragraph.
getKeywordNum()
cnt ← 0
for i in code
if substr(code, i, length(keyword)) = keyword then
cnt ← cnt + 1
getSwitchNum()
cntSwitch ← 0
cntCase ← 0
for i in code
if substr(code, i, length('switch')) = switch then
getCaseNum
return cntSwitch, cntCase
getIfElseNum()
cntIfElse ← 0
cntIfElseIfElse ← 0
for i in code
if substr(code, i, length('if')) = if then
if else_if next to if then
cntIfElse ← cntIfElse + 1
else then
cntIfElseIfElse ← CntIfElseIfElse + 1
return cntIfElse, cntIfElseIfElse
main()
input code, level
if level >= 1 then getKeywordNum()
if level >= 2 then getSwitchNum()
if level >= 3 then getIfElseNum() and print cntIfElse
if level >= 4 then getIfElseNum and print cntIfElseIfElse
string keyword[] = {"asm", "do","if","return","typedef","autohttps://img-community.csdnimg.cn/images/72dec4246a964445bfd2b3e1b9cefd23.png "#left")
","double","inline","short","typeid","bool","dynamic_cast","int","signed","typename","break","else","long","sizeof","union","case","enum","mutable","static","unsigned","catch","explicit","namespace","static_cast","using","char","export","new","struct","virtual","class","extern","operator","switch","void","const","false","private","template","volatile","const_cast","float","protected","this","wchar_t","continue","for","public","throw","while","default","friend","register","true", "delete","goto","reinterpret_cast","try"};
int cnt = 0;
for (int i = 0; i < 63; i++) {
int len = keyword[i].length();
for (int j = 0; j < input.length(); j++) {
if (input.substr(j, len) == keyword[i]) {
cnt++;
if (input.substr(j, len) == "do") {
if (input.substr(j, 6) == "double") {
cnt--;
}
}
}
}
}
void get_switch(int& st, int id) {
sw.emplace_back(0);
int cnt = 0;
for (int i = st; i < input.length(); i++) {
if (input[i] == '{') {
cnt++;
} else if (input[i] == '}') {
cnt--;
if (!cnt) {
st = i;
return;
}
}
if (input.substr(i, 4) == "case") {
sw[id]++;
}
}
}
void get_if(int& st) {
bool is_else_if = 0;
bool flag = 0;
int cnt = 0;
for (int i = st + 2; i < input.length(); i++) {
if (flag == 1) {
if (input[i] == '{') {
cnt++;
}
if (input[i] == '}') {
cnt--;
if (!cnt) {
st = i + 1;
return;
}
}
}
if (input.substr(i, 2) == "if") {
get_if(i);
}
if (input.substr(i, 7) == "else if") {
if (!is_else_if) {
is_else_if = 1;
cnt_else_if++;
}
i += 7;
} else if (input.substr(i, 4) == "else") {
if (!is_else_if) {
cnt_else++;
}
flag = 1;
i += 4;
}
}
}
We can take advantage of clock() function to analyze the running time.
We can just add 3 lines in the code.
int main() {
string path;
cout << "Please input the file's path: ";
cin >> path;
cout << "Please input completion level: ";
int level;
cin >> level;
double st = clock();
...
double end = clock();
cout << "time cost: " << end - st << endl;
return 0;
}
In this lab, I've learnt how to commit files and push that to GitHub.
Here is my Github link:https://github.com/DoubleQ666/EE308_Lab1