[문제]
(1) abcd/*1234*/efg
-> abcdefg
(2) abc/abcd/*1234*/efg
-> abc/abcdefg
(3) abc/*123*456*/def
-> abcdef
(4) abcd/*123*/ef/*45*/gh
-> abcdefgh
(5) abc/*/123*/def
-> abcdef
(6) abcd/*123/*ddd*/45*/ef
-> abcd45*/ef
(7) abcd//1234
-> abcd
(8) abcd//1234/*!!!*/567
-> abcd
(9) abcd/*123***/defg
-> abcddefg
(10) abcd/*123****/defg
-> abcddefg
////////////////////////////////////////////////////////////////////////////////////////
#define IN 1
#define OUT 0
#include <stdio.h>
#include <string.h>
void main()
{
FILE *fp;
char ch, ch2, ch3;
int state = OUT;
fp = fopen("output.txt", "w");
printf("\t<< 주석 삭제 프로그램 (종료 : ctrl+z) >>\n\n");
while((ch=getchar()) != EOF)
{
if(ch=='/')
{
if(ch3=='*') // 주석 종료 '*/'
state = OUT;
ch2 = getchar();
if(ch2=='*') // 주석 시작 '/*'
state = IN;
else if( state == IN ) // 주석 내부
continue;
else if ( ch2=='/' ) // 주석 시작 '//'
{
state = IN;
while(getchar() != '\n')
; // '//'는 enter까지 주석처리
}
else // 주석X
{
if(ch3!='*')
fputc(ch, fp);
fputc(ch2, fp);
}
}
else if(ch=='*' && state==IN)
{
ch3 = getchar();
if(ch3=='/') // 주석 종료 '*/'
state = OUT;
}
else if(state==OUT) // 주석 외부
fputc(ch, fp);
}
printf("\n=> output.txt에 저장 되었습니다\n");
}
댓글