月度归档:2015年09月

按文件名存储-哈希表

通过散列函数将数据值和它的存储位置联系起来。即通过精心地向表载入元素实现,从而提高访问速度。本例中采取的解决冲突的办法是建立一个链表,挂在这个位置的后面,所有散列函数值为这个位置的元素都添加到这个链表中,可以从头部插入也可以从尾部追加,甚至可以再这个位置后面再挂一个散列表。代码实现了将文件名按字母a-z分类,不区分大小写。先以数组存储各节点,当发生冲突后即将节点加入链表中。然后遍历显示所有的文件名。

/* a2z.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define HASHSIZE 26
#define FILENAMELENGTH 20
#define TRUE 1
#define FALSE 0

struct file {
        char name[FILENAMELENGTH];
        struct file *next;
};

struct file * files[HASHSIZE];

char case_insensitive(char ch)
{
        if (isupper(ch))
                return ch - 'A' + 'a';
        return ch;
}

void init(void)
{
        int i;
        for (i = 0; i < HASHSIZE; i++)
                files[i] = NULL;
}

int hash(char *s)
{
        return case_insensitive(s[0]) - 'a';
}

int search(char *s)
{
        int num = hash(s);
        if (files[num] != NULL) {
                struct file *p = files[num];
                while (p != NULL) {
                        if (strcmp(p->name, s) == 0)
                                return TRUE;
                        p = p->next;
                }
        }

        return FALSE;
}

void insert(char *s)
{
        if (search(s) == FALSE) {
                int num = hash(s);
                struct file *new_node = malloc(sizeof (struct file));
                strcpy(new_node->name, s);
                if (files[num] == NULL) {
                        files[num] = new_node;
                        files[num]->next = NULL;
                } else {
                        new_node->next = files[num];
                        files[num] = new_node;
                }
        }

        return;
}

void free_node(struct file *p)
{
        free(p);

        return;
}


void del_node(char *s)
{
        if (search(s) == TRUE) {
                struct file *p = files[hash(s)], *q;
                if (strcmp(p->name, s) == 0) {
                        q = p;
                        files[hash(s)] = p->next;
                        free_node(q);
                } else {
                        while (p != NULL) {
                                q = p;
                                p = p->next;
                                if (strcmp(p->name, s) == 0)
                                        free_node(q);
                        }
                }
        }

        return;
}


void del(int num)
{
        struct file *p =files[num], *q;
        while (p != NULL) {
                q = p;
                p = p->next;
                free(q);
        }

        return;
}

void destroy(void)
{
        int i;

        for (i = 0; i < HASHSIZE; i++)
                del(i);

        return;
}

void show(int num)
{
        struct file *p = files[num];
        if (p != NULL) {
                printf("For file begins with '%c': \n", num + 'a');
                while (p != NULL) {
                        printf("%s\n", p->name);
                        p = p->next;
                }
                printf("\n");
        }



}

void show_all(void)
{
        int i;
        for (i = 0; i < HASHSIZE; i++)
                show(i);

        return;
}

int main(void)
{
        char * file_name[] = {
                "apple", "because", "song", "Dan", "discuz", "cartoon",
                "nobody", "android", "information", "love", "like", "No",
                "nothing", "like", "alone", "nothing", "sizeof", "Yes",
        };

        int len = sizeof file_name / sizeof (char *);
        int i;
        for (i = 0; i < len; i++)
                insert(file_name[i]);
        show_all();

        printf("---------------------------------\n");
        del_node("like");
        show_all();
        destroy();

        return 0;
}

继续阅读

printf 打印颜色

引言: 控制字符的通用格式如下:

Esc[{attr1};…;{attrn}m
其中: Esc 是转义字符, 其值为”\033″;
[ 是常量的左中括号;
{attr1};…{attrn} 是若干属性, 通常是由一个有特定意义的数字代替, 每个属性之间用分号分隔;
m 就是字面常量字符m, 结束标志

属性列表如下:
>>1. 通用格式控制:
0 重置所有属性
1 高亮/加粗
2 暗淡
4 下划线
5 闪烁
7 反转
8 隐藏

>>2. 前景色:
30 黑色
31 红色
32 绿色
33 黄色
34 蓝色
35 品红
36 青色
37 白色

>>3. 背景色:
40 黑色
41 红色
42 绿色
43 黄色
44 蓝色
45 品红
46 青色
47 白色
头文件1:

/* font color */
#define NONE            "\e[0m"         /* default */
#define BLACK           "\e[0;30m"      /* black */
#define L_BLACK         "\e[1;30m"      /* light black */
#define RED             "\e[0;31m"      /* red */
#define L_RED           "\e[1;31m"      /* light red */
#define GREEN           "\e[0;32m"      /* green */
#define L_GREEN         "\e[1;32m"      /* light green */
#define BROWN           "\e[0;33m"      /* brown */
#define YELLOW          "\e[1;33m"      /* yellow */
#define BLUE            "\e[0;34m"      /* blue */
#define L_BLUE          "\e[1;34m"      /* light blue */
#define PURPLE          "\e[0;35m"      /* purple */
#define L_PURPLE        "\e[1;35m"      /* light purple */
#define CYAN            "\e[0;36m"      /* cyan */
#define L_CYAN          "\e[1;36m"      /* light_cyan */
#define GRAY            "\e[0;37m"      /* gray */
#define WHITE           "\e[1;37m"      /* white */

/* background color*/
#define BG_BLACK        "\e[40m"        /* black background */
#define BG_RED          "\e[41m"        /* red background */
#define BG_GREEN        "\e[42m"        /* green background */
#define BG_YELLOW       "\e[43m"        /* yellow background */
#define BG_BLUE         "\e[44m"        /* blue background */
#define BG_CYAN         "\e[45"         /* cyan background */
#define BG_L_CYAN       "\e[46"         /* light cyan background */
#define BG_WHITE        "\e[47"         /* white background */

/* font attribure */
#define BOLD            "\e[1m"         /* blod/light */
#define UNDERLINE       "\e[4m"         /* underline */
#define BLINK           "\e[5m"         /* blink */
#define REVERSE         "\e[7m"         /* reverse */
#define HIDE            "\e[8m"         /* hide */
#define CLEAR           "\e[2J"         /* clear */
#define CLRLINE         "\r\e[K"        //or "\e[1K\r clear line "

头文件2:

/* cprint.h
 * color print
 */

/* font attribute */
#define NONE            "\e[0m"         /* defaults */
#define BOLD            "\e[1m"         /* blod */
#define H_BRIGHT        "\e[2m"         /* half-bright */
#define UNDERLINE       "\e[4m"         /* underscore */
#define BLINK           "\e[5m"         /* blink */
#define REVERSE         "\e[7m"         /* reverse video */
#define HIDE            "\e[8m"         /* hide */
#define DOUBLE_UNDERLINE "\e[21m"       /* doubly underline */
#define INTERSITY       "\e[22m"        /* intensity */
#define UNDERLINE_OFF   "\e[24m"        /* underline off */
#define BLINK_OFF       "\e[25m"        /* blink off */
#define REVERSE_OFF     "\e[27m"        /* reverse video off */
#define CLEAR           "\e[2j"         /* clear */
#define CLRLINE         "\r\e[k"        /* or "\e[1k[r clear line" */

/* foreground color */
#define BLACK           "\e[30m"        /* black foreground */
#define RED             "\e[31m"        /* red foreground */
#define GREEN           "\e[32m"        /* green foreground */
#define BROWN           "\e[33m"        /* brown foreground */
#define YELLOW          "\e[33m"
#define BLUE            "\e[34m"        /* blue foreground */
#define MAGENTA         "\e[35m"        /* magenta foreground */
#define CYAN            "\e[36m"        /* cyan foreground */
#define WHITE           "\e[37m"        /* white foreground */
#define COLOR           "\e[39m"        /* default foreground */

/* background color*/
#define BG_BLACK        "\e[40m"        /* black background */
#define BG_RED          "\e[41m"        /* red background */
#define BG_GREEN        "\e[42m"        /* green background */
#define BG_BROWN        "\e[43m"        /* brown background */
#define BG_YELLOW       "\e[43m"
#define BG_BLUE         "\e[44m"        /* blue background */
#define BG_MAGENTA      "\e[45m"        /* magenta background */
#define BG_CYAN         "\e[46m"        /* cyan background */
#define BG_WHITE        "\e[47m"        /* white background */
#define BG_COLOR        "\e[49m"        /* defaulte background */

正文, 如何使用printf函数在控制台彩色打印

转义序列以控制字符’ESC’开头。该字符的ASCII码十进制表示为27,十六进制表示为0x1B,八进制表示为033。多数转义序列超过两个字符,故通常以’ESC’和左括号'[‘开头。该起始序列称为控制序列引导符(CSI,Control Sequence Intro),通常由’\033[‘或’\e[‘代替。
继续阅读