删除从C程序的所有意见 - 这个code任何可能的改进? [英] Remove all comments from a C program - any possible improvements to this code?

查看:90
本文介绍了删除从C程序的所有意见 - 这个code任何可能的改进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习从K&放C; R书,在第一章练习1.23,我必须写一个程序,消除给出了一些C code。该用户输入的所有注释。这是我完成的程序为止。是否有任何改善,我可以做呢?

  / **
 周二,2013年10月7日 练习1.23
 编写一个程序,从C删除所有评论
 程序。不要忘了处理引用的字符串
 和字符常量正确。 C注释
 不要窝。
** /#包括LT&;&stdio.h中GT;
#定义MAX_LENGTH 65536
#定义NOT_IN_COMMENT 0
#定义SINGLE_COMMENT 1
#定义MULTI_COMMENT 2主要()
{
    字符code [MAX_LENGTH] / *缓冲存储输入code * /
    INT大小= 0; / *输入code *长度/
    INT回路; / *整数用于循环* /
    焦炭℃; / *字符来输入* /
    INT状态= NOT_IN_COMMENT; / *我们是在一个评论?什么样的? * /
    INT in_string = 0; / *我们是一个字符串常量里面? * /
    焦炭last_character; / *最后一个字符值* /
    / *所有输入code到缓冲区中,直到转义序列pressed * /
    而((C =的getchar())!= EOF)
        code [尺寸++] = C;
    code [大小] ='\\ 0';
    / *取下code和显示效果都评论用户* /
    对于(循环= 0;环路LT;大小;环++){
        烧焦电流= code [循环]        如果(in_string){
            如果(当前=='')in_string = 0;
            的putchar(电流);
        }        其他{
            如果(状态== NOT_IN_COMMENT){
                如果(当前==''){
                    的putchar(电流);
                    in_string = 1;
                    继续;
                }                如果(当前=='/'和;&安培; last_character =='/')状态= SINGLE_COMMENT;
                否则,如果(当前=='*'和;&安培; last_character =='/')状态= MULTI_COMMENT;
                否则,如果(电流='/'||(当前=='/'和;!&安培;环<大小1安培;&安培;!(code [环+ 1] =='/'|| code [环+ 1] =='*')))的putchar(电流);
            }            否则,如果(状态== SINGLE_COMMENT){
                如果(当前=='\\ n'){
                    状态= NOT_IN_COMMENT;
                    的putchar('\\ n');
                }
            }            否则,如果(状态== MULTI_COMMENT){
                如果(当前=='/'和;&放大器; last_character =='*')状态= NOT_IN_COMMENT;
            }
        }        last_character =电流;
    }
}


解决方案

将您的意见剥离成一个功能(更多有用),以及用于fgets()一次读取一行,last_character是模糊的(意思去年或previous?),这款采用少得多调用的putchar()中,只有一个的printf(可以使用看跌期权)每行,preserves大部分你在做什么,

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#定义MAX_LENGTH 65536#定义NOT_IN_COMMENT 0
#定义SINGLE_COMMENT 1
#定义MULTI_COMMENT 2
INT状态= NOT_IN_COMMENT; / *我们是在一个评论?什么样的? * /
INT in_string = 0; / *我们是一个字符串常量里面? * /
字符* stripcomments(字符*剥离,字符* code)
{
    INT NDX; / *指数为code [] * /
    INT ondx; / *指数输出[] * /
    焦炭preVCH; / *的previous字符值* /
    焦炭CH; / *字符来输入* /    / *取下code和显示效果都评论用户* /
    对于(NDX = ondx = 0;&NDX LT;的strlen(code); NDX ++)
    {
        烧焦电流= code [NDX]        如果(in_string){
            如果(当前=='')in_string = 0;
            剥离[ondx ++] =电流;
        }
        其他{
            如果(状态== NOT_IN_COMMENT){
                如果(当前==''){
                    剥离[ondx ++] =电流;
                    in_string = 1;
                    继续;
                }                如果(当前=='/'和;&安培; preVCH =='/')状态= SINGLE_COMMENT;
                否则,如果(当前=='*'和;&安培; preVCH =='/')状态= MULTI_COMMENT;
                否则,如果(电流='/'||(当前=='/'和;!&安培; NDX<的strlen(code)-1放大器;&安培;!(code [NDX + 1] = ='/'|| code [NDX + 1] =='*')))剥离[ondx ++] =电流;
            }            否则,如果(状态== SINGLE_COMMENT){
                如果(当前=='\\ n'){
                    状态= NOT_IN_COMMENT;
                    剥离[ondx ++] ='\\ n';
                }
            }            否则,如果(状态== MULTI_COMMENT){
                如果(当前=='/'和;&放大器; preVCH =='*')状态= NOT_IN_COMMENT;
            }
        }
        preVCH =电流;
    }
    剥离[ondx] ='\\ 0';
    返回(剥离);
}INT主要(无效)
{
    字符code [MAX_LENGTH] / *缓冲存储输入code * /
    焦炭剥离[MAX_LENGTH]    而(与fgets(code,sizeof的(code),标准输入))
    {
        //的printf(%S \\ n,code);
        //删除评论...
        stripcomments(剥离,code);
        如果(strlen的(剥离)大于0)的printf(%S,汽提);
    }
}

我将让你去除多余的空行。

I'm learning C from the K&R book and for exercise 1.23 in the first chapter, I have to write a program that removes all comments given some C code that the user inputs. This is my completed program so far. Are there any improvements I can make to it?

/**
 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; 
    }
}

解决方案

Move your stripping of comments into a function (more useful), and read one line at a time with fgets(), last_character is ambiguous (does it mean last, or previous?), this uses far fewer calls to putchar(), only one printf (could use puts) per line, preserves most of what you were doing,

#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);
    }
}

I'll leave it to you to remove extra blank lines.

这篇关于删除从C程序的所有意见 - 这个code任何可能的改进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆