⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 text.cpp

📁 非常好用的五子棋游戏源码
💻 CPP
字号:
// Created:09-21-98
// By Jeff Connelly

// Text compression.  Uses a dictionary and replaces each word with a code.


#include "stdafx.h"
#define EXPORTING
#include "comprlib.h"
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
//#include <unistd.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>

// The limit of how many words in the dictionary.  This value is the maximum
// size of a 2-byte integer, 'short' on the compilers I have
#define WLIMIT 0xFFFF

FILE* source_file;
FILE* dest_file;

char* dictionary[WLIMIT];

// Reads a word (space or punctuation delimited)
char* read_word ()
{
    char c;
    int i = 0;
    static char* word;

    while (!feof(source_file))
    {
        c = getc(source_file);
        if (ispunct(c) || c == ' ')     // Delimter
        {
            word[i + 1] = NULL;      // Put NULL at end
            break;
        }
        word[i] = c;
        ++i;
    }
    return word;
}

// Searches for 'word' in the dictionary and returns true if found, else
// false
static bool in_dictionary (char* word)
{
    int i;

    // Loop until not end of dictionary
    for (i = 0; dictionary[i]; i++)
    {
        if (!stricmp(dictionary[i], word))   // Found word
            return true;
    }
    return false;
}

// Finds 'word' in the dictionary and returns the index (code)
static unsigned short get_code (char* word)
{
    int i;
    for (i = 0; dictionary[i]; i++)
    {
        if (!stricmp(dictionary[i], word))
            return (unsigned short)i;
    }
	return 0;
}

int main (int argc, char* argv[])
{
    char* word = NULL;
    int wordcount = 0;
    int i;
    short code;

    if (argc != 3)
        abort ();

    source_file = fopen(argv[1], "rt");  // Text mode
    dest_file = fopen(argv[2], "wb");

    if (!source_file || !dest_file)
    {
        printf ("Failed to open\n");
        exit (1);
    }

    printf ("Compressing...\n");

    // First build the dictionary with a list of all the words and there
    // codes
    while (!feof(source_file))
    {
        // Read a word.  If the word is not in the dictionary already then
        // add it.
        word = read_word ();
        if (!in_dictionary(word))       // New word
        {
            strcpy (dictionary[wordcount], word);
            ++wordcount;
        }
    }
    dictionary[wordcount + 1] = NULL;      // Put terminating NULL
    fseek (source_file, 0, SEEK_SET);

    // Write the dictionary backwards (faster?)
    i = wordcount;
    while (i--)
        fwrite (dictionary[i], 1, strlen(dictionary[i]), dest_file);

    // Followed by the compressed data
    while (!feof(source_file))
    {
        word = read_word();
        code = get_code (word);
        fwrite (&code, 1, 2, dest_file);
    }

    printf ("%s compressed as %s\n", argv[1], argv[2]);
    printf ("%f / %f", filelength(fileno(source_file)),
                       filelength(fileno(dest_file)));
    fclose (source_file);
    fclose (dest_file);
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -