全国绿色计算大赛 模拟赛第一阶段 第3关:图片查看器

用第二关的代码修改即可,因为图片属于普通文件,而txt也是普通文件,单纯用stat无法区分,这时我们要提取后缀名进行判断,so easy,见过关代码:

void showDirStructure(char *folderPath)
{
    static int flor = 0;     //²ãÊý

    for (int i = 0; i < flor*2; i++) cout << " ";    //Êä³öǰÖÿոñ

    char buf[256];
    int len = 0;
    for (int i = strlen(folderPath)-1; folderPath[i] != '/'; i--) buf[len++] = folderPath[i];
    buf[len] = '\0';

    for (int i = 0; i < len/2; i++) {
        char t = buf[i];
        buf[i] = buf[len-1-i];
        buf[len-1-i] = t;
    }

    cout << "+--" << buf << endl;


    DIR *dir = opendir(folderPath);
    struct dirent *i = NULL;

    while ((i = readdir(dir)) != NULL) {

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

        strcpy(buf, folderPath);
        strcat(buf, "/");
        strcat(buf, i->d_name);

        struct stat M;
        stat(buf, &M);

        if (S_ISDIR(M.st_mode))
        {
            flor += 1;
            showDirStructure(buf);
            flor -= 1;
        }
        else
        {
            if (S_ISREG(M.st_mode)) {

                char ext[256];
                len = 0;
                for (int j = strlen(buf)-1; buf[j] != '.'; j--) ext[len++] = buf[j];
                ext[len] = '\0';


                for (int j = 0; j < len/2; j++) {
                    char t = ext[j];
                    ext[j] = ext[len-1-j];
                    ext[len-1-j] = t;
                }


                //cout << "test:" << ext << endl;


                if (!strcmp(ext, "jpg") || !strcmp(ext, "png") || !strcmp(ext, "bmp")) {

                    for (int j = 0; j < (flor+1)*2; j++) cout << " ";

                    cout << "--" << i->d_name << endl;
                }

            }
        }
    }

    closedir(dir);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水能zai舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值