回 帖 发 新 帖 刷新版面

主题:求助:帮帮我!!!!!!!!

本来是个统计注释行(开头为//),空行和代码行的程序,乱编了一个,算法不对,无法运行。希望各位高手能告诉小弟如何改正,如有好的算法或帮忙编一个也万分感谢!!
(这是我编的,由于是初学,非常乱,不好意思)
#include<conio.h>
#include<stdio.h>
#include <string.h>
#include<stdlib.h>
char text[10000];
main()


{
int ex=0,space=0,l=0,t=0,other=0;
char filename[40];
int i=0;
file *fp;
printf("请输入路径:");
scanf(" %s",filename);
if((fp=fopen(filename,"r+"))==null)
{printf("文件不能打开!\n");
getch();
exit(0);}
while(feof(fp)==0)
{text[i]=fgetc(fp);i++;}
text[i-1]='\0';
fclose(fp);
i=0;
while (text[i]!='\0')
{
if (text[i]=='/'&&text[i+1]=='/') {ex++;{for(;text[i]!='\n';i++); i++;}}
else if (text[i]=='\n'&&text[i+1]=='\n') {space++;i=i+2;}
else {while (text[i]=='\n')
{if (text[i]==' ') {i++; l++; t++;}
else l++;break;}
if (t==l) {space++;t=0;l=0;}
else if (text[i]=='\n') {other++;{for (;text[i]!='\n';);i++;}t=0;l=0;}
else {for(;text[i]!='\n';i++);}

}}
printf ("%d,%d,%d",ex,space,other);
                   }

回复列表 (共5个回复)

沙发

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
char text[1000];
main()
{
  int ex=0,space=0,other=0;
  char filename[40];
  int i=0;
  file *fp;
  printf("请输入路径:");
  scanf(" %s",filename);
  if((fp=fopen(filename,"r+"))==NULL)
  {
     printf("文件不能打开!\n");
     getch();
     exit(0);
  }
  while(!feof(fp))
  {
     fgets(fp,1000,text);
     if (text[0]=='/' && text[1]='/')     ex++;
     else {
        i=0;
        while (text[i] !='\0' && text[i] ==' ') i++;
        if (text[i]='\0' )  space++;
        else other++;
     }
  }
  fclose(fp);
  printf ("%d,%d,%d",ex,space,other);
}
程序没有调试,你自己试试吧。

板凳

非常感谢您的解答,我在调试后发现程序并不能实现要求,是不是fgets不能写入换行符\n呢?如果这样,text就只有一行了,不如何修改?

3 楼

/*
测试时把本文件保存为xxx.c,然后输入xxx.c就可以看到结果了。
完成日期:2003。8。31
*/
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

char eatspace(FILE *fp)
/*吃掉行首的所有空格符,如果文件未到达末尾返回非空格符,否则返回0*/
{
    char text=0;
    while(!feof(fp)&&(text==' '||text=='\t'||text==0))
    {
       text=fgetc(fp);
    }
    if(feof(fp))
       return 0;
    else return text;
}

void eatline(FILE *fp)
/*吃掉代码行,遇到文件结束符或者换行符返回*/
{
    char text=0;
    while(!feof(fp)&&text!='\n')
    {
        text=fgetc(fp);
    }
}

main()
{
    char text;
    int commentline=0,spaceline=0,codeline=0;
    char filename[40];
    FILE *fp;
    printf("请输入路径:");
    scanf("%s",filename);
    if((fp=fopen(filename,"r+"))==NULL)
    {
        printf("文件不能打开!\n");
        getch();
        exit(0);
    }
    while(!feof(fp))
    {
        text=eatspace(fp);//吃掉行首的空格符,返回遇到的非空格符
        if(text=='\n')    //如果吃掉空格符后遇到的是\n,则表示当前行为空行
            spaceline++;
        else if(text=='/')//有可能是注释行或代码行
        {
            text=fgetc(fp);
            if(text=='/')//肯定是注释行
                commentline++;
            else         //代码行
                codeline++;
            eatline(fp);   //吃掉当前行
        }
        else             //代码行
        {
             codeline++;
             eatline(fp);  //吃掉当前行
        }
    }
    fclose(fp);
    printf ("commentline:%d,spaceline:%d,codeline:%d\n",commentline,spaceline,codeline);
    //如果在TC或者VC下编译,保留这行:
    getch();
    //如果在DEV C++下编译,保留这一行:
    //system("PAUSE");
}

4 楼

谢谢指教,我刚刚做成功了,我认为没有错!
#include <stdio.h>
main()
{char filename[40],text[10000],temp;
int i=0,space=0,other=0,temp_spa=1,ex=0,line=0;
FILE *fp;
printf("请输入路径:");
scanf(" %s",filename);
if((fp=fopen(filename,"r+"))==NULL)
{printf("文件不能打开!\n");
getch();
exit(0);}
while(feof(fp)==0)
{text[i]=fgetc(fp);i++;}
text[i-1]='\0';
fclose(fp);
temp=getchar();
i=0;
while (text[i]!='\0')
{if (text[i]=='\n')
{line++;i++;}
else i++;}
i=0;
scan:
if (text[i]=='\n') {i++;goto statistics;}
else if (text[i]=='\0') goto output;
else {i++;goto scan;}
statistics:   switch(text[i])
{case  '/'  :   {if (text[i+1]=='/') {ex++;goto scan;}else goto scan;}
case  '\n'  :   {space++;goto scan;}
case  ' '  :   {while (text[i]!='\n') {if (text[i]!=' '){temp_spa=0;break;}
                                           else i++;}
                  space=space+temp_spa; temp_spa=1;goto scan;}
default  :  goto scan;         }
output :
if (text[0]=='\n') space++;
other=(line-space-ex+1);
printf("注释行数:%d,空行数:%d,代码行数:%d\n",ex,space,other);
getch();
}


5 楼

你的程序是错的,
如果注释符号“//”没有在行首,而它前面都是空格或者制表符,你的程序会把它看作代码行。
你试试看?

我来回复

您尚未登录,请登录后再回复。点此登录或注册