Write a function that removes the annotation from the C + + program code and returns the result. __jquery
Last Update:2018-07-29
Source: Internet
Author: User
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
/**
* Function: Remove comments in C/C + + program code
*
* Input: A pointer to C + + program code
*/
void
Remove_comment (char *buf, size_t size)
{
Char *p, *end, C;
Char *sq_start, *dq_start;
Char *lc_start, *bc_start;
size_t Len;
p = buf;
End = p + size;
Sq_start = NULL;
Dq_start = NULL;
Lc_start = NULL;
Bc_start = NULL;
while (P < end) {
c = *p;
Switch (c) {
Case ' \ ':/* Single QUOTE * *
if (Dq_start | | lc_start | | bc_start) {
/* Ignore single quotes in strings and comments * *
p++;
Continue
}
if (Sq_start = = NULL) {
Sq_start = p++;
} else {
Len = p++-Sq_start;
if (len = = 2 && * (Sq_start + 1) = = ' \ ') {
/* Ignore single quotes in characters * * *
Continue
}
Sq_start = NULL;
}
Break
Case ' "':/* Double Quotes * *
if (Sq_start | | lc_start | | bc_start) {
/* Ignore double quotes in characters or comments * *
p++;
Continue
}
if (Dq_start = = NULL) {
Dq_start = p++;
} else {
if (* (p++-1) = = ' \ ') {
/* Ignore double quotes in String * *
Continue
}
Dq_start = NULL;
}
Break
Case '/':/* Oblique pole/*
if (Sq_start | | dq_start | | lc_start | | bc_start) {
/* Ignore diagonal bars in characters, strings, or comments
p++;
Continue
}
c = * (P + 1);
if (c = = '/') {
Lc_start = p;
p = 2;
else if (c = = ' * ') {
Bc_start = p;
p = 2;
} else {
/* Ignore Division * *
p++;
}
Break
Case ' * ': * * asterisk * *
if (Sq_start | | dq_start | | lc_start | | bc_start = NULL) {
/* ignores asterisks in characters, strings, or line comments, and ignores multiplication/
p++;
Continue
}
if (* (p + 1)!= '/') {
/* Ignore the asterisk in the block comment * *
p++;
Continue
}
p = 2;
memset (Bc_start, ', P-bc_start);
Bc_start = NULL;
Break
Case ' \ n ':/* newline character * *
if (Lc_start = = NULL) {
p++;
Continue
}
c = * (P-1);
memset (Lc_start, "",
(c = = ' \ r '? (p++-1): p++)-Lc_start);
Lc_start = NULL;
Break
Default
p++;
Break
}
}
if (Lc_start) {
memset (Lc_start, ', P-lc_start);
}
}
Int
Main (int argc, char *argv[])
{
int FD, N;
Char buf[102400];
FD = open ("C:\\Documents and settings\\administrator\\ desktop \\remove_comment.c",
_o_rdonly, 0);
if (fd = = 1) {
return-1;
}
n = Read (fd, buf, sizeof (BUF));
if (n = = 1 | | n = = 0) {
Close (FD);
return-1;
}
printf ("test\n");
Remove_comment (buf, N);
* (BUF + N) = ' ";
printf (BUF);
/***********\\\/////// */
Close (FD);
return 0;
}