본문 바로가기

Native/C

예제 주석을 지워랏 (진행중)

/***************************************************************************
*
*        Date        : 2005-04-09
*        Copyright    : aucd29
*        E-mail        : aucd29@daum.net
*
*        주석 지우자
*
***************************************************************************/

#include <stdio.h>
#define MAXLINE 1000
#define AR_MAX 1000

int getline(char line[],int maxline);
void copy(char to[AR_MAX][MAXLINE],char from[],int *nCnt);
void ResultOut(char to[AR_MAX][MAXLINE], int nStrLen[], int *nCnt);
void UpLine(char to[AR_MAX][MAXLINE], int i, int *nCnt);
void BlankLineRemove(char to[AR_MAX][MAXLINE], int i, int j, int *nCnt);

void main()
{
    int len;
    // int max;
    char line[MAXLINE];
    char longest[AR_MAX][MAXLINE];
    int nStrLen[AR_MAX];
    int nCnt=0,i=0;

    for(i=0;i<AR_MAX;++i) nStrLen[i] = 0;    // 변수 초기화

    while((len = getline(line,MAXLINE)) > 0)
    {
        if(AR_MAX<nCnt) break;

        copy(longest, line, &nCnt);            // 문단 배열에 복사
        nStrLen[nCnt] = len;                // 단어 카운트

        ++nCnt;
    }

    if(nCnt >0) ResultOut(longest, nStrLen, &nCnt);
}

int getline(char s[], int lim)
{
    int c, i;

    for(i=0;i<lim-1 && (c=getchar()) != EOF && c!='\n'; ++i)
        s[i] = c;

    if(c == '\n')
    {
        s[i] = c;
        ++i;
    }

    s[i] = '\0';
    return i;
}

void copy(char to[AR_MAX][MAXLINE], char from[], int *nCnt)
{
    int i=0;

    // 글자를 복사한다.... 배열이므로 하나씩...
    while((to[*nCnt][i] = from[i]) != '\0')
        ++i;
}

void ResultOut(char to[AR_MAX][MAXLINE], int nStrLen[], int *nCnt)
{
    int i,j=0, nMultiCmtStart=0;

    for(i=0;i<*nCnt;++i)
    {
        // 코멘트를 지우자.
        while(to[i][j]!='\0')
        {
            // 한줄 주석 지우기
            if(to[i][j]=='/' && to[i][j+1]=='/')
            {
                //
                // 첫 줄 일경우는 배열을 다시 정의 하자.
                // 즉 한줄 땡겨주는 상태이다.
                //
                if(j==0)
                {
                    UpLine(to,i, nCnt);
                }
                else
                {
                    to[i][j] = '\n';
                    to[i][j+1] = '\0';

                    //BlankLineRemove(to,i,j,nCnt);
                }
            }

            // 여러줄 주석 지우기
            if(to[i][j]=='/' && to[i][j+1]=='*')
            {
                nMultiCmtStart = 1;
                to[i][j] = '\n';
                to[i][j+1] = '\0';
            }

            if(to[i][j]=='*' && to[i][j+1]=='/')
            {
                nMultiCmtStart = 0;

                to[i][j] = '\n';
                to[i][j+1] = '\0';
            }
            ++j;
        }

        if(nMultiCmtStart)
        {
            to[i][0] = '\n';
            to[i][1] = '\0';
        }

        j=0;    // 첫번째 줄로 이동하자.
    }

    // result print
    for(i=0;i<AR_MAX;++i)
    {
        if(*nCnt<i) break;
        if(nStrLen[i] > 0)
            printf("%s", to[i]);
    }
}

//
// 한줄 짜리 주석 지운뒤 올려주기
//
void UpLine(char to[AR_MAX][MAXLINE], int n, int *nCnt)
{
    int i=0;

    //printf("입력받은 줄 %d\t 주석 위치 %d\n", *nCnt, n);

    //
    // row수를 받아온 값을 넣는다.
    //
    while(1)
    {
        if(!(n<*nCnt)) break;
        to[n][0] = '\0';
        while(to[n+1][i]!='\0')    // data copy
        {
            to[n][i] = to[n+1][i];
            ++i;
        }
        
        if(i) to[n][i] = '\0';
        i=0;
        ++n;        
    }
    to[n-1][0] = '\0';
    *nCnt--;    // 줄 감소
}

void BlankLineRemove(char to[AR_MAX][MAXLINE], int n, int max, int *nCnt)
{
    int j=0,check=0;
    int i=0;

    // 주석의 앞부분이 공백뿐인지를 확인해서
    while(max<j)
    {
        if(to[n][j]!=' '||to[n][j]!='\t')
            check = 1;
        ++j;
    }

    // 공백만 있으면 줄을 삭제한다.
    if(!check)
    {
        while(1)
        {
            to[i][0]='\0';

            while(to[n+1][i]!='\0')    // data copy
            {
                to[n][i] = to[n+1][i];
                ++i;
            }
            if(i) to[n][i] = '\0';
            i=0;
            ++n;
        }
        to[n-1][0] = '\0';
        *nCnt--;    // 줄 감소
    }
}

'Native > C' 카테고리의 다른 글

char 를 float로 변경하라  (0) 2013.10.02
소프트웨어 개발 순서  (0) 2013.10.02
10진수를 2진수로 메모리상에서비교 & 이용  (0) 2013.10.02
16진수를 10진수로 바꾸자  (0) 2013.10.02
예제 1-23  (0) 2013.10.02