Help on search implementation

Jan 16, 2012 at 4:50pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* 
 * File:   main.cpp
 * Author: black4
 *
 * Created on January 13, 2012, 10:04 PM
 */
#include <map>
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <cstring>
#include <sstream>
#include <vector>
#include <iomanip>
using namespace std;

int main () 
{
    int number;
  {
    string data;
    string text="file.txt";
    int position=0;
    ifstream getData (text.c_str());
    if (getData.is_open())
    {
        while (getData.good())
        {
            string result;
            while(getline(getData, result))
            {data+=result;}
        }
                 getData.close();

         position=data.find("[20]")+4;
         data.erase(data.begin(),data.begin()+position);
         position=data.find("* [40]")+7;
         data.erase(data.begin()+position,data.end());
         
         for(int n=19;n<43;n+=3)
         {
         stringstream intToString;
         string name;
         string date;
         string price;
         string line,tmp;
         int newposition=0; 
         intToString<<n;
         position=data.find("["+intToString.str()+"]");
         line=data.substr(0,position);
         
         //cout<<line<<endl;
         newposition=line.find("in [");
         name=line.substr(4,newposition-99);
         date=line.substr(newposition-12,12);
         cout<<name<<endl<<endl;
         cout<<date<<endl<<endl;
         
         int money;
         money=line.find("S$");
         price=line.substr(money,line.length());
         cout<<price<<endl<<endl;
                          
         data.erase(data.begin(),data.begin()+position);
         
         //searcher
    if(!getData.eof())     
    {
    multimap<string,int> words;
    map<int,string> lines;
    getData.open("file.txt");
        while(getline(getData,data))
         {
             int i=1;
             istringstream in(data);
             string s;
             while(in>>s)
                 {
                words.insert(make_pair(s,i));
                }
             lines.insert(make_pair(i,data));
               i++;
         }
string search;
cout<<"\nEnter a word to search: ";
cin>>search;
cout<<"\nThe number of matches = "<<words.count(search)<<'\n';
multimap<string,int>::iterator it1=words.lower_bound(search);
multimap<string,int>::iterator it2=words.upper_bound(search);
while(it1!=it2)
{
int x=it1->second;
map<int,string>::iterator iter=lines.find(x);
cout<<'\n'<<x<<" ) "<<iter->second<<'\n';
it1++;
while(true)
{
if(it1!=it2 && it1->second==x)
{
it1++;

}
else
{
break;
}
}
}
cout<<'\n';
    }
         
         //searcher
         }
      
    }
    else
    {
    cout<<"Database not found.";
    }
    cin>>number;
    }
  }
    


Hi guys sorry I am still pretty new to c++, I would like to implement this similar search code(below) into my current code as above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<map>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdlib.h>
#include <cstring>
#include <sstream>

using namespace std;
using std::istringstream;
using std::ifstream;
using std::string;
using std::cout;
using std::cin;
using std::cerr;
using std::map;
using std::multimap;

int main()
{

//
       
multimap<string,int> words;
map<int,string> lines;
string str;
int o = system("lynx -dump \"input website here\" > file.txt");
ifstream Myfile;
Myfile.open("file.txt"); //create file for input
if(Myfile.fail())
{
cerr<<"\nThe file could not be opened.";
return -1;
}
int i=1;
while(getline(Myfile,str))
{
istringstream in(str);
string s;
while(in>>s)
{
words.insert(make_pair(s,i));
}
lines.insert(make_pair(i,str));
i++;
}
string search;
cout<<"\nEnter a word to search: ";
cin>>search;
cout<<"\nThe number of matches = "<<words.count(search)<<'\n';
multimap<string,int>::iterator it1=words.lower_bound(search);
multimap<string,int>::iterator it2=words.upper_bound(search);
while(it1!=it2)
{
int x=it1->second;
map<int,string>::iterator iter=lines.find(x);
cout<<'\n'<<x<<" ) "<<iter->second<<'\n';
it1++;
while(true)
{
if(it1!=it2 && it1->second==x)
{
it1++;

}
else
{
break;
}
}
}

cout<<'\n';

return 0;
}


how can i do it? cheers
Last edited on Jan 16, 2012 at 4:50pm
Jan 17, 2012 at 12:39am
bump
Jan 17, 2012 at 2:20am
I think the reason why you're not getting many responses is because we're not exactly sure what you want. What do you mean by implement? The sorting code is right there for the taking, isn't it?

-Albatross
Jan 17, 2012 at 6:05am
You told us nothing about what problem your having...
Jan 17, 2012 at 2:01pm
Sorry for the misinformation I gave to you guys.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    /* 
    * File:   main.cpp
    * Author: black4
    *
    * Created on January 13, 2012, 10:04 PM
    */
    #include <map>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h>
    #include <cstring>
    #include <sstream>
    #include <vector>
    #include <iomanip>
    using namespace std;

    int main () 
    {
        int o = system("lynx -dump \"http://www.haha.com\" > file.txt");
        int number;
    {
        string data;
        string text="file.txt";
        int position=0;
        ifstream getData (text.c_str());
        if (getData.is_open())
        {
            while (getData.good())
            {
                string result;
                while(getline(getData, result))
                {data+=result;}
            }

                    getData.close();

            position=data.find("[101]")-0;
            data.erase(data.begin(),data.begin()+position);
            position=data.find("* [131]")+7;
            data.erase(data.begin()+position,data.end()); 

            ofstream output("file2.txt"); //create file for input

            for(int n=104;n<132;n+=3) 
            {
            stringstream intToString;
            string name;
            string date;
            string price;
            string line,tmp;
            int newposition=0; 
            intToString<<n;
            position=data.find("["+intToString.str()+"]");
            line=data.substr(0,position);

            newposition=line.find("in [");
            name=line.substr(5,newposition-17);
            date=line.substr(newposition-12,12);
            cout<<name<<endl<<endl;
            cout<<date<<endl<<endl;

            int money;
            money=line.find("S$");
            price=line.substr(money,line.length());
            cout<<price<<endl<<endl;


            data.erase(data.begin(),data.begin()+position);

            output << "[name]-[publish]-[pricing]" 
                    << '\n' << name<< "-" << date<< "-" << price <<'\n' << endl;
            }

//advance searcher for file2.txt
    multimap<string,int> words;
    map<int,string> lines;
    string str;
    ifstream Myfile;
    Myfile.open("file2.txt"); 
    if(Myfile.fail())
    {
        cerr<<"\nThe file could not be opened.";
        return -1;
    }
    int i=1;
    while(getline(Myfile,str))
    {
        istringstream in(str);
        string s;
        while(in>>s)  
        {
            words.insert(make_pair(s,i));
        }
        lines.insert(make_pair(i,str));
        i++;
    }
    string search;
    cout<<"\nEnter a word to search: ";
    cin>>search;
    cout<<"\nThe number of matches = "<<words.count(search)<<'\n';
    multimap<string,int>::iterator it1=words.lower_bound(search);
    multimap<string,int>::iterator it2=words.upper_bound(search);
    {
        int x=it1->second;
        map<int,string>::iterator iter=lines.find(x);
        cout<<'\n'<<x<<" ) "<<iter->second<<'\n';
        it1++;
    while(true)
    {
        if(it1!=it2 && it1->second==x)
    {
    it1++;
    }
    else
    {
    break;
    }
    }
    }
    cout<<'\n';
        }
        else
        {
        cout<<"Database not found.";
        }
        cin>>number;
        }
        return 0;
    }



I have rewritten the code for parsing and search in the particular file. Now the problem is I need to hand this assignment with CPPunit testing and thus I need to write it in classes and I am very bad with classes.What can I do with this set of code I have to rewrite it into classes so I can test each individual class. Please assist thanks.
Last edited on Jan 17, 2012 at 2:02pm
Topic archived. No new replies allowed.