| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 407 人关注过本帖
标题:帮忙检查一下 去掉注释问题
只看楼主 加入收藏
witjay
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2011-3-10
结帖率:83.33%
收藏
已结贴  问题点数:6 回复次数:4 
帮忙检查一下 去掉注释问题
题目是去掉源文件中的注释  // ....    /* ....  */

#include <stdio.h>
#include <stdlib.h>
void main()
{
    FILE *fin,*fout;
    char ch,pre;
    char resource[256],target[256];
    printf("请输入源文件名:\n");
    scanf("%s",resource);
        if((fin=fopen(resource,"r"))==NULL)
    {
        printf("打开源文件出错:\n");
        exit(0);
    }

    printf("请输入目标文件名:\n");
    scanf("%s",target);


    if((fout=fopen(target,"w"))==NULL)
    {
        printf("打开目标文件出错:\n");
            exit(0);
    }

    while(!feof(fin))
    {
        ch=fgetc(fin);
        if(ch=='/')
        {   pre=ch;
            ch=fgetc(fin);
            if(ch=='/')
            {   
                while(ch!='/n')
                    ch=fgetc(fin);
                fputc(ch,fout);
            }
            else if(ch=='*')
            {
                pre=fgetc(fin);
                ch=fgetc(fin);
                while(pre!='*'&&ch!='*')
                    ch=fgetc(fin),pre=ch;
            }
            else
               fputc(pre,fout);
               fputc(ch,fout);
        }
        else
            fputc(ch,fout);
    }
    fclose(fin);
    fclose(fout);
}编译成功后运行,输入目标文件按回车后 一直等待 ,生成文件为空 。
搜索更多相关主题的帖子: 源文件 
2011-03-26 23:10
ppfly
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:297
专家分:1956
注 册:2009-5-17
收藏
得分:3 
回复 楼主 witjay
修改后的code,自己对照。
注意两个斜杠:'\','/';
程序代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fin,*fout;
    char ch,pre;
    char resource[256],target[256];
    printf("请输入源文件名:\n");
    scanf("%s",resource);
    if((fin=fopen(resource,"r"))==NULL)
    {
        printf("打开源文件出错:\n");
        exit(0);
    }
    printf("请输入目标文件名:\n");
    scanf("%s",target);
    if((fout=fopen(target,"w"))==NULL)
    {
        printf("打开目标文件出错:\n");
        exit(0);
    }
    while(!feof(fin))
    {
        ch=fgetc(fin);
        if(ch=='/')
        {   pre=ch;
            ch=fgetc(fin);
            if(ch=='/')
            {  
                while(ch!='\n')
                    ch=fgetc(fin);
                fputc(ch,fout);
            }
            else if(ch=='*')
            {
                pre=fgetc(fin);
                ch=fgetc(fin);
                while(pre!='*' && ch!='/')
                    ch=fgetc(fin),pre=ch;
                ch=fgetc(fin),pre=ch;
            }
            else
            {
                fputc(pre,fout);
                fputc(ch,fout);
            }
        }
        else
            fputc(ch,fout);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}

你的code可以进一步改进,去掉不必要的换行符,可以加一些if判断。

********多贴代码,少说空话*******
2011-03-27 00:06
witjay
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2011-3-10
收藏
得分:0 
回复 2楼 ppfly
else if(ch=='*')
            {
                pre=fgetc(fin);
                ch=fgetc(fin);
                while(pre!='*' && ch!='/')
                    ch=fgetc(fin),pre=ch;    // ch取文件下一个字符,然后将ch值赋予pre   那while循环判断的不是两个相同的字符吗?
                ch=fgetc(fin),pre=ch;
            }
这段代码有问题,
2011-03-27 10:29
flylee
Rank: 5Rank: 5
等 级:职业侠客
帖 子:309
专家分:374
注 册:2004-8-10
收藏
得分:3 
printf("//I am a comment");
考虑过这种情况吗
2011-03-27 11:11
flylee
Rank: 5Rank: 5
等 级:职业侠客
帖 子:309
专家分:374
注 册:2004-8-10
收藏
得分:0 
闲来无事,我也写了一个
程序代码:

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

int match(char *pos_start, char *pos_end, char *pattern, char **ptr)
{
    int v_match = 0, p_len;
    char *tmp = pos_start;
    *ptr = 0;
    p_len = strlen(pattern);
    while((tmp<pos_end) &&
            (tmp=strstr(tmp, pattern))) {
        v_match++;
        *ptr = *ptr ? *ptr : tmp;
        tmp += p_len;
    }
    return v_match;
}


int main(int argc, char *argv[])
{
    FILE *fin, *fout;
    char *buf, *ptr, *tmp, *p_end;
    int nbytes, i;
    buf = (char*)malloc(4096);
    if(argc !=3 ) {
        printf("<usage> uncomment source target\n");
        return 1;
    }

    if(fin = fopen(argv[1], "r")) {
        if(fout = fopen(argv[2], "w")) {
            while(getline(&buf, &nbytes, fin)>0) {
                p_end = buf + strlen(buf);
                tmp = buf;
                while(match(tmp, p_end, "//", &ptr)) {
                    tmp = ptr;
                    if((match(buf, tmp, "\"", &ptr)-match(tmp+2, p_end, "\\\"", &ptr))%2==0) {
                        strcpy(tmp, "\n\0");
                        if(buf + match(buf, tmp, " ", &ptr)
                            +match(buf, tmp, "\t", &ptr)==tmp)
                            *buf = 0;
                        break;
                    }
                    tmp += 2;
                }
                fprintf(fout, "%s", buf);
            }
            fclose(fin);
            fclose(fout);
        }
        else {
            printf("cannot create target file\n");
            fclose(fin);
            return 1;
        }
    }
    else {
        printf("source file does not exist ");
        return 1;
    }
    //test 1
    printf("//\"\\\"//last\n"); //last test
    return 0;
}

2011-03-27 12:45
快速回复:帮忙检查一下 去掉注释问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018780 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved