183
社区成员
发帖
与我相关
我的任务
分享| The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZ |
|---|---|
| The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/600798588 |
| The Aim of This Assignment | Extract keywords of different levels from the C or C++ code files that are read in. |
| MU STU ID and FZU STU ID | 19103557&831902230 |
Here is the GitHub repository link for this experiment:
| https://github.com/ArrXiao/EE308_LAB2 |
|---|
Coding Standard: https://cs50.readthedocs.io/style/c/
| Personal Software Process Stage | Estimated Time (minutes) | Actual Time (minutes) |
|---|---|---|
| Planning | ||
| · Estimate | 40 | 30 |
| Development | ||
| · Analysis | 40 | 50 |
| · Design Spec | 30 | 20 |
| · Design Review | 15 | 15 |
| · Coding Standard | 30 | 30 |
| · Design | 60 | 60 |
| · Coding | 1500 | 1200 |
| · Code Review | 60 | 120 |
| · Test | 30 | 30 |
| Reporting | ||
| · Test Report | 60 | 60 |
| · Size Measurement | 30 | 30 |
| · Postmortem&Process Improvement | 30 | 30 |
| Total | 1925 | 1675 |
1.When I first saw the title, I thought it was just like looking up a few strings, and then I found a lot of restrictions, such as the judgment of code like quotation marks, comments, etc.
2.Then I tried to find a lot of information about keywords on the Internet, and cleared up a lot of logical judgments.In fact, I have not written the C language for a long time, so many of the function methods are not well remembered. I checked a lot of small details and tested them, which wasted a lot of time.
3.Then I started to write code. I used C language. Although many students say Python is simple, I think as long as the logic is similar, I know how to use it and what programming language I choose can solve this problem.
4.The second step was clear, but the last two judgments about if-else blocks stuck me.The first thing I think about is a set of criteria to distinguish each case, and what changes are needed after each case, but the later I find that logic is one layer at a time, which is too complex, I give up this method and start looking for data.
5.Finally, I found the stack method, which is clever and easy to understand.


1.To determine the number of key words, I considered two interference situations: quotation marks and comments. Although there are two, there are actually many branches. First, judge whether it is lowercase letters or not, judge whether it is quotation marks, comments and other situations, and exclude the code in the situation respectively, then judge whether the compared string is less than 8 bits, and finally compare the string less than 8 bits with the keyword to draw a conclusion.

enum MY_STATE{
INVAILD,
IN_QUOTE,
IN_QUOTE_TRANS,
IN_WORD,
NOT_KEY,
COMMENT_1,
COMMENT_LINE,
COMMENT_BLOCK,
COMMENT_BLOCK_1
};
if(IsKeyLetter(input[i])){
state=IN_WORD;
i++;
}
else if(IsOtherLetter(input[i]))
state=NOT_KEY;
else if(input[i]=='\"')
state=IN_QUOTE;
else if(input[i]=='/')
state=COMMENT_1;
break;
case IN_QUOTE:
if(input[i]=='\\')
state=IN_QUOTE_TRANS;
else if(input[i]=='\n')
state=INVAILD;
break;
case IN_QUOTE_TRANS:
if(input[i]=='\n')
state=INVAILD;
else
state=IN_QUOTE;
break;
case IN_WORD:
if(IsKeyLetter(input[i])){
if(i<Len_Max)
i++;
else{
i=0;
state=NOT_KEY;
}
}
else if(IsOtherLetter(input[i])){
i=0;
state=NOT_KEY;
}
else{
input[i]='\0';
CheckKeyWord();
i=0;
if(input[i]=='\"')
state=IN_QUOTE;
else if(input[i]=='/')
state=COMMENT_1;
else
state=INVAILD;
}
break;
case NOT_KEY:
if(input[i]=='\"')
state=IN_QUOTE;
else if(input[i]=='/')
state=COMMENT_1;
else if(!IsKeyLetter(input[i]) && !IsOtherLetter(input[i]))
state=INVAILD;
break;
case COMMENT_1:
if(input[i]=='/')
state=COMMENT_LINE;
else if(input[i]=='*')
state=COMMENT_BLOCK;
else if(IsKeyLetter(input[i])){
state=IN_WORD;
i++;
}
else if(IsOtherLetter(input[i]))
state=NOT_KEY;
else
state=INVAILD;
break;
case COMMENT_LINE:
if(input[i]=='\n')
state=INVAILD;
break;
case COMMENT_BLOCK:
if(input[i]=='*')
state=COMMENT_BLOCK_1;
break;
case COMMENT_BLOCK_1:
if(input[i]=='/')
state=INVAILD;
else
state=COMMENT_BLOCK;
break;
2.Judging the "swich case" is relatively simple. After finding a "swich", judge the variable storing the "swich". If the variable is not 0, add 1, and move the counting position of the array storing the "case" go to next. If it is 0, add 1, and add 1 when you see the case.

if(result==0&&i==25&count[i]!=0&&level>=2)case_num++;//level_2判断为第几个switch
if(result==0&&i==2&&level>=2)Case[case_num]++;//level_2判断在一个swich下case个数
3.Judge the combination of "if" and "else". I use the stack method. First, judge whether a row is "if", "else" or "else if". When it is "if", press 1 on the stack, when it is "else if", press 3 on the stack, and when it is "else", pop up the stack.then stop when it pop-up 1 , if there are 3 pop-up stacks in the middle, it is "if-elseif-else" combination, otherwise it is "if-else" combination.

void stackfunc(stack *p){//判断是否弹栈内容包括else if
int comp=0;
if(if_else_num[0]==1&&if_else_num[1]==0){//如果是if则压栈1
if_else_num[0]=0;
StackInput(p,1);
}
if(if_else_num[0]==1&&if_else_num[1]==1){//如果是else if则压栈3
if_else_num[0]=0;
if_else_num[1]=0;
StackInput(p,3);
}
if(if_else_num[0]==0&&if_else_num[1]==1){//如果是else,则弹栈直到弹出1
while (p->data[p->top-1]!=1){
if (p->data[p->top-1]==3){
comp=1;
StackOutput(p);
}
}
1.First, I used the official test code to test
#include <stdio.h>
int main(){
int i=1;
double j=0;
long f;
switch(i){
case 0:
break;
case 1:
break;
case 2:
break;
default:
break;
}
switch(i){
case 0:
break;
case 1:
break;
default:
break;
}
if(i<0){
if(i<-1){}
else{}
}
else if(i>0){
if (i>2){}
else if (i==2) {}
else if (i>1) {}
else {}
}
else{
if(j!=0){}
else{}
}
return 0;
}
The result is correct

2.Then I used a code to judge the if else combination. I omitted many braces and nested many layers, but the result was still OK.
#include <stdio.h>
int main(){
int i=-1;
if(i<0){
if(i<-1) i++;
else i++;
}else if(i>0){
if (i>2){}
else if (i==2)
if(i!=0) i++;
else if(i>2)
if(i>5) i++;
else i++;
else i++;
}
return 0;
}

When I saw this, I didn't know what it was, so I looked up a lot of information. After that, I seemed to understand that this is a test of measuring the quality of code.So I was full of joy to find what tools can make C language use, but the result was disappointing.I saw what others said"for agile development, unit testing is essential. For java development, JUnit is very good. For C + + development, CPPUnit can also be used. For traditional C language development, there is no good tool to use."However, I still found several software that can test the C language, such as cutest, Cunit and C + + test, but I couldn't find the software resources. I downloaded several software that needed money to use, and there was no cracked version. I was very disappointed because I spent several hours, which wasted my patience, so I had to skip this stage.
This topic is very difficult, but I have also learned a lot. I know how to analyze and overcome a problem. Although this process is extremely difficult, I have never written so many lines of code before, which greatly tests the application of my comprehensive knowledge. It is also very interesting to write down my experience in a blog, although I have some regrets in the end, I haven't finished all the tasks, but I'm still satisfied.