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

📄 tvguid19.cpp

📁 borland c++3.1的压缩文件
💻 CPP
字号:
/*---------------------------------------------------------*/
/*                                                         */
/*   Turbo Vision 1.0                                      */
/*   TVGUID19.CPP Demo Source File                         */
/*   Copyright (c) 1991 by Borland International           */
/*                                                         */
/*---------------------------------------------------------*/

/*
   Read a line and add each unique word to a sorted
   string collection. Use the forEach iterator to
   traverse the collection and print out each word.
*/

#include <stdio.h>              // temp for gets
#include <stdlib.h>             // for exit()
#include <iostream.h>           // for << etc
#include <string.h>             // for strlen etc
#include <ctype.h>              // for ispunct

#define Uses_TStringCollection
#include <tv.h>

const int maxLine = 81;
char line[maxLine];

class TWordCollection : public TStringCollection
{

public:

    TWordCollection( short aLimit, short aDelta ) :
       TStringCollection( aLimit, aDelta) {}
    virtual void print();
};

/* Iterator */
static void printWord( void *w, void * )
{
    char *s = (char *)w;
    cout << s << endl;
}

void TWordCollection::print()
{
    forEach( &printWord, 0 );
}

// Enter a line of text, extract words and insert them in coll
static void insertWord( char *line, TStringCollection *sc )
{
    char *curr;
    char *first;

    char wstr[maxLine];
    char ch = EOS;
    Boolean inword;

    curr = first = line;
    inword = False;

    while( (ch = *curr) != EOS )
        {
        if( !isalnum( ch ) && inword )
            {
            inword = False;
            strncpy( wstr, first, size_t(curr - first) );
            wstr[size_t(curr-first)]=EOS;
            sc->insert( newStr( wstr ) );
            }
        else if( isalnum( ch ) && !inword )
            {
            inword = True;
            first = curr;
            }
        curr++;
        }

    if( inword )
        // grab last word if final char before null is alfa-num
        {
        strncpy( wstr, first, size_t(curr - first) );
        wstr[size_t(curr-first)]=EOS;
        sc->insert( newStr( wstr ));
        }
}

int main()
{
    TWordCollection *wordCollection = new TWordCollection( 20, 5 );
    // in production code, check lowMemory here

    cout << "Enter a line of text: " << endl;
    cin.get( line, maxLine, '\n' );

    // parse line, insert unique words into sorted collection
    insertWord( line, wordCollection );
    if( wordCollection->getCount() > 0 )
        {
        wordCollection->print();
        cout << "Total word count = " << wordCollection->getCount() << endl;
        }
    else
        cout << "No words in WordCollection!" << endl;

    return 0;
}

⌨️ 快捷键说明

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