鱼C论坛

 找回密码
 立即注册
查看: 1160|回复: 4

[已解决]求做一道oj题

[复制链接]
发表于 2020-3-24 12:35:15 | 显示全部楼层 |阅读模式
5鱼币
本帖最后由 nameisnothing 于 2020-3-24 17:51 编辑

学校oj上的一道题目,卡了很久了,谁能帮帮我。我目前只会最基础的c语言,没学数据结构和算法,文件操作只会最简单的fopen,putc,fseek等函数,会用的函数库也只有stdio.h,string.h这些。所以各位大哥尽量用基础方法解决,让我这个初学者能看得懂。
问题 C: 实验12_4_过滤注释
时间限制: 1 Sec  内存限制: 128 MB
题目描述
C语言的注释分为两种,第一种:在一行源代码中“//”后的内容为注释内容。第二种:“/*”与“*/”之间的内容为注释内容。第三种:程序中只出现了“/*”,没有“*/”与之对应,那么将“/*”后的全部内容都要过滤掉。注意,只要是注释内容,那么注释内容中的字符应该全部忽略,即不起任何的作用。例如“/*”与“*/”之间如果再有“//”,那么“//”不应起作用;如果“//”再有“/*”,那么“/*”也不应起作用。
你的任务是先打开一个名字为dict.dic的文本文件,该文件中前5行每行为1个整数,从第6行开始为5段C语言的源代码。那5个数字代表这5段源代码结束的行数,比如如果第一行的整数为20,第二行的整数为60,则表示从第6行到第20为第一段代码,从第21行到第60为第二段代码。然后根据输入要求将源代码中所有注释过滤掉。
在本过滤注释系统中,你可以忽略源文件中双引号导致“//”、“/*”、“*/”失去作用的情况,即只要“//”、“/*”、“*/”不是注释内容,在任何情况下都起作用。
输入
只可能是1,2,3,4,5之一
输出
输入为1则输出第一段代码过滤后的结果,输入为2则输出第二段代码过滤后的结果,依此类推。
样例输入
1
样例输出
如果第一段代码是这样:


#include<stdio.h>

int main()
{
        int a = 10 , b = 2 , c ;
        c = a / b ; //I just want to test '/'
        printf("I love programming C.\n") ; //"printf" is a useful function /*
        printf("I hope you love it too!\n") ;
        /*
        //C is not always hard , if you love it , it will not treat you rough.
        */
        return 0 ;
}

则输出是这样:


#include<stdio.h>

int main()
{
        int a = 10 , b = 2 , c ;
        c = a / b ;
        printf("I love programming C.\n") ;
        printf("I hope you love it too!\n") ;

        return 0 ;
}
最佳答案
2020-3-24 12:35:16
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>

  4. void print(FILE *fp, int sel)
  5. {
  6.     char buff[1024];
  7.     int start = 0;
  8.     int end;
  9.     bool comment = false;
  10.     bool string = false;
  11.     for(int i = 1; i <= 5; ++i) {
  12.         int temp;
  13.         fscanf(fp, "%d", &temp);
  14.         if(i == sel - 1) start = temp + 1;
  15.         if(i == sel) end = temp;
  16.     }
  17.     fgetc(fp);
  18.     if(!start) start = 6;
  19.     for(int i = 6; i < start; ++i)
  20.         fgets(buff, 1024, fp);
  21.     for(int i = start; i <= end; ++i) {
  22.         fgets(buff, 1024, fp);
  23.         for(int j = 0; buff[j]; ++j) {
  24.             if(comment) {
  25.                 if(buff[j + 1] && (buff[j] == '*' && buff[j + 1] == '/')) {
  26.                     ++j;
  27.                     comment = false;
  28.                 }
  29.             } else if(string) {
  30.                 if(buff[j] == '"') string = false;
  31.                 printf("%c", buff[j]);
  32.             } else {
  33.                 if(buff[j + 1] && (buff[j] == '/' && buff[j + 1] == '/')) {
  34.                     printf("\n");
  35.                     break;
  36.                 }
  37.                 if(buff[j + 1] && (buff[j] == '/' && buff[j + 1] == '*')) {
  38.                     ++j;
  39.                     comment = true;
  40.                     continue;
  41.                 }
  42.                 if(buff[j] == '"') string = true;
  43.                 printf("%c", buff[j]);
  44.             }
  45.         }
  46.     }
  47. }

  48. int main(void)
  49. {
  50.     int sel;
  51.     FILE *fp = fopen("dict.dic", "r");
  52.     printf("请输入: ");
  53.     scanf("%d", &sel);
  54.     if(sel >= 1 && sel <= 5)
  55.         print(fp, sel);
  56.     fclose(fp);
  57.     return 0;
  58. }
复制代码


dict.dic
  1. 19
  2. 60
  3. 80
  4. 89
  5. 103
  6. // 1
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>

  10. void Free_String(char **mybuf, int num)
  11. {
  12.     if(mybuf == NULL)
  13.         return;
  14.     for(int i = 0; i < num; ++i) {
  15.         free(mybuf[i]);
  16.     }
  17.     free(mybuf);
  18. }
  19. // 2
  20. // 分割字符串
  21. int spitString(const char *mytmp, char c, char ***mybuf, int *num)
  22. {
  23.     if(mytmp == NULL || num == NULL)
  24.         return -1;
  25.     const char *p1 = mytmp;
  26.     int count = 0;

  27.     /* 先扫描一遍字符串,得到子串数量*/
  28.     /* // abcd */
  29.     // /* abcd
  30.     while(p1 = strchr(p1, ','))
  31.     {
  32.         count++;
  33.         p1++;
  34.     }
  35.     ++count;
  36.     char **pp = NULL;
  37.     pp = malloc(count * sizeof(char *));
  38.     if(pp == NULL)
  39.     {
  40.         *mybuf = pp;
  41.         return -2;
  42.     }
  43.     const char *p = mytmp, *q;
  44.     for(int i = 0; i < count; ++i)  // 初始化这些指针  /* abcd */
  45.         pp[i] = NULL;
  46.     for(int i = 0; i < count; ++i) {
  47.         q = strchr(p, c);
  48.         if(!q) q = p + strlen(p);
  49.         pp[i] = malloc(q - p + 1);
  50.         if(!pp[i]) return -3;
  51.         strncpy(pp[i], p, q - p);
  52.         pp[i][q - p] = '\0';
  53.         p = q + 1;
  54.     }
  55.     *mybuf = pp;
  56.     *num = count;
  57.     return 0;
  58. }
  59. // 3
  60. int main(void)
  61. {
  62.     int cls = 0;
  63.     char tmp[100] = {"abcdef,acccd,eeee,aaaa,e3eeee,/* abc */,ssssa,//"};
  64.     char c = ',';
  65.     int num = 0, i;
  66.     char **buf = NULL;
  67.     cls = spitString(tmp, c, &buf, &num);
  68.     if(cls != 0)
  69.     {
  70.         printf("fucn spitString() err: %d\n", cls);
  71.         return cls;
  72.     }
  73.     for(i = 0; i < num; i++)
  74.         printf("%s\n", buf[i]);
  75.     printf("%d\n", num);
  76.     Free_String(buf, num);
  77.     return cls;
  78. }
  79. // 4
  80. /*
  81. #include <stdio.h>

  82. int main(void)
  83. {
  84.         printf("hello world!\n");
  85.         return 0;
  86. }
  87. // 5
  88. #include<stdio.h>

  89. int main()
  90. {
  91.         int a = 10 , b = 2 , c ;
  92.         c = a / b ; //I just want to test '/'
  93.         printf("I love programming C.\n") ; //"printf" is a useful function /*
  94.         printf("I hope you love it too!\n") ;
  95.         /*
  96.         //C is not always hard , if you love it , it will not treat you rough.
  97.         */
  98.         return 0 ;
  99. }
复制代码

最佳答案

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-24 12:35:16 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>

  4. void print(FILE *fp, int sel)
  5. {
  6.     char buff[1024];
  7.     int start = 0;
  8.     int end;
  9.     bool comment = false;
  10.     bool string = false;
  11.     for(int i = 1; i <= 5; ++i) {
  12.         int temp;
  13.         fscanf(fp, "%d", &temp);
  14.         if(i == sel - 1) start = temp + 1;
  15.         if(i == sel) end = temp;
  16.     }
  17.     fgetc(fp);
  18.     if(!start) start = 6;
  19.     for(int i = 6; i < start; ++i)
  20.         fgets(buff, 1024, fp);
  21.     for(int i = start; i <= end; ++i) {
  22.         fgets(buff, 1024, fp);
  23.         for(int j = 0; buff[j]; ++j) {
  24.             if(comment) {
  25.                 if(buff[j + 1] && (buff[j] == '*' && buff[j + 1] == '/')) {
  26.                     ++j;
  27.                     comment = false;
  28.                 }
  29.             } else if(string) {
  30.                 if(buff[j] == '"') string = false;
  31.                 printf("%c", buff[j]);
  32.             } else {
  33.                 if(buff[j + 1] && (buff[j] == '/' && buff[j + 1] == '/')) {
  34.                     printf("\n");
  35.                     break;
  36.                 }
  37.                 if(buff[j + 1] && (buff[j] == '/' && buff[j + 1] == '*')) {
  38.                     ++j;
  39.                     comment = true;
  40.                     continue;
  41.                 }
  42.                 if(buff[j] == '"') string = true;
  43.                 printf("%c", buff[j]);
  44.             }
  45.         }
  46.     }
  47. }

  48. int main(void)
  49. {
  50.     int sel;
  51.     FILE *fp = fopen("dict.dic", "r");
  52.     printf("请输入: ");
  53.     scanf("%d", &sel);
  54.     if(sel >= 1 && sel <= 5)
  55.         print(fp, sel);
  56.     fclose(fp);
  57.     return 0;
  58. }
复制代码


dict.dic
  1. 19
  2. 60
  3. 80
  4. 89
  5. 103
  6. // 1
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>

  10. void Free_String(char **mybuf, int num)
  11. {
  12.     if(mybuf == NULL)
  13.         return;
  14.     for(int i = 0; i < num; ++i) {
  15.         free(mybuf[i]);
  16.     }
  17.     free(mybuf);
  18. }
  19. // 2
  20. // 分割字符串
  21. int spitString(const char *mytmp, char c, char ***mybuf, int *num)
  22. {
  23.     if(mytmp == NULL || num == NULL)
  24.         return -1;
  25.     const char *p1 = mytmp;
  26.     int count = 0;

  27.     /* 先扫描一遍字符串,得到子串数量*/
  28.     /* // abcd */
  29.     // /* abcd
  30.     while(p1 = strchr(p1, ','))
  31.     {
  32.         count++;
  33.         p1++;
  34.     }
  35.     ++count;
  36.     char **pp = NULL;
  37.     pp = malloc(count * sizeof(char *));
  38.     if(pp == NULL)
  39.     {
  40.         *mybuf = pp;
  41.         return -2;
  42.     }
  43.     const char *p = mytmp, *q;
  44.     for(int i = 0; i < count; ++i)  // 初始化这些指针  /* abcd */
  45.         pp[i] = NULL;
  46.     for(int i = 0; i < count; ++i) {
  47.         q = strchr(p, c);
  48.         if(!q) q = p + strlen(p);
  49.         pp[i] = malloc(q - p + 1);
  50.         if(!pp[i]) return -3;
  51.         strncpy(pp[i], p, q - p);
  52.         pp[i][q - p] = '\0';
  53.         p = q + 1;
  54.     }
  55.     *mybuf = pp;
  56.     *num = count;
  57.     return 0;
  58. }
  59. // 3
  60. int main(void)
  61. {
  62.     int cls = 0;
  63.     char tmp[100] = {"abcdef,acccd,eeee,aaaa,e3eeee,/* abc */,ssssa,//"};
  64.     char c = ',';
  65.     int num = 0, i;
  66.     char **buf = NULL;
  67.     cls = spitString(tmp, c, &buf, &num);
  68.     if(cls != 0)
  69.     {
  70.         printf("fucn spitString() err: %d\n", cls);
  71.         return cls;
  72.     }
  73.     for(i = 0; i < num; i++)
  74.         printf("%s\n", buf[i]);
  75.     printf("%d\n", num);
  76.     Free_String(buf, num);
  77.     return cls;
  78. }
  79. // 4
  80. /*
  81. #include <stdio.h>

  82. int main(void)
  83. {
  84.         printf("hello world!\n");
  85.         return 0;
  86. }
  87. // 5
  88. #include<stdio.h>

  89. int main()
  90. {
  91.         int a = 10 , b = 2 , c ;
  92.         c = a / b ; //I just want to test '/'
  93.         printf("I love programming C.\n") ; //"printf" is a useful function /*
  94.         printf("I hope you love it too!\n") ;
  95.         /*
  96.         //C is not always hard , if you love it , it will not treat you rough.
  97.         */
  98.         return 0 ;
  99. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-24 12:47:49 | 显示全部楼层
可能会用到的函数有getc,putc,fopen,fclose,fseek,ftell,fread,fwrite,fgets,fputs,fflush,都在stdio.h中。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-24 15:44:57 | 显示全部楼层
能不能提供 dict.dic 文件?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-24 16:55:47 | 显示全部楼层
人造人 发表于 2020-3-24 15:44
能不能提供 dict.dic 文件?

这个是文件是Oj测试用例,我也不知道
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 14:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表