c语言实现tree的功能

写了个函数实现tree的功能,把所有的目录一目了然的呈现在屏幕上,不用一个个的去点击查看。
之前误传的是测试的代码,现在这个略有改动。
头文件还是常用的那几个

#ifndef MYIOHEAD_H_
#define MYIOHEAD_H_
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <stdbool.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <dirent.h>
#include <linux/input.h>
// #include "kernel_list.h"
#include <termio.h>
#include <bits/signum.h>
#include <signal.h>
#include <semaphore.h>

#endif

下面是主代码,逻辑和变量搞清楚就很简单。
此处和上一篇博客差不多,暂时就不多写注释了(晚点可能会写),重点是变量和逻辑要搞清楚。

#include "myiohead.h"
int dirnum = 1;
int filenum = 0;
int theone = 0;

#define GREEN                "\e[0;32m" //give you some green collor see see
#define NONE                  "\e[0m"  //don't give you collor

//读取目录数目
int  dir_num(char *mydir)
{
	char allpath[1000];
	char otherpath[1000];
	char * dirpath = mydir;
	DIR * dir = opendir(dirpath);
	if (NULL == dir)
	{
		perror("打开目录失败!");
		exit(0);
	}
	
	struct dirent * rd = NULL;
	static int mystatic = -1;
	mystatic++;
	while((rd = readdir(dir)) != NULL) 
	{ 
		if(strncmp(rd->d_name,".",1)==0)
			continue;
		//printf("d_name : %s\n", rd->d_name); 
		// int a = mystatic;
		if (rd->d_type == DT_REG)
	 	{	
	 		// 获得文件的绝对路径
			bzero(allpath,1000);
			sprintf(allpath,"%s/%s",dirpath,rd->d_name);
			// printf("当前读取的文件名字是:%s\n",allpath);
			
			filenum++;

	 		//printf("这是普通文件!\n");
	 	}

	 	if (rd->d_type == DT_DIR)
	 	{
	 		//递归调用自己,接着读取子目录
			bzero(otherpath,1000);
			sprintf(otherpath,"%s/%s",dirpath,rd->d_name);
			dirnum++;
			dir_num(otherpath);
	 		// printf("这是目录!\n");
	 	}
	 	
	}
	return 0;
}

void read_dir(char *mydir)
{
	char allpath[1000];
	char otherpath[1000];
	char * dirpath = mydir;
	DIR * dir = opendir(dirpath);
	if (NULL == dir)
	{
		perror("打开目录失败!");
		exit(0);
	}
	
	struct dirent * rd = NULL;
	static int mystatic = 0;
	while((rd = readdir(dir)) != NULL) 
	{ 

		if(strncmp(rd->d_name,".",1)==0)
			continue;
		//printf("d_name : %s\n", rd->d_name); 
		// int a = mystatic;
		if (rd->d_type == DT_REG)
	 	{	
	 		// 获得文件的绝对路径
			bzero(allpath,1000);
			sprintf(allpath,"%s/%s",dirpath,rd->d_name);
			// printf("当前读取的文件名字是:%s\n",allpath);
			if (mystatic != 0)
			{
				printf(NONE"|");
			}
			
	 		for (int i = 0; i < mystatic; i++)
			{
				printf(NONE"   ");
			}
			printf(NONE"|_");
			printf(NONE"%s\n" ,rd->d_name);

	 		//printf("这是普通文件!\n");
	 		// if (strstr(rd->d_name,".bmp") != 0)
	 		// {
	 		// 	insert_(rd->d_name,head);
	 		// }
	 	}

	 	if (rd->d_type == DT_DIR)
	 	{
	 		//递归调用自己,接着读取子目录
			bzero(otherpath,1000);
			sprintf(otherpath,"%s/%s",dirpath,rd->d_name);
			mystatic++;
			if (mystatic>0)
			{
				printf(NONE"|");
			}
			for (int i = 0; i < mystatic-1; i++)
			{
				printf(GREEN"——————");
			}
			printf(GREEN"|_");
			printf(GREEN"%s\n" ,rd->d_name);
			
			read_dir(otherpath);
			mystatic--;
	 		// printf("这是目录!\n");
	 	}
	 	
	}

}

int main(int argc, char  *argv[])
{
	if (argc !=2)
	{
		perror("用法错误,正确用法:./a.out 目录名");
		exit(0);
	}
	printf(GREEN"%s\n", argv[1]);
	dir_num(argv[1]);
	read_dir(argv[1]);
	printf("目录数目:%d   文件数目:%d\n",dirnum,filenum);

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是用C语言实现构建Huffman树算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 1000 // 定义Huffman树节点结构体 typedef struct { int weight; // 权值 int parent; // 父节点下标 int left; // 左子节点下标 int right; // 右子节点下标 } HuffNode, *HuffTree; // 定义Huffman编码结构体 typedef struct { char ch; // 字符 char code[MAX_SIZE]; // 编码 } HuffCode, *HuffCodeTable; // 选择权值最小的两个节点 void select(HuffTree tree, int n, int *s1, int *s2) { int i; int min1, min2; min1 = min2 = MAX_SIZE; // 找出第一个权值最小的节点 for (i = 0; i < n; i++) { if (tree[i].weight < min1 && tree[i].parent == -1) { min1 = tree[i].weight; *s1 = i; } } // 找出第二个权值最小的节点 for (i = 0; i < n; i++) { if (tree[i].weight < min2 && tree[i].parent == -1 && i != *s1) { min2 = tree[i].weight; *s2 = i; } } } // 构建Huffman树 void buildHuffTree(HuffTree tree, int n) { if (n <= 1) return; int i, s1, s2; for (i = 0; i < 2 * n - 1; i++) { tree[i].parent = -1; tree[i].left = -1; tree[i].right = -1; } // 构建n-1个节点的Huffman树 for (i = n; i < 2 * n - 1; i++) { select(tree, i, &s1, &s2); tree[s1].parent = i; tree[s2].parent = i; tree[i].left = s1; tree[i].right = s2; tree[i].weight = tree[s1].weight + tree[s2].weight; } } // 获取Huffman编码 void getHuffCode(HuffTree tree, HuffCodeTable table, int n) { int i, j; char *code = (char *)malloc(n * sizeof(char)); code[n - 1] = '\0'; for (i = 0; i < n; i++) { int start = n - 1; int c = i; int p = tree[c].parent; while (p != -1) { if (tree[p].left == c) { code[--start] = '0'; } else { code[--start] = '1'; } c = p; p = tree[c].parent; } strcpy(table[i].code, &code[start]); } free(code); } int main() { int n; printf("输入字符个数:"); scanf("%d", &n); // 输入字符及其权值 HuffTree tree = (HuffTree)malloc(sizeof(HuffNode) * (2 * n - 1)); HuffCodeTable table = (HuffCodeTable)malloc(sizeof(HuffCode) * n); int i; for (i = 0; i < n; i++) { printf("输入第%d个字符及其权值:", i + 1); scanf("%s %d", &table[i].ch, &tree[i].weight); } // 构建Huffman树 buildHuffTree(tree, n); // 获取Huffman编码 getHuffCode(tree, table, n); // 输出Huffman编码 printf("Huffman编码如下:\n"); for (i = 0; i < n; i++) { printf("%c: %s\n", table[i].ch, table[i].code); } free(tree); free(table); return 0; } ``` 这段代码实现了从用户输入字符及其权值,构建Huffman树,获取Huffman编码,输出Huffman编码的功能。在构建Huffman树时,采用的是贪心策略,每次选择权值最小的两个节点进行合并。在获取Huffman编码时,采用的是自底向上的遍历方式,从叶子节点到根节点依次获取每个字符的编码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值