The Link Your Class | |
The Link of Requirement of This Assignment | |
The Aim of This Assignment | ① Learn git and Github; Create a Github repository. ③ Code writing; ④ Update and optimize the code. |
MU STU ID and FZU STU ID | eg. 20122543_832001317 |
目录
Design and implementation process
PSP form for the work
Personal Software Process Stages | Estimated Time/minutes | Completed Time/minutes |
PlanningPlanning | 30 | 45 |
Estimate | 15 | 15 |
Development | - | - |
Analysis | 20 | 30 |
Design Spec | 40 | 40 |
Design Review | 30 | 40 |
Coding Standard | 20 | 25 |
Design | 40 | 60 |
Coding | 100 | 120 |
Code Review | 50 | 50 |
Test | 40 | 35 |
Test Report | 80 | 110 |
Size Measurement | 20 | 10 |
Postmortem & Process Improvement Plan | 50 | 45 |
Total | 535 | 625 |
To see more detailed code go to:https://github.com/Mrhc159/EE308lab1.git
The preparatory work
I chose C++ to complete this project.
Thinking about problems and solutions
Task 1:Finds output "keyword" statistics, finds the keyword and counts it.
Solution:First create the text and enter the keywords; Then read the text, delete the text symbols and Spaces, and count the number of keyword occurrences.
Task 2:Finds the number of "switch case" structures, and the number of "case" corresponding to each group.
Solution:The input file is traversed, and the if-else is used to count the total number of switches and corresponding cases.
Task 3:Count the number of "if else" structures and "if, else if, else" structures.
Solution:To use the stack method, use the find function to find the "else if" and put it on stack 2; if "if" is found, it is placed on stack 1. If there is an "else", take if_else_num++ from stack 1. The same goes for "if-else-if". However, due to the lack of in-depth learning and understanding of this method, I have limited ability to write code by myself. Check the following information:
Design and implementation process
Task 1: Task 2:
Task 3:
Code Description
Task 1:
(1)Open the target file
int main(){
ifstream infile;
infile.open("lab2.txt");
(2)Builds strings and keywords
int total=0;
int switch=0;
int case=0;
string keyword[32]={"auto","break","case","char","const","continue","default","double","do","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"};
string keyword1[]={"switch"};
string keyword2[]={"case"};
string keyword3[]={"if"};
string keyword4[]={"else"};
(3)Delete Spaces and characters
while(getline(inFile,str)){
str1=replace_all(str, "{", " ");
str1=replace_all(str, "}", " ");
str1=replace_all(str, "(", " ");
str1=replace_all(str, ")", " ");
str1=replace_all(str, ";", " ");
str1=replace_all(str, ":", " ");
str1=trim(str);
stringstream input(str1);
(4)Count the number of keywords by traversing the text
for(int j=0;j<100;j++){
if(d==keyword[j]){
tot+=1;
Task 2:
The length of the string is obtained, and then the string is traversed. When any letter in "switch" appears, the number is increased by one, while skipping four characters to continue traversing the string. "Case" in the same way.
l=str.length();//获取字符串长度
for(int m=0;m<l;m++){
if(str[m]=='s'&&str[m+1]=='w'&&str[m+2]=='i'&&str[m+3]=='c'&&str[m+4]=='h')
{
switch+=1;
m+4;
}
}
for(int n=0;n<l;n++){
if(str[n]=='c'&&str[n+1]=='a'&&str[n+2]=='s'&&str[n+3]=='e')
{
case+=1;
n+3;
}
}
Task 3:
Count "if else","if,elseif, else" in a string using the stack method.
if(str.find("else if")!=string::npos){//string::npos表示直到字符串结束
istack.push(2);//此时将"else if"压入堆栈2的栈顶
}else if(str.find("if")!=string::npos){
istack.push(1);//此时将"if"压入堆栈1的栈顶
}else if(str.find("else")!=string::npos){
if(istack.top() == 1){//".top()"返回栈顶元素(从堆栈1中取出)
istack.pop();//".pop()"删除栈顶元素
if_else_num++;
continue;
}
while(istack.top() == 2) {
istack.pop();
}
istack.pop();
if_elseif_if_num++;
}
Unit test
Summary
Through this experiment, I not only became more proficient in the use of github, but also strengthened my ability to write code. In the process of solving the problem, difficulties are inevitable, in the inquiry of a lot of relevant information, combined with the actual operation, many problems will be readily solved. I also learned a lot of new knowledge in it. I hope I can continue to work hard and persist in the future.