There are a lot of typical questions in the programmer's interview book, which can be used for our consideration and will be very helpful to our interview.
The following is a question on page 1: compile a function to remove the comments in the C/C ++ program code, which I can't understand at first, later, I saw a netizen analyze the code in detail on the Internet. However, after reading the code, I thought there were a lot of errors in his analysis. So, I spent more than half a day analyzing the analysis and thought I understood it. The following is my analysis statement. If there are still some less rigorous parts, please advise!
/*************************************** *****************
Function: removes comments from C/C ++.
Input: pointer and length pointing to C/C ++ program code
Source: programmer interview 45th page
Analysis: Read a row at a time in two cases, because there are two Annotations:
(1) Search for "//" in the row obtained. If "//" is found, the "//" and its subsequent parts are discarded.
(2) search for "/*" in the row obtained, record the position pos1, and then find "*/" in this line. If yes, record the position pos2, discard the content and start with pos2 and continue searching for "/*". If it is not found in the current row, remove the "/*" and its contents from the current row, read a new row and find "*/", if not. Remove the row to be read, read a row, and search for "*/". For example, locate and record the position of pos2, and remove the characters between 0 and pos2.
(3) Perform steps 1 and 2 until the program ends.
Special situations for programming I:
I think this question is so painful and I am willing to fall.
# Include "stdio. H "# include" stdlib. H "# define maxbuf 102400 void removecomment (char * Buf, int length) {char * P, * end, C; int Len; char * singlequota, * doublequota, * lc_start, * bc_start; singlequota = NULL; doublequota = NULL; lc_start = NULL; bc_start = NULL; P = Buf; end = BUF + length; while (P <End) {c = * P; Switch (c) {Case '\ '': If (doublequota | lc_start | bc_start) {P ++; continue ;} else if (singlequota = NULL) {singlequota = P ++; continue;} else {c = * (p-1); Len = p-singlequota; If (LEN = 2 & C = '\\') {P ++; continue;} singlequota = NULL;} break; Case '\ "': If (singlequota | lc_start | bc_start) {P ++; continue ;} else if (doublequota = NULL) {doublequota = P ++; continue;} else {c = * (p-1); If (C = "\\") {P ++; Continue ;}} P ++; doublequota = NULL; break; Case '/': If (singlequota | lc_start | bc_start | doublequota) {P ++; continue;} c = * (p + 1); If (C = '/') {LC _ Start = P; P + = 2;} else if (C = '*') {bc_start = P; P + = 2;} else {P ++ ;} break; Case '*': If (singlequota | doublequota | lc_start | bc_start = NULL) {P ++; continue;} c = * (p + 1 ); if (C = '/') {P + = 2;/* a bug here should be BC clear */memset (bc_start, '', p-bc_start ); lc_start = NULL;} else {P ++;} break; Case '\ N': If (lc_start = NULL) {P ++; continue;} If (lc_start) {c = * (p-1); memset (lc_start, '', (C = '\ R ')? (P ++-1-lc_start) :( P ++-lc_start); lc_start = NULL;} break; default: P ++; break ;}} if (lc_start) /* this is very important, if there is such // double slash comment at the end of the file, you still need to consider */{memset (lc_start, '', p-lc_start );}} int main () {char Buf [maxbuf]; char C; int I = 0; int FD; FD = fopen ("test. C "," R "); If (FD = NULL) {exit (0);} while (C = fgetc (FD ))! = EOF) {Buf [I ++] = C;} fclose (FD); removecomment (BUF, I); Buf [I] = '\ 0 '; printf ("% s", Buf); Return 0 ;}
If it's okay, it looks easy, and it's complicated to implement it.