/*
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
*/
2015-3
最新推荐文章于 2023-06-04 14:34:18 发布