PDA

View Full Version : جست و جوی یک کلمه در متن



im.r3za
جمعه 06 دی 1392, 03:45 صبح
سلام
دوستان من یه برنامه نوشتم که میخواستم یک کلمه رو توی یه متن جستوجو کنم که درست بود
اما حالا میخوام
1- اول یک متن از ورودی بخونم
2- یک کلمه هم از ورودی بخونم ( یعنی 2تا فایل .txt رو باز کنم و دومی رو توی اولی سرچ کنم)
3- اگه کلمه توی متن بود بهم یا نشونش بده یا بگه کجای متنه

میشه راهنماییم کنید ؟ بعدجور توی مخم رفته و الان توی فرجه ها هستیم و نمیتونم از اساتید کمک بگیرم

amirhossein.ha
جمعه 06 دی 1392, 23:28 عصر
اینم کدش اول ادرس فایل اول بعد ادرس فایل دوم توی فایل دوم هم کلمه مورد نظرتون برای سرچ کردن رو بزارید :

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
system("color 17");
char f1[1000],f2[1000] ;
cout << "enter the addres of first file : \n" ;
cin >> f1 ;
cout << "enter the addres of second file : \n" ;
cin >> f2 ;
fstream file(f1) ;
fstream file2(f2) ;
if(file.is_open())
{
string temp;
file2 >> temp;
while (!file.eof())
{
string temp2 ;
int a = file.tellg() ;
file >> temp2;
if(temp2==temp)
{
cout << "Your word find in the " << a + 2 << "th character of first file" << '\n' ;
break ;
}
}
file.close();
}
else
{
cout<<"can not open file";
}
return 0;
}


فقط این وقتی پیدا میکنه که فایل دوم فقط یک کلمه باشه و اون توی فایل اول هم به صورت یک کلمه اومده باشه اگه با جزپیات بیشتر میخواید باید کارکتر به کارکتر بخونید و مقایسه کنید .

amirhossein.ha
شنبه 07 دی 1392, 18:58 عصر
بیا این از قبلی خیلی بهتره هرجای فایل باشه میگه که کلمه ای مه توی فایل دومه از کجای فایل اول شروع میشه :

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
system("color 17");
char f1[1000],f2[1000] ;
cout << "enter the addres of first file : \n" ;
cin >> f1 ;
cout << "enter the addres of second file : \n" ;
cin >> f2 ;
fstream file1(f1) ;
fstream file2(f2) ;
string s;
char ch;
int g;
file2>>s;
int si=s.size();
while(!(file1.eof()))
{
g=file1.tellg();
file1>>ch;
if(ch==s[0])
{
for(int i=1;i<si;i++)
{
file1>>ch;
if(ch!=s[i])
{
goto st;
}
}
if(g%10==0)
cout<<"Your word find in the "<<g+1<<"st character of first file\n";
else if(g%10==1)
cout<<"Your word find in the "<<g+1<<"nd character of first file\n";
else if(g%10==2)
cout<<"Your word find in the "<<g+1<<"rd character of first file\n";
else
cout<<"Your word find in the "<<g+1<<"th character of first file\n";
return 0;
}
st :
file1.seekg(g+1);
}
cout << "Your word not found \n";
system("pause");
return 0;
}


ولی توی فایل دومت فقط باید یه کلمه باشه و اگه بیشتر باشه فقط کلمه اول رو پیدا میکنه