Cpp Design:1. Extract the keywords ifandelseand consider else-if as one keyword
2. At the same time, also extract the "level" of each of them like this:3. Then, we can process the if-elseandif-elseif-else with all the levels
4. Traverse the same level at a time.
voidread(string &path,int&level){
cout <<"Please type in the path if code file: ";puts("");
cout <<"The path should be written like: /Users/lancecai/Documents/GitHub/EE308FZ/Record-for-LAB/Lab1/ctest.txt"<< endl;
cin >> path;
cout <<"Please select the level of completion (1 to 4): ";
cin >> level;if(level <=0|| level >5){throw"Unexpected level number!";}}voidreadFile(string &path, vector<string>&code){
ifstream file(path);
string temp;while(file >> temp){
code.push_back(temp);}// for (int i = 0; i < code.size(); i++)// {// cout << code[i] << endl;// }//cout << code << endl;}boolisNotLetter(char x){return!((x >=65)&&(x <=90)||(x >=97)&&(x <=122)||(x =='{')||(x =='}'));}voidpreprocess(vector<string>&code){for(int i =0; i < code.size(); i++){
string temp = code[i];
temp.erase(std::remove_if(temp.begin(), temp.end(), isNotLetter),
temp.end());
code[i]= temp;}}
3. count keywords
const set<string> keywords ={"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"};/*
Return the total num of keywords in a code file
the path directed to.
*/intcountKeywords(vector<string>&code){int total_num =0;for(int i =0; i < code.size(); i++){if(keywords.find(code[i])!= keywords.end()){
total_num++;}}return total_num;}
4. count switch-case
/*
Print the number of "switch case" structures, and output
the number of "case" corresponding to each group
*/voidcountSwitchAndCase(vector<string>&code){int switch_num =0;int case_temp_num =0;
vector<int> case_num;for(int i =0; i < code.size(); i++){if(code[i]=="switch"){
switch_num++;
case_temp_num =0;for(int j = i +1; j < code.size(); j++){if(code[j]=="switch"){break;}else{if(code[j]=="case"){
case_temp_num++;}}}
case_num.push_back(case_temp_num);}}
cout <<"switch num: "<< switch_num << endl;
cout <<"case num: ";for(int i =0; i < case_num.size(); i++){
cout << case_num[i]<<" ";}puts("");}
Please type in the path if code file:
The path should be written like:/Users/lancecai/Desktop/Lab-2022W/Lab-EE304-Software/Record/Lab2/ctest.txt
/Users/lancecai/Desktop/Lab-2022W/Lab-EE304-Software/Record/Lab2/ctest.txt
Please select the level of completion(1 to 4):5
Case 1: Count keywords
total num:35
Case 2switch num:2case num:32
Case 3if-else num:2
Case 4if-elseif-else num:2
On my machine (Apple M1-chip 8GB), the time consumption is around: Time = 0.000081 seconds. And the Time complexity is no more than
O
(
n
2
)
O(n^2)
O(n2), which is acceptable.
What did I learn in this Lab1-2
Revise every thing of C++
Learn how to use Git
Learn how to plan and design the program before typing in mess.