gpt4 book ai didi

c - 删除 C 程序中的所有注释 - 此代码是否有任何可能的改进?

转载 作者:太空狗 更新时间:2023-10-29 15:00:43 28 4
gpt4 key购买 nike

我正在从 K&R 书中学习 C,在第一章的练习 1.23 中,我必须编写一个程序,在给定用户输入的一些 C 代码的情况下删除所有注释。这是我到目前为止完成的程序。我可以对其进行任何改进吗?

/**
Tuesday, 10/07/2013

Exercise 1.23
Write a program to remove all comments from a C
program. Don't forget to handle quoted strings
and character constants properly. C comments
don't nest.
**/

#include <stdio.h>
#define MAX_LENGTH 65536
#define NOT_IN_COMMENT 0
#define SINGLE_COMMENT 1
#define MULTI_COMMENT 2

main()
{
char code[MAX_LENGTH]; /* Buffer that stores the inputted code */
int size = 0; /* Length of the inputted code */
int loop; /* Integer used for the for loop */
char c; /* Character to input into */
int status = NOT_IN_COMMENT; /* Are we in a comment? What type? */
int in_string = 0; /* Are we inside of a string constant? */
char last_character; /* Value of the last character */


/* Input all code into the buffer until escape sequence pressed */
while ((c = getchar()) != EOF)
code[size++] = c;
code[size] = '\0';


/* Remove all comments from the code and display results to user */
for (loop = 0; loop < size; loop++) {
char current = code[loop];

if (in_string) {
if (current == '"') in_string = 0;
putchar(current);
}

else {
if (status == NOT_IN_COMMENT) {
if (current == '"') {
putchar(current);
in_string = 1;
continue;
}

if (current == '/' && last_character == '/') status = SINGLE_COMMENT;
else if (current == '*' && last_character == '/') status = MULTI_COMMENT;
else if (current != '/' || (current == '/' && loop < size-1 && !(code[loop+1] == '/' || code[loop+1] == '*'))) putchar(current);
}

else if (status == SINGLE_COMMENT) {
if (current == '\n') {
status = NOT_IN_COMMENT;
putchar('\n');
}
}

else if (status == MULTI_COMMENT) {
if (current == '/' && last_character == '*') status = NOT_IN_COMMENT;
}
}

last_character = current;
}
}

最佳答案

将你的注释剥离移动到一个函数中(更有用),并使用 fgets() 一次读取一行,last_character 是不明确的(它是最后一个,还是前一个?),这使用更少的调用 putchar( ), 每行只有一个 printf(可以使用 puts),保留你正在做的大部分事情,

#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 65536

#define NOT_IN_COMMENT 0
#define SINGLE_COMMENT 1
#define MULTI_COMMENT 2
int status = NOT_IN_COMMENT; /* Are we in a comment? What type? */
int in_string = 0; /* Are we inside of a string constant? */
char* stripcomments(char* stripped,char* code)
{
int ndx; /* index for code[] */
int ondx; /* index for output[] */
char prevch; /* Value of the previous character */
char ch; /* Character to input into */

/* Remove all comments from the code and display results to user */
for (ndx=ondx=0; ndx < strlen(code); ndx++)
{
char current = code[ndx];

if (in_string) {
if (current == '"') in_string = 0;
stripped[ondx++] = current;
}
else {
if (status == NOT_IN_COMMENT) {
if (current == '"') {
stripped[ondx++] = current;
in_string = 1;
continue;
}

if (current == '/' && prevch == '/') status = SINGLE_COMMENT;
else if (current == '*' && prevch == '/') status = MULTI_COMMENT;
else if (current != '/' || (current == '/' && ndx < strlen(code)-1 && !(code[ndx+1] == '/' || code[ndx+1] == '*'))) stripped[ondx++] = current;
}

else if (status == SINGLE_COMMENT) {
if (current == '\n') {
status = NOT_IN_COMMENT;
stripped[ondx++] = '\n';
}
}

else if (status == MULTI_COMMENT) {
if (current == '/' && prevch == '*') status = NOT_IN_COMMENT;
}
}
prevch = current;
}
stripped[ondx] = '\0';
return(stripped);
}

int main(void)
{
char code[MAX_LENGTH]; /* Buffer that stores the inputted code */
char stripped[MAX_LENGTH];

while( fgets(code,sizeof(code),stdin) )
{
//printf("%s\n",code);
//strip comments...
stripcomments(stripped,code);
if( strlen(stripped) > 0 ) printf("%s",stripped);
}
}

我会留给你删除多余的空行。

关于c - 删除 C 程序中的所有注释 - 此代码是否有任何可能的改进?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19238390/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com