有没有大佬帮我看看哪里不对
做的是基于词频的文件查找课设 以下是写的代码
函数实现文件:
#include <bits/stdc++.h>
using namespace std;
const int MOD=1e9+7;
const int bas=2333;
int gethash(string s) {
int len=s.size(),ret=0;
for(int i=0; i<len; i++) {
ret=(ret*bas%MOD+s[i]-'A')%MOD;
}
return ret;
}
string add(string a,string b)//只限两个非负整数相加
{
const int L=1e5;
string ans;
int na[L]={0},nb[L]={0};
int la=a.size(),lb=b.size();
for(int i=0;i<la;i++) na[la-1-i]=a[i]-'0';
for(int i=0;i<lb;i++) nb[lb-1-i]=b[i]-'0';
int lmax=la>lb?la:lb;
for(int i=0;i<lmax;i++) na[i]+=nb[i],na[i+1]+=na[i]/10,na[i]%=10;
if(na[lmax]) lmax++;
for(int i=lmax-1;i>=0;i--) ans+=na[i]+'0';
return ans;
}
string sub(string a,string b)//只限大的非负整数减小的非负整数
{
const int L=1e5;
string ans;
int na[L]={0},nb[L]={0};
int la=a.size(),lb=b.size();
for(int i=0;i<la;i++) na[la-1-i]=a[i]-'0';
for(int i=0;i<lb;i++) nb[lb-1-i]=b[i]-'0';
int lmax=la>lb?la:lb;
for(int i=0;i<lmax;i++)
{
na[i]-=nb[i];
if(na[i]<0) na[i]+=10,na[i+1]--;
}
while(!na[--lmax]&&lmax>0) ;lmax++;
for(int i=lmax-1;i>=0;i--) ans+=na[i]+'0';
return ans;
}
函数原型文件:
#include
using namespace std;
int cnt1,cnt2,tail;
string word1[10000],word2[10000];
string word[10000];
int arr[10000];
int gethash(string s);
主代码:
#include <bits/stdc++.h>
#include “myfunc.h” //函数实现文件
#include “file1.h” //函数原型文件
using namespace std;
typedef long long ll;
int main() {
string str1,str2; //两个文件名
cout<<"请输入两个文件名"<<endl;
cin>>str1>>str2;
ifstream p1;
ofstream p2;
p1.open(str1,ios::in); //打开文件1
p2.open("temp.txt",ios::out); //打开文件temp.txt
string temp;
string s;
while(!p1.eof()) { //读入第一个文件
getline(p1,s);
int len=s.size();
for(int i=0; i<len; i++) {
if((s[i]<'a' || s[i]>'z') && (s[i]<'A' || s[i]>'Z')) { //把标点去掉,暂时保存到temp文件
if(temp.size()) { //写入temp文件
p2<<temp;
p2<<" ";
}
temp="";
}
else temp+=s[i];
}
}
p1.close();
p2.close();
p1.open("temp.txt",ios::in); //写入第一个文件
p2.open(str1,ios::out);
getline(p1,s); //写入str1文件
p2<<s;
p1.close();
p2.close();
//第二个文件同理
p1.open(str2,ios::in);
p2.open("temp.txt",ios::out);
while(!p1.eof()) {
getline(p1,s);
int len=s.size();
for(int i=0; i<len; i++) {
if((s[i]<'a' || s[i]>'z') && (s[i]<'A' || s[i]>'Z')) { //第二个文件去掉标点
if(temp.size()) {
p2<<temp;
p2<<" ";
}
temp="";
}
else temp+=s[i];
}
}
p1.close();
p2.close();
p1.open("temp.txt",ios::in);
p2.open(str2,ios::out);
getline(p1,s);
p2<<s;
p1.close();
p2.close();
ifstream in1file;
ifstream in2file;
in1file.open(str1,ios::in);
in2file.open(str2,ios::in);
while(!in1file.eof()){ //把文件1的内容读入到word1数组中
in1file>>word1[++cnt1];
};
while(!in2file.eof()){ //把文件2的内容读入到word2数组中
in2file>>word2[++cnt2];
};
cout<<"-----------索引表-------------\n"; //打印索引表
cout<<"文件一"<<endl;
for(int i=1; i<=cnt1; i++) {
int h=gethash(word1[i]); //得到hash值
cout<<word1[i]<<" "<<h<<endl; //打印信息
arr[++tail]=h; //存入总单词
}
sort(arr+1,arr+1+tail);
cout<<"文件二"<<endl;
int js=0;
for(int i=1; i<=cnt2; i++)
{
int h=gethash(word2[i]);
cout<<word2[i]<<" "<<h<<endl;
int pos=lower_bound(arr+1,arr+1+tail,h)-arr; //二分找哈希值
if(arr[pos]==h) { //公共单词
js++; //计数
word[js]=word2[i]; //保存进去
}
}
cout<<fixed<<setprecision(2)<<"相似度: "<<js*1.0/(cnt1+cnt2-js)*100<<"%"<<endl; //打印相似度
cout<<"---------相同单词-----------"<<endl;
for(int i=1; i<=js; i++)
{
cout<<word[i]<<endl;
}
return 0;
}
放在同一个文件夹里,打开之后编译会出现错误