图片查看器(c++)

第3关:图片查看器

挑战任务

参加“绿盟杯”竞赛的小明想要开发一个图片查看器,他想只显示文件夹下所有图片类型的文件。

你来帮小明实现这个功能吧。

编程要求

基本功能与第二题类似,编程实现对给定文件夹目录结构的展示,如果是文件夹则在其名字之前加上+--若是文件则加上--,上级目录与下级目录、下级文件用两个空格作为间隔,同级下依照文件夹、文件的首字母顺序依次打印;另外需要对文件进行过滤,只显示图片类型的文件,本关需要过滤的图片文件类型有:“jpg,png,bmp”,请补充完善右侧代码区中的showDirStructure(char *folderPath)函数实现本关要求的功能,其中函数参数含义如下:

  • folderPath:指定要显示的文件夹

测试说明

样例1

输入:src/step3/root

输出:

提示:

你可以通过如下链接下载本关涉及到的目录文件:

https://www.educoder.net/attachments/download/202620/step3Dir.zip


开始挑战吧,祝你成功!

/***************************
 * 函数功能: 遍历文件夹
 * return: void
 * @para folderPath: 文件夹路径
***************************/
void showDirStructure(char *folderPath)
{
	/********** BEGIN **********/
	static int floor=0;	//层数
	
	for(int i=0;i<floor*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))
		{
			floor+=1;
			showDirStructure(buf);
			floor-=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;
				}
				if(!strcmp(ext,"jpg")||!strcmp(ext,"png")||!strcmp(ext,"bmp")){
					for(int j=0;j<(floor+1)*2;j++)
						cout<<" ";
					cout<<"--"<<i->d_name<<endl;
				}
			}
		}
	}
	closedir(dir);
	/********** END **********/
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的 Windows 图片查看程序的代码示例: ```c #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { static TCHAR szAppName[] = TEXT("Image Viewer"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, TEXT("Image Viewer"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static HBITMAP hBitmap; static int cxClient, cyClient; HDC hdc, hdcMem; PAINTSTRUCT ps; RECT rect; switch (message) { case WM_CREATE: hBitmap = (HBITMAP)LoadImage(NULL, TEXT("image.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); if (hBitmap == NULL) { MessageBox(hwnd, TEXT("Could not load image.bmp!"), TEXT("Error"), MB_ICONERROR); return -1; } return 0; case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); return 0; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); hdcMem = CreateCompatibleDC(hdc); SelectObject(hdcMem, hBitmap); GetClientRect(hwnd, &rect); StretchBlt(hdc, 0, 0, cxClient, cyClient, hdcMem, 0, 0, 640, 480, SRCCOPY); DeleteDC(hdcMem); EndPaint(hwnd, &ps); return 0; case WM_DESTROY: DeleteObject(hBitmap); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } ``` 这个程序使用了 Windows API 来创建一个窗口,并在窗口中显示一张图片图片文件名为 "image.bmp",需要放在程序所在的目录下。您可以根据自己的需要修改代码来实现更多功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值