2015-3

/*
2020/4/17
第二遍
*/
//方法一:建立二叉搜索树
/*
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
using namespace std;

const int MAXWORD = 100;

typedef struct node
{
    char word[MAXWORD];
    node* lchild;
    node* rchild;
}Node,*Tree;

char getWord(char word[],int lim,FILE* in)
{
    char c;
    char *w = word;
    while(isspace(c = fgetc(in)))
        ;
    if(c != EOF)
        *w++ = c;
    if(!isalpha(c))
    {
        *w = '\0';
        return c;
    }
    for(;--lim;w++)
    {
        if( isalnum(c = fgetc(in)) || c == '_')
        {
            *w = c;
        }
        else
        {
            ungetc(c,in);
            break;
        }
    }
    *w = '\0';
    return w[0];
}

void insert(Node* &root,char word[])
{
    if(root == NULL)
    {
        root = (Node*)malloc(sizeof(Node));
        strcpy(root->word,word);
        root->lchild = NULL;
        root->rchild = NULL;
        return ;
    }

    int cmp = strcmp(root->word,word);
    if(cmp == 0)
    {
        return ;
    }
    else if(cmp > 0)
    {
        insert(root->lchild,word);
    }
    else
    {
        insert(root->rchild,word);
    }
    return ;
}

void preOrder(Node* root)
{
    if(root)
    {
        preOrder(root->lchild);
        printf("%s\n",root->word);
        preOrder(root->rchild);
    }
}

int main()
{
    FILE* fp;
    Node* root = NULL;
    fp = fopen("data.txt","r");
    char c;
    char word[MAXWORD];
    while( (c = getWord(word,MAXWORD,fp)) != EOF)
    {
        if( isalpha(word[0]))
        {
            //printf("%s\n",word);
            insert(root,word);

        }
    }
    preOrder(root);
    fclose(fp);
    return 0;
}
*/

//方法二使用set
#include<stdio.h>
#include<set>
#include<string.h>
#include<string>
#include<iostream>
using namespace std;

const int MAXWORD = 100;

char getWord(char word[],int lim,FILE* in)
{
    char c;
    char *w = word;
    while(isspace(c = fgetc(in)))
        ;
    if(c != EOF)
        *w++ = c;
    if(!isalpha(c))
    {
        *w = '\0';
        return c;
    }
    for(;--lim;w++)
    {
        if( isalnum(c = fgetc(in)) || c == '_')
        {
            *w = c;
        }
        else
        {
            ungetc(c,in);
            break;
        }
    }
    *w = '\0';
    return w[0];
}


int main()
{
    FILE* fp;
    set<string> s;
    char c;
    char word[MAXWORD];
    fp = fopen("data.txt","r");
    while( (c = getWord(word,MAXWORD,fp)) != EOF)
    {
        if( isalpha(word[0]))
        {
            s.insert(word);//调用string的构造函数,将字符串转化为string对象
        }
    }

    set<string>::iterator it;
    for(it = s.begin();it!=s.end();it++)
    {
        //printf("%s\n",(*it).c_str());//string->char*
        cout<<*it<<endl;
    }
    fclose(fp);
    return 0;
}

/*
IN:
Alcatel provides end-to-end solutions.
It enables enterprises to deliver content to any type of user.
lcatel operates in 130 countries.
Alcatel focus on optimizing their service offerings and revenue streams.

OUT:
Alcatel
It
and
any
content
countries
deliver
enables
end
enterprises
focus
in
lcatel
of
offerings
on
operates
optimizing
provides
revenue
service
solutions
streams
their
to
type
user
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值