创建一个a.txt 存放文本,
但是注意,由于时间较紧,所以对于标点符号的处理没有做。
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define N 100
struct a
{
char ch[20]
int m
int flag
}
void main()
{
struct a word[N]=
int k=0,j=0,i=0,count=1
FILE *fp
char c[20]
if((fp=fopen("a.txt","r"))!=NULL)
{
while(!(feof(fp)))
{
fscanf(fp,"%s",word[k].ch)
k++
}
for(i=0i<ki++)
{
if(word[i].flag!=1)
{
word[i].flag=1
word[i].m++
for(j=i+1j<kj++)
{
if((strcmp(word[i].ch,word[j].ch)==0)&&(word[j].flag!=1))
{
word[i].m++
word[j].flag=1
}
}
}
}
}
printf("文本统计结果如下: \n")
for(i=0(word[i].ch!=NULL)&&(i<N)i++)
{
if(word[i].m!=0)
{
printf("%3d. %s,出现了次数:%d次.\n",count,word[i].ch,word[i].m)
count++
}
}
getch()
}
///////////////////哇哈哈哈哈哈哈~~~~~ 你自己再在这个基础上修改一下就是了 基础功能已经帮你实现了#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
#include<stack>
#include<queue>
#include<utility>
#include <sstream>
#include<map>
using namespace std
chuangjian()
mima(int jk)
zidianlujing()
xuanzemoshi(map<string,string>pp,string name)
lianxu(map<string,string>pc,string dd)
dange(map<string,string>pa,string la)
tianjia(string jx)
gongneng()
int main()
{
cout<<endl
cout<<" 虾味单词搜索引擎 V1.2"<<endl
cout<<endl
cout<<" 虾味千层糕荣誉出品"<<endl
cout<<endl
cout<<" 本程序受虾味密码引擎保护"<<endl
cout<<endl
cout<<" QQ: 381426068"<<endl
cout<<" 435952144"<<endl
cout<<endl
int uh=0
mima(uh)
return 0
}
mima(int jk)
{
if(jk==3)
{
cout<<"已连续 3 次输入错误!"<<endl
cout<<endl
cout<<"禁止进入程序!"<<endl
cout<<endl
cout<<"终止程序!"<<endl
cout<<endl
system("pause")
return 0
}else{
cout<<endl
cout<<"退出请输入 end"<<endl
cout<<endl
cout<<"请输入程序开启密码:"<<endl
string pp
cin>>pp
cout<<"正在验证密码......."<<endl
if(pp=="1q2w3e")
{
cout<<endl
cout<<"密码正确!"<<endl
cout<<endl
cout<<"进入主程序...."<<endl
cout<<endl
cout<<endl
zidianlujing()
}else{
if(pp=="end")
{
return 0
}
++jk
cout<<endl
cout<<"密码错误!还剩下"<<3-jk<<"次机会"<<endl
cout<<endl
cout<<"重试? y or n"<<endl
string zxcv
cin>>zxcv
if(zxcv=="y")
{
system("cls")
mima(jk)
}else{
cout<<"终止程序!"<<endl
system("pause")
return 0
}
}
}
return 0
}
zidianlujing()
{
system("cls")
cout<<endl
cout<<"-------------------"<<endl
cout<<"1.打开字典"<<endl
cout<<"2.生成新字典"<<endl
cout<<"3.功能介绍"<<endl
cout<<"4.退出"<<endl
cout<<"-------------------"<<endl
cout<<"请选择:"<<endl
string la
cin>>la
if(la=="1"){
cout<<"请输入字典路径(默认为当前程序目录):"<<endl
string zidian
cin>>zidian
ifstream lk(zidian.c_str())
if(!lk)
{
cout<<endl
cout<<"找不到该文件!虾味千层糕QQ: 381426068"<<endl
cout<<endl
cout<<"请重新输入!"<<endl
cout<<endl
system("pause")
cout<<endl
cout<<"-------------------"<<endl
cout<<"1.生成新字典"<<endl
cout<<"2.返回菜单"<<endl
cout<<"3.退出程序"<<endl
cout<<"-------------------"<<endl
cout<<"请选择:"<<endl
string lpl
cin>>lpl
if(lpl=="1")
{
lk.close()
lk.clear()
cout<<endl
chuangjian()
}
if(lpl=="2")
{
lk.close()
lk.clear()
cout<<endl
zidianlujing()
}
if(lpl=="3")
{
lk.close()
lk.clear()
return 0
}
}else{
cout<<"已成功打开字典!"<<endl
cout<<endl
cout<<"正在读入字典资料......"<<endl
map<string,string>hello
string name,ziliao
while(lk>>name>>ziliao)
{
hello.insert(make_pair(name,ziliao))
}
cout<<"已读入完毕!"<<endl
lk.close()
lk.clear()
cout<<"已关闭程序流!"<<endl
cout<<endl
system("pause")
xuanzemoshi(hello,zidian)
}
}
if(la=="2")
{
chuangjian()
}
if(la=="3")
{
gongneng()
}
if(la=="4")
{
return 0
}
return 0
}
xuanzemoshi(map<string,string>pp,string name)
{
system("cls")
cout<<endl
cout<<"-----------------------"<<endl
cout<<"(退出请输入任意字母)"<<endl
cout<<endl
cout<<"1.单个查找模式"<<endl
cout<<"2.连续查找模式"<<endl
cout<<"3.添加单词模式"<<endl
cout<<"----------------------"<<endl
cout<<"请选择模式:"<<endl
int xuanze
cin>>xuanze
if(xuanze==1)
{
dange(pp,name)
}
if(xuanze==2)
{
lianxu(pp,name)
}
if(xuanze==3)
{
tianjia(name)
}
return 0
}
dange(map<string,string>pa,string la)
{
system("cls")
cout<<endl
cout<<"已成功进入单个查找模式!"<<endl
cout<<endl
cout<<"请输入要查找的单词:"<<endl
string word
cin>>word
map<string,string>::const_iterator lq=pa.find(word)//查找
if(lq==pa.end())
{
cout<<endl
cout<<"没有找到此单词!"<<endl
cout<<endl
system("pause")
}
if(lq!=pa.end())
{
cout<<endl
cout<<"要查找的目标单词: "<<lq->first<<endl
cout<<endl
cout<<"单词解释: "<<lq->second<<endl
cout<<endl
}
cout<<endl
cout<<"继续? y or n"<<endl
cout<<endl
cout<<"请选择:"<<endl
string qw
cin>>qw
if(qw=="y")
{
dange(pa,la)
}else{
cout<<endl
cout<<"-------------------"<<endl
cout<<"1.返回菜单"<<endl
cout<<"2.重新选择字典文件"<<endl
cout<<"3.添加单词到字典"<<endl
cout<<"4.退出程序"<<endl
cout<<"-------------------"<<endl
cout<<"请选择:"<<endl
string ad
cin>>ad
cout<<endl
if(ad=="1"|ad=="2"|ad=="3"|ad=="4")
{
if(ad=="1")
{
xuanzemoshi(pa,la)
}
if(ad=="2")
{
zidianlujing()
}
if(ad=="3")
{
int uj=tianjia(la)
if(uj==-1)
{
return 0
}
}
if(ad=="4")
{
return 0
}
}else{
cout<<endl
cout<<"没有该选项!"<<endl
cout<<endl
system("pause")
cout<<"返回菜单!"<<endl
cout<<endl
xuanzemoshi(pa,la)
return 0
}
}
return 0
}
lianxu(map<string,string>pc,string dd)
{
system("cls")
cout<<endl
cout<<"已成功进入连续查找模式!"<<endl
cout<<endl
cout<<"请输入要查找的单词(用空格隔开并以#结束输入):"<<endl
string yy//要查找的
cin>>yy
map<string,string>::const_iterator uu//存储结果
while(yy!="#")
{
uu=pc.find(yy)
if(uu!=pc.end()){
cout<<endl
cout<<"要查找的单词: "<<uu->first<<endl
cout<<"该单词资料: "<<uu->second<<endl
cout<<endl
}
if(uu==pc.end())
{
cout<<endl
cout<<"没有找到"<<yy<<"这个单词!虾味千层糕QQ: 435952144"<<endl
cout<<endl
}
cin>>yy
}
cout<<endl
cout<<"已查找完毕!"<<endl
cout<<endl
cout<<"继续? y or n"<<endl
string qw
cin>>qw
cout<<endl
if(qw=="y")
{
lianxu(pc,dd)
}else{
cout<<"-------------------"<<endl
cout<<"1.返回菜单"<<endl
cout<<"2.重新选择字典"<<endl
cout<<"3.添加单词到字典"<<endl
cout<<"4.退出程序"<<endl
cout<<"-------------------"<<endl
cout<<"请选择:"<<endl
string ad
cin>>ad
if(ad=="1"|ad=="2"|ad=="3"|ad=="4")
{
if(ad=="1")
{
xuanzemoshi(pc,dd)
}
if(ad=="2")
{
zidianlujing()
}
if(ad=="3")
{
int uu=tianjia(dd)
if(uu==-1)
{
return 0
}
}
if(ad=="4")
{
return 0
}
}else{
cout<<endl
cout<<"没有该选项!"<<endl
cout<<endl
system("pause")
cout<<endl
cout<<"返回菜单!"<<endl
cout<<endl
xuanzemoshi(pc,dd)
return 0
}
}
return 0
}
tianjia(string jx)
{
system("cls")
cout<<endl
cout<<"已成功进入单词添加模式!"<<endl
cout<<endl
cout<<"正在展开字典...."<<endl
ofstream qq(jx.c_str(),ios::out|ios::app|ios::ate)
cout<<"展开成功!"<<endl
if(!qq)
{
qq.close()
qq.clear()
cout<<"展开失败!"<<endl
cout<<endl
cout<<"要创建字典吗? y or n"<<endl
string oo
cin>>oo
if(oo=="y")
{
cout<<"请输入要创建的路径:"<<endl
string op
cin>>op
ofstream qe(op.c_str())
if(!qe)
{
cout<<endl
cout<<"创建失败!"<<endl
cout<<"返回菜单!"<<endl
cout<<endl
zidianlujing()
}
cout<<"已创建成功!"<<endl
qe.close()
qe.clear()
cout<<endl
cout<<"请重新输入路径!"<<endl
cout<<endl
tianjia(jx)
}
if(oo=="n")
{
qq.close()
qq.clear()
cout<<"1.重新输入要添加的字典的路径"<<endl
cout<<"2.返回菜单!"<<endl
cout<<endl
cout<<"请选择:"<<endl
string lp
cin>>lp
if(lp=="1")
{
tianjia(jx)
}
if(lp=="2")
{
cout<<endl
cout<<"返回程序开端!"<<endl
cout<<endl
main()
}
cout<<endl
cout<<"返回输入字典路径"<<endl
cout<<endl
zidianlujing()
}
cout<<endl
system("pause")
cout<<endl
cout<<"返回程序开端!"<<endl
cout<<endl
zidianlujing()
}
string word,ziliao
cout<<endl
cout<<"字典打开成功!"<<endl
cout<<endl
cout<<"允许开始添加!"<<endl
cout<<endl
cout<<endl
cout<<"注意:绝对不可以在单词或解释内使用空格和换行!"<<endl
cout<<endl
system("pause")
cout<<endl
cout<<"请输入要创建的单词:"<<endl
cin>>word
cout<<"请输入该单词解释:"<<endl
cin>>ziliao
cout<<"正在处理中...."<<endl
qq<<"\n"<<word<<" "<<ziliao
cout<<"已添加成功!"<<endl
cout<<endl
cout<<"继续添加? y or n"<<endl
string ty
cin>>ty
if(ty=="y")
{
tianjia(jx)
}
if(ty=="n")
{
cout<<"退出?否将返回程序开端 y or n"<<endl
string kl
cin>>kl
if(kl=="y")
{
qq.close()
qq.clear()
return -1
}
cout<<endl
system("pause")
cout<<endl
cout<<"返回程序开端!"<<endl
qq.close()
qq.clear()
cout<<"已成功关闭数据流!"<<endl
cout<<endl
cout<<endl
zidianlujing()
}
qq.close()
qq.clear()
return 0
}
chuangjian()
{
system("cls")
cout<<endl
cout<<"请输入要创建的路径(默认为当前程序目录):"<<endl
string op
cin>>op
ofstream qe(op.c_str())
if(!qe)
{
cout<<endl
cout<<"创建失败!"<<endl
cout<<endl
system("pause")
cout<<endl
cout<<"返回菜单!"<<endl
cout<<endl
zidianlujing()
}
cout<<"文件创建成功!"<<endl
cout<<endl
cout<<"正在生成字典原始框架....."<<endl
cout<<"已生成成功!"<<endl
cout<<endl
cout<<"正在为字典输入基础资料....."<<endl
qe<<"hi 你好"<<endl
qe<<"want 想"<<endl
qe<<"big 大"<<endl
qe<<"small 小"<<endl
qe<<"count 计算"<<endl
qe<<"find 找到"<<endl
qe<<"world 地球"<<endl
qe<<"green 绿"
cout<<"已输入完毕!"<<endl
cout<<endl
cout<<"字典生成成功!"<<endl
cout<<endl
system("pause")
cout<<endl
qe.close()
qe.clear()
cout<<"关闭该数据流!"<<endl
cout<<endl
cout<<"返回菜单!"<<endl
cout<<endl
zidianlujing()
return 0
}
gongneng()
{
system("cls")
cout<<endl
cout<<"!!!功能介绍!!!"<<endl
cout<<endl
cout<<"本程序由虾味千层糕制作"<<endl
cout<<endl
cout<<"程序名: 单词搜索引擎"<<endl
cout<<endl
cout<<"以字典为基础实现搜索功能,"<<endl
cout<<endl
cout<<"以输入的字符为索引对字典所有的资料进行搜索,"<<endl
cout<<endl
cout<<"对错误进行了综合处理也是此程序一大特色"<<endl
cout<<endl
cout<<"此处的字典应用了文本格式"<<endl
cout<<endl
cout<<"应用了两种截然不同的选择方式,"<<endl
cout<<endl
cout<<"包括用数字为索引和用单词名为索引"<<endl
cout<<endl
cout<<"搜索引擎拥有搜索速度高,精确度高的特点"<<endl
cout<<endl
cout<<"本程序自带了字典生成功能"<<endl
cout<<endl
cout<<"可是只能生成一些基础资料"<<endl
cout<<endl
cout<<"仅是用来展示字典需要的编写格式"<<endl
cout<<endl
cout<<"建议生成的文件名: 文件名.txt"<<endl
cout<<endl
cout<<"这样的格式可方便查看字典的内容"<<endl
cout<<endl
cout<<"这样可知道搜索引擎搜索出来的结果是否准确"<<endl
cout<<endl
cout<<"本程序暂不支持中文输入!"<<endl
cout<<endl
cout<<"介绍完毕"<<endl
cout<<endl
system("pause")
system("cls")
cout<<endl
cout<<"返回菜单!"<<endl
cout<<endl
cout<<endl
system("pause")
system("cls")
cout<<endl
zidianlujing()
return 0
}
//////////////////////////////////
完全原创,已经编译运行确认:创建一个a.txt 存放文本,
但是注意,由于时间较紧,所以对于标点符号的处理没有做。
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define N 100
struct a
{
char ch[20]
int m
int flag
}
void main()
{
struct a word[N]={NULL}
int k=0,j=0,i=0,count=1
FILE *fp
char c[20]
if((fp=fopen("a.txt","r"))!=NULL)
{
while(!(feof(fp)))
{
fscanf(fp,"%s",word[k].ch)
k++
}
for(i=0i<ki++)
{
if(word[i].flag!=1)
{
word[i].flag=1
word[i].m++
for(j=i+1j<kj++)
{
if((strcmp(word[i].ch,word[j].ch)==0)&&(word[j].flag!=1))
{
word[i].m++
word[j].flag=1
}
}
}
}
}
printf("文本统计结果如下: \n")
for(i=0(word[i].ch!=NULL)&&(i<N)i++)
{
if(word[i].m!=0)
{
printf("%3d. %s,出现了次数:%d次.\n",count,word[i].ch,word[i].m)
count++
}
}
getch()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)