wyxnogiveup 发表于 2022-7-29 11:59:51

带你学C语言的p2作业抄代码


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

#define MAX       256

long total;

int countLines(const char *filename);
void findAllCodes(const char *path);
void findALLFiles(const char *path);

int countLines(const char *filename)
{
        FILE *fp;
        int count = 0;
        int temp;
       
        if ((fp = fopen(filename, "r")) == NULL)
        {
                fprintf(stderr, "Can not open the file: %s\n", filename);
                return 0;
        }
       
        while ((temp = fgetc(fp)) != EOF)
        {
                if (temp == '\n')
                {
                        count++/n/;
                }
        }
       
        fclose(fp);
       
        return count;
}

void findAllCodes(const char *path)
{
        struct _finddata_t fa;
        long handle;
        char thePath, target;
       
        strcpy(thePath, path);
        if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
        {
                do
                {
                        sprintf(target, "%s/%s", path, fa.name);
                        total += countLines(target);
                }while (_findnext(handle, &fa) == 0);
        }
       
        _findclose(handle);
}

void findALLDirs(const char *path)
{
        struct _finddata_t fa;
        long handle;
        char thePath;
       
        strcpy(thePath, path);
        if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
        {
                fprintf(stderr, "The path %s is wrong!\n",path);
                return;
        }
       
        do
        {
                if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
                        continue;
               
                if( fa.attrib == _A_SUBDIR)
                {
                        sprintf(thePath, "%s/%s", path, fa.name);
                        findAllCodes(thePath);
                        findALLDirs(thePath);
                }   
        }while (_findnext(handle, &fa) == 0);
       
        _findclose(handle);
}

int main()
{
        char path = ".";
       
        printf("计算中...\n");
       
        findAllCodes(path);
        findALLDirs(path);
       
        printf("目前你总共写了 %ld 行代码! \n\n", total);
        system("pause");
       
        return 0;
p2作业抄代码,运行问题如下,求各位大佬指点,谢谢
32        4        C:\Users\lenovo\Desktop\c study\p2\Untitled5.cpp        stray '\243' in program
32        4        C:\Users\lenovo\Desktop\c study\p2\Untitled5.cpp        stray '\273' in program
C:\Users\lenovo\Desktop\c study\p2\Untitled5.cpp        In function 'int countLines(const char*)':
32        12        C:\Users\lenovo\Desktop\c study\p2\Untitled5.cpp        'n' was not declared in this scope
33        3        C:\Users\lenovo\Desktop\c study\p2\Untitled5.cpp        expected primary-expression before '}' token

wyxnogiveup 发表于 2022-7-29 12:05:09

我是发帖的萌新,第三十二行的/n/是后来加的,之前没有/n/也运行不了

临时号 发表于 2022-7-29 12:28:50

wyxnogiveup 发表于 2022-7-29 12:05
我是发帖的萌新,第三十二行的/n/是后来加的,之前没有/n/也运行不了

看我的注释
#include<io.h>
#include<direct.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX       256

long total;

int countLines(const char *filename);
void findAllCodes(const char *path);
void findALLFiles(const char *path);

int countLines(const char *filename)
{
      FILE *fp;
      int count = 0;
      int temp;
      
      if ((fp = fopen(filename, "r")) == NULL)
      {
                fprintf(stderr, "Can not open the file: %s\n", filename);
                return 0;
      }
      
      while ((temp = fgetc(fp)) != EOF)
      {
                if (temp == '\n')
                {
                        count++;// 这里/n/去掉,并将中文的;改称英文的;
                }
      }
      
      fclose(fp);
      
      return count;
}

void findAllCodes(const char *path)
{
      struct _finddata_t fa;
      long handle;
      char thePath, target;
      
      strcpy(thePath, path);
      if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
      {
                do
                {
                        sprintf(target, "%s/%s", path, fa.name);
                        total += countLines(target);
                }while (_findnext(handle, &fa) == 0);
      }
      
      _findclose(handle);
}

void findALLDirs(const char *path)
{
      struct _finddata_t fa;
      long handle;
      char thePath;
      
      strcpy(thePath, path);
      if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
      {
                fprintf(stderr, "The path %s is wrong!\n",path);
                return;
      }
      
      do
      {
                if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
                        continue;
               
                if( fa.attrib == _A_SUBDIR)
                {
                        sprintf(thePath, "%s/%s", path, fa.name);
                        findAllCodes(thePath);
                        findALLDirs(thePath);
                }   
      }while (_findnext(handle, &fa) == 0);
      
      _findclose(handle);
}

int main()
{
      char path = ".";
      
      printf("计算中...\n");
      
      findAllCodes(path);
      findALLDirs(path);
      
      printf("目前你总共写了 %ld 行代码! \n\n", total);
      system("pause");
      
      return 0;
}// 这里少个}

wyxnogiveup 发表于 2022-7-29 13:03:03

加了,但是还是不行

wyxnogiveup 发表于 2022-7-29 13:06:05

哦哦,好了,谢谢大佬!
页: [1]
查看完整版本: 带你学C语言的p2作业抄代码