xiaojiaxiang 发表于 2020-8-9 15:27:31

我来看答案了

子超爱学习 发表于 2020-8-9 21:01:38

#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>

#define MAX 256

long total;

int countlines(const char *filename);
int isCode(const char *filename);
void findALLDirs(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++;
            }
    }

    fclose(fp);

    return count;
}

int isCode(const char *filename)
{
       int length;

       length = strlen(filename);

       if (!strcmp(filename + (length - 2), ".c"))
       {
            return 1;
       }
       else
       {
         return 0;
       }
}

void findALLDirs(const char *path)
{
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;

    if ((dp = opendir(path)) == NULL)
    {
      fprintf(stderr, "The path %s is wrong!\n",path);
      return;
    }

    chdir(path);
    while ((entry = readdir(dp)) != NULL)
    {
            lstat(entry->d_name, &statbuf);

            if(!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
                continue;

            if (S_ISDIR(statbuf.st_mode))
            {
                   findALLDirs(entry->d_name);
            }
            else
            {
                   if (isCode(entry->d_name))
                   {
                         total += countlines(entry->d_name);
                   }
            }
    }

    chdir("..");
    clearerr(dp);
}

int main()
{
       char path = ".";

       printf("计算中...\n");

       findALLDirs(path);

       printf("目前你总共写了 %ld 行代码!\n\n", total);

       return 0;
}

CaseyUp 发表于 2020-8-9 21:32:13

初学者报道!!!!

爱学习的熊二 发表于 2020-8-9 21:34:35

瞧瞧

姜有进 发表于 2020-8-9 21:54:32

Krina 发表于 2020-8-9 22:53:32


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

#define MAX         256

long total;

int countLines(const char *fukename);
void findAllCodes(const char *path);
void findAllFiles(const char *path);

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

void findAllCodes(const char *path)
{
        struct _finddata_t fa;
        long handle;
        char thePath, target;
       
    strcpy(thePath, path);
        if((handle = _findfirst(strcat(the Path, "/*.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("¼ÆËãÖD...\n");
       
        findAllCodes(path);
        findALLDirs(path);
       
        printf("ĿǰÄã×ü12D′áË %ld DD′úÂë! \n\n", total);
        system("pause");
       
        return 0;       
}

雅莉雅莉雅 发表于 2020-8-10 00:09:08

感謝小甲魚

kingjinjin 发表于 2020-8-10 01:04:54

好人一生平安

赵小莫是只猫 发表于 2020-8-10 09:05:16

{:5_109:}

什么名字好呀 发表于 2020-8-10 09:10:32

lail

415774036 发表于 2020-8-10 09:49:15

s

热心市民杨先生 发表于 2020-8-10 10:14:50

感谢小甲鱼!!!

皓晨sama 发表于 2020-8-10 10:46:57

那个代码真的敲得我头秃

茶花ww 发表于 2020-8-10 10:47:02

好难

jiaweizhuzzz 发表于 2020-8-10 11:00:34

huifu

951024 发表于 2020-8-10 14:22:01

9999999

1836566538 发表于 2020-8-10 15:39:43

看答案先

珙白 发表于 2020-8-10 15:46:26

完成

icemanliu 发表于 2020-8-10 16:20:11

0.因为计算机只认识0和1两个数
1.汇编语言
2.编译
3.执行过程不一样,编译型语言经过编译后直接执行编译得到的汇编代码;而解释型语言由解释器翻译执行;所以编译型语言的效率更高
4.不能
5.解释型语言跨平台需要预装解释器
6.摩斯密码是通过长短信号相当于0和1的排列组合定义出字符集,通信过程中先翻译为代码组合再截石位字符
7.QIN MEN CHEN DI REN CHI FAN SHI FA DONG JIN GONG

vectorfunction 发表于 2020-8-10 16:55:49

1.机器语言
2.编译
页: 795 796 797 798 799 800 801 802 803 804 [805] 806 807 808 809 810 811 812 813 814
查看完整版本: S1E2:第一个程序 | 课后测试题及答案