第2关:文件查看器
题目
按格式输出文件目录:
思路
flor记录输出空格数量,递归输出到没有文件夹
参考 :
代码参考:https://blog.csdn.net/dengkuomin/article/details/83187655
按顺序读取文件目录:https://www.jianshu.com/p/5db0f8e344a8
代码
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <string>
using namespace std;
int fileNameFilter(const struct dirent *cur) {
std::string str(cur->d_name);
if (str.size()) {
return 1;
}
return 0;
}
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]; //folderPath是完整的攀附路径,在此初步提取文件夹名
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; //将文件夹名称输出
struct dirent **namelist;
int n = scandir(folderPath, &namelist, fileNameFilter, alphasort); //按文件目录字典序读取文件名
//DIR *dir = opendir(folderPath);
struct dirent *i = NULL;
for(int k = 0; k < n; ++ k) //遍历每一个文件
{
i = namelist[k];
if (!strcmp(i->d_name, ".") || !strcmp(i->d_name, "..")) continue; //读取出的内容包含.或..将其跳过
strcpy(buf, folderPath);
strcat(buf, "/");
strcat(buf, i->d_name); //这3步string操作将完整的路径名称存放置buf中
struct stat M;
stat(buf, &M);
if (S_ISDIR(M.st_mode)) //判断文件类型是否为文件夹
{
flor += 1;
showDirStructure(buf);
flor -= 1; //这里运用到了回溯的思想
}
else
{
for (int i = 0; i < (flor+1)*2; i++) cout << " "; //若为文件则多输出两个空格然后输出文件名
cout << "--" << i->d_name << endl;
}
free(i);
}
free(namelist);
}
第3关:图片查看器
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
using namespace std;
/***************************
* 函数功能: 遍历文件夹
* return: void
* @para folderPath: 文件夹路径
***************************/
int fileNameFilter(const struct dirent *cur) {
std::string str(cur->d_name);
if (str.size()) {
return 1;
}
return 0;
}
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]; //folderPath是完整的攀附路径,在此初步提取文件夹名
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; //将文件夹名称输出
struct dirent **namelist;
int n = scandir(folderPath, &namelist, fileNameFilter, alphasort);
//DIR *dir = opendir(folderPath);
struct dirent *i = NULL;
for(int k = 0; k < n; ++ k)
{
i = namelist[k];
if (!strcmp(i->d_name, ".") || !strcmp(i->d_name, "..")) continue; //读取出的内容包含.或..将其跳过
strcpy(buf, folderPath);
strcat(buf, "/");
strcat(buf, i->d_name); //这3步string操作将完整的路径名称存放置buf中
struct stat M;
stat(buf, &M);
if (S_ISDIR(M.st_mode)) //判断文件类型是否为文件夹
{
flor += 1;
showDirStructure(buf);
flor -= 1; //这里运用到了回溯的思想
}
else
{
char clas[10];
int clen = 0;
for(int h = strlen(i->d_name) - 1; i->d_name[h] != '.'; --h) clas[clen++] = i->d_name[h];
clas[clen] = '\0';
if (!strcmp(clas, "gpj") || !strcmp(clas, "pmb") || !strcmp(clas, "gnp"))
{
for (int j = 0; j < (flor+1)*2; j++) cout << " "; //若为文件则多输出两个空格然后输出文件名
cout << "--" << i->d_name << endl;
}
}
free(i);
}
free(namelist);
}
第3关:渡口与船
代码
#include <iostream>
#include <queue>
using namespace std;
/***************************
* 函数功能: 计算渡口中停了多少艘船
* return: 渡口中停的艘船个数
* @para ferry: 二维的渡口
* @para m: 渡口的行数
* @para n: 渡口的列数
***************************/
struct Node
{
int x, y;
};
bool vis[1000][1000];
int Next[4][2] = {
{1, 0}, {-1, 0}, {0, -1}, {0, 1}
};
int countOfShips(char **ferry, int m, int n)
{
/********** BEGIN **********/
int ans = 0;
for(int i = 0; i < m; ++ i)
{
for(int j = 0; j < n; ++ j)
{
if(ferry[i][j] == '+')
{
++ans;
int x = i, y = j;
queue<Node> q;
Node be;
be.x = x, be.y = y;
q.push(be);
vis[x][y] = true;
ferry[x][y] = 'o';
while(!q.empty())
{
Node now = q.front();
q.pop();
for(int k = 0; k < 4; ++ k)
{
int tx = now.x + Next[k][0];
int ty = now.y + Next[k][1];
if(tx < 0 || tx >= m || ty < 0 || ty >= n || vis[tx][ty] || ferry[tx][ty] != '+')
continue;
Node nex;
nex.x = tx, nex.y = ty;
q.push(nex);
vis[tx][ty] = true;
ferry[tx][ty] = 'o';
}
}
}
}
}
return ans;
/********** END **********/
}