1. 程式人生 > >第三次作業(WC擴充套件)

第三次作業(WC擴充套件)

(1)碼雲地址:
(2)參與者:201631062316,201631062216

WordCount作業思路

使用c語言編寫wc擴充套件功能,並進行相關測試

程式設計實現過程

程式碼說明

1.統計空行

int count_blankL(char*filename_counted)
{
    FILE*fp=fopen(filename_counted,"r");
    char buffer[100];
    int i=0;
    char c=0;
    char cl=0;
    int bufferLen;
    int charNum=0;
    int blankL=0;
    while(fgets(buffer,100,fp)!=NULL)
    {
        bufferLen=strlen(buffer);
        for(i=0;i<bufferLen;i++)
        {
            c=buffer[i];
            if(c!=' '&&c!='\t'&&c!='\n'&&c!='\r')
            {
                charNum++;
                cl=c;
            }
        }
        if(charNum==0)
        {
            blankL++;
        }
        if(charNum==1&&(cl=='{'||cl=='}'))
        {
            blankL++;
        }
    }
    fclose(fp);
    printf("%s, 空行數: %d\n",filename_counted,blankL);
    return blankL;
}

2.統計註釋行

int count_noteL(char*filename_counted)
{
    FILE*fp=fopen(filename_counted,"r");
    char buffer[100];
    int i=0;
    char c=0;
    char cl=0;
    int bufferLen;
    int noteL=0;
    int isLastNote=0;
    while(fgets(buffer,100,fp)!=NULL)
    {
        bufferLen=strlen(buffer);
        for(i=0;i<bufferLen;i++)
        {
            c=buffer[i];
            if(c=='/')
            {
                if(isLastNote==1)
                {
                    noteL++;
                }
                isLastNote=1;
            }
        }
        isLastNote=0;
    }
    fclose(fp);
    printf("%s, 註釋行: %d\n",filename_counted,noteL);
    return noteL;
}

3.遞迴遍歷檔案

int FileSearch(const char *dir)
{
    long handle;
    struct _finddata_t fileinfo;
    char dirNew[500];
    strcpy(dirNew, dir);
    strcat(dirNew,"\\*.*");
if ((handle = _findfirst(dirNew, &fileinfo)) == -1L)
{
    printf("Failed to findfrist file");
    return -1;
}
    while (_findnext(handle,&fileinfo)==0)
    {
        if (fileinfo.attrib & _A_SUBDIR)
        {
            if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)
                continue;
            strcpy(dirNew, dir);
            strcat(dirNew, "\\");
            strcat(dirNew, fileinfo.name);
            FileSearch(dirNew);
        }
        else
        {

            if (++i < Max&&strstr(fileinfo.name,".c"))//將路徑入棧
            {
                strcpy(data[i].path , dir);
                strcat(data[i].path, "\\");
                strcat(data[i].path, fileinfo.name);
                strcpy(data[i].filename_matched,fileinfo.name);
                data[i].size = fileinfo.size;
            }
        }
    }
    _findclose(handle);
}

4.對遍歷符合條件的檔案進行相關操作

void op_s(Data data[],char*filename_result)
{
    int totalC=0;
    int totalW=0;
    int totalL=0;
    printf("ok");
    while (i > -1)
    {
            printf("okin");
        totalC=countC(data[i].path);
        output(filename_result,data[i].filename_matched,"字元數",totalC);
        totalW=countW(data[i].path);
        output(filename_result,data[i].filename_matched,"單詞數",totalC);
        totalL=countL(data[i].path);
        output(filename_result,data[i].filename_matched,"總行數",totalC);
    i--;
    }
}

測試用例

1.

2.

3.

測試效果

總結

通過本次結對程式設計,暴露出自己很多問題,比如程式碼格式、命名不規範給結對程式設計的過程帶來很多麻煩,同時此次也是自己首次體驗結對程式設計,對這種模式有了更多的瞭解。

參考文獻

https://blog.csdn.net/gang9881/article/details/78504407
https://www.cnblogs.com/collectionne/p/6815924.html