练习1-23:删去C语言程序中所有的注释语句(C程序设计语言 第2版)

 1 #include <stdio.h>
 2 main()
 3 {
 4     FILE * fp_i;
 5     FILE * fp_o;
 6     fp_i = fopen("input.txt", "r");
 7     fp_o = fopen("output.txt", "w");
 8     char ch;
 9     int sign;
10     while((ch=fgetc(fp_i)) != EOF){
11         if (ch == '/' ){
12 
13             ch=fgetc(fp_i);
14             if (ch == '/'){
15                 while(ch != '\n'){
16                     ch=fgetc(fp_i);
17                 }
18                 fputc('\n', fp_o);
19 
20             }else if(ch == '*'){
21                 ch = fgetc(fp_i);
22                 sign = 0;
23                 while(sign != 1 && ch != '\n'){
24                     if (ch == '*'){
25                         ch = fgetc(fp_i);
26                         if (ch == '/'){
27                             sign = 1;
28                         }
29                     }else{
30                         ch = fgetc(fp_i);
31                     }
32                 }
33                 fputc('\n', fp_o);
34 
35             }else{
36                 fputc('/', fp_o);
37                 fputc(ch, fp_o);
38             }
39         }else{
40             fputc(ch, fp_o);
41         }
42     }
43     fclose(fp_i);
44     fclose(fp_o);
45 }
View Code

 

 

posted on 2013-09-16 10:46  Samuel Yang  阅读(248)  评论(0编辑  收藏  举报