Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1172723
  • 博文数量: 341
  • 博客积分: 12744
  • 博客等级: 上将
  • 技术积分: 4040
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-12 09:34
文章分类
文章存档

2014年(1)

2013年(10)

2012年(17)

2011年(63)

2010年(102)

2009年(107)

2008年(41)

分类: C/C++

2010-07-10 14:29:19

这个程序适用于linux下的程序,windows下的需稍作修改

#include <stdio.h>
#include <stdlib.h>

/*
 * 该程序用于消除C/C++源代码中的注释
 */

enum STATE
{
    STATE_SLASH,
    STATE_SLASH_SLASH,
    STATE_SLASH_SLASH_BACKSLASH,
    STATE_SLASH_POINT,
    STATE_SLASH_POINT_POINT,
    STATE_QUOTA,
    STATE_QUOTA_BACKSLASH,
    STATE_APOSTROPHE,
    STATE_APOSTROPHE_BACKSLASH,
    STATE_NORMAL
};


int main(int argc, char** argv) {
    if( argc != 1 )
    {
        printf("usage: remove_comment xxx\n");
        exit(1);
    }
    int m_state = STATE_NORMAL;
    FILE *fp = fopen( argv[1], "r");
    if( fp == NULL )
    {
        printf("open file failed\n");
        exit(1);
    }
    int i_char;
    while((i_char = fgetc(fp)) != EOF)
    {
        switch(i_char)
        {
            case '/':
                switch(m_state)
                {
                    case STATE_NORMAL:
                        m_state = STATE_SLASH;
                        break;
                    case STATE_SLASH:
                        m_state = STATE_SLASH_SLASH;
                        break;
                    case STATE_SLASH_POINT_POINT:
                        m_state = STATE_NORMAL;
                        break;
                    case STATE_SLASH_SLASH:
                    case STATE_SLASH_POINT:
                        break;
                    case STATE_QUOTA:
                        putchar(i_char);
                        break;
                    case STATE_QUOTA_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_QUOTA;
                        break;
                    case STATE_APOSTROPHE:
                        putchar(i_char);
                        break;
                    case STATE_APOSTROPHE_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_APOSTROPHE;
                        break;
                    case STATE_SLASH_SLASH_BACKSLASH:
                        m_state = STATE_SLASH_SLASH;
                        break;
                }
                break;
            case '"':
                switch(m_state)
                {
                    case STATE_NORMAL:
                        m_state = STATE_QUOTA;
                        putchar(i_char);
                        break;
                    case STATE_SLASH:
                        m_state = STATE_NORMAL;
                        putchar('/');
                        putchar(i_char);
                        break;
                    case STATE_SLASH_POINT_POINT:
                        m_state = STATE_SLASH_POINT;
                        break;
                    case STATE_SLASH_SLASH:
                    case STATE_SLASH_POINT:
                        break;
                    case STATE_QUOTA:
                        m_state = STATE_NORMAL;
                        putchar(i_char);
                        break;
                    case STATE_QUOTA_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_QUOTA;
                        break;
                    case STATE_APOSTROPHE:
                        putchar(i_char);
                        break;
                    case STATE_APOSTROPHE_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_APOSTROPHE;
                        break;
                    case STATE_SLASH_SLASH_BACKSLASH:
                        m_state = STATE_SLASH_SLASH;
                        break;
                }
                break;
            case '\'':
                switch(m_state)
                {
                    case STATE_NORMAL:
                        m_state = STATE_APOSTROPHE;
                        putchar(i_char);
                        break;
                    case STATE_SLASH:
                        m_state = STATE_NORMAL;
                        putchar('/');
                        putchar(i_char);
                        break;
                    case STATE_SLASH_POINT_POINT:
                        m_state = STATE_SLASH_POINT;
                        break;
                    case STATE_SLASH_SLASH:
                    case STATE_SLASH_POINT:
                        break;
                    case STATE_QUOTA:
                        putchar(i_char);
                        break;
                    case STATE_QUOTA_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_QUOTA;
                        break;
                    case STATE_APOSTROPHE:
                        putchar(i_char);
                        m_state = STATE_NORMAL;
                        break;
                    case STATE_APOSTROPHE_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_APOSTROPHE;
                        break;
                    case STATE_SLASH_SLASH_BACKSLASH:
                        m_state = STATE_SLASH_SLASH;
                        break;
                }
                break;
            case '\n':
                switch(m_state)
                {
                    case STATE_NORMAL:
                        putchar(i_char);
                        break;
                    case STATE_SLASH:
                        m_state = STATE_NORMAL;
                        putchar('/');
                        putchar(i_char);
                        break;
                    case STATE_SLASH_POINT_POINT:
                        m_state = STATE_SLASH_POINT;
                        break;
                    case STATE_SLASH_SLASH:
                        m_state = STATE_NORMAL;
                        putchar(i_char);
                        break;
                    case STATE_SLASH_POINT:
                        break;
                    case STATE_QUOTA:
                        // 这个情况语法出错了

                        break;
                    case STATE_QUOTA_BACKSLASH:
                        // 这个情况语法出错了

                        break;
                    case STATE_APOSTROPHE:
                        // 这个情况语法出错了

                        break;
                    case STATE_APOSTROPHE_BACKSLASH:
                        // 这个情况语法出错了

                        break;
                    case STATE_SLASH_SLASH_BACKSLASH:
                        m_state = STATE_SLASH_SLASH;
                        break;
                }
                break;
            case '*':
                switch(m_state)
                {
                    case STATE_NORMAL:
                        putchar(i_char);
                        break;
                    case STATE_SLASH:
                        m_state = STATE_SLASH_POINT;
                        break;
                    case STATE_SLASH_POINT_POINT:
                        m_state = STATE_SLASH_POINT;
                        break;
                    case STATE_SLASH_SLASH:
                        break;
                    case STATE_SLASH_POINT:
                        m_state = STATE_SLASH_POINT_POINT;
                        break;
                    case STATE_QUOTA:
                        putchar(i_char);
                        break;
                    case STATE_QUOTA_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_QUOTA;
                        break;
                    case STATE_APOSTROPHE:
                        putchar(i_char);
                        break;
                    case STATE_APOSTROPHE_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_APOSTROPHE;
                        break;
                    case STATE_SLASH_SLASH_BACKSLASH:
                        m_state = STATE_SLASH_SLASH;
                        break;
                }
                break;
            case '\\':
                switch(m_state)
                {
                    case STATE_NORMAL:
                        putchar(i_char);
                        break;
                    case STATE_SLASH:
                        m_state = STATE_SLASH_POINT;
                        putchar('/');
                        putchar(i_char);
                        break;
                    case STATE_SLASH_POINT_POINT:
                        m_state = STATE_SLASH_POINT;
                        break;
                    case STATE_SLASH_SLASH:
                        m_state = STATE_SLASH_SLASH_BACKSLASH;
                        break;
                    case STATE_SLASH_POINT:
                        break;
                    case STATE_QUOTA:
                        m_state = STATE_QUOTA_BACKSLASH;
                        putchar(i_char);
                        break;
                    case STATE_QUOTA_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_QUOTA;
                        break;
                    case STATE_APOSTROPHE:
                        m_state = STATE_APOSTROPHE_BACKSLASH;
                        putchar(i_char);
                        break;
                    case STATE_APOSTROPHE_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_APOSTROPHE;
                        break;
                    case STATE_SLASH_SLASH_BACKSLASH:
                        m_state = STATE_SLASH_SLASH;
                        break;
                }
                break;
            case '\ ':
            case '\t':
                switch(m_state)
                {
                    case STATE_NORMAL:
                        putchar(i_char);
                        break;
                    case STATE_SLASH:
                        m_state = STATE_NORMAL;
                        putchar('/');
                        putchar(i_char);
                        break;
                    case STATE_SLASH_POINT_POINT:
                        m_state = STATE_SLASH_POINT;
                        break;
                    case STATE_SLASH_SLASH:
                    case STATE_SLASH_POINT:
                        break;
                    case STATE_QUOTA:
                        putchar(i_char);
                        break;
                    case STATE_QUOTA_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_QUOTA;
                        break;
                    case STATE_APOSTROPHE:
                        putchar(i_char);
                        break;
                    case STATE_APOSTROPHE_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_APOSTROPHE;
                        break;
                    case STATE_SLASH_SLASH_BACKSLASH:
                        m_state = STATE_SLASH_SLASH_BACKSLASH;
                        break;
                }
                break;
            default:
                switch(m_state)
                {
                    case STATE_NORMAL:
                        putchar(i_char);
                        break;
                    case STATE_SLASH:
                        m_state = STATE_NORMAL;
                        putchar('/');
                        putchar(i_char);
                        break;
                    case STATE_SLASH_POINT_POINT:
                        m_state = STATE_SLASH_POINT;
                        break;
                    case STATE_SLASH_SLASH:
                    case STATE_SLASH_POINT:
                        break;
                    case STATE_QUOTA:
                        putchar(i_char);
                        break;
                    case STATE_QUOTA_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_QUOTA;
                        break;
                    case STATE_APOSTROPHE:
                        putchar(i_char);
                        break;
                    case STATE_APOSTROPHE_BACKSLASH:
                        putchar(i_char);
                        m_state = STATE_APOSTROPHE;
                        break;
                    case STATE_SLASH_SLASH_BACKSLASH:
                        m_state = STATE_SLASH_SLASH;
                        break;
                }
                break;
        }
    }
    fclose(fp);
    return (EXIT_SUCCESS);
}


阅读(817) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~