|
-
January 10th, 2007, 01:07 AM
#1
word freq and length sorting.. need help.. stuck..
hye.. i really need ur guys help.. im stuck.. i want to count the word freq and print them according to the word length.. > the longest be output first..
ouh.. and my input must be alphabet or alhabet+numeric only..
here what i did so far..
Code:
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Cmp : public std::binary_function<std::string,std::string,bool> {
public:
bool operator()(const std::string& s1, const std::string& s2){ //????
}
};
//typedef std::map<std::string, int, Cmp> MAP;
typedef enum {s1, s2} STATUS;
struct Count {
int N;
Count() : N(0) {}
operator int() { return N; }
void operator++() { N++; }
};
typedef map<string,Count> Map;
int main(void){
char c;
map<string,Count> m;
std::string buffer;
STATUS status=s1;
// while( cin >> str ) stringCounts[str]++;
while(std::cin >> c){
switch(status){
case s1:
if(isalpha(c)){
// m[string]++;
buffer=c;
status=s2;
}
break;
case s2:
if(isalnum(c)){
buffer+=c;
}else{
//std::cout << s1 << s2 << std::endl;
std::cout << c << buffer << std::endl;
status=s1;
}
break;
}
}
if(status=s2){
Map::iterator i = m.begin();
while ( i != m.end() ) {
cout << i->first << ':'
<< i->second << endl;
++i;
std::cout << c << buffer << std::endl;
}
}
return 0;
}
can you guys please help me.
Last edited by rehet; January 10th, 2007 at 02:00 AM.
-
January 10th, 2007, 01:32 AM
#2
Re: word freq and length sorting.. need help.. stuck..
-
January 10th, 2007, 01:59 AM
#3
Re: word freq and length sorting.. need help.. stuck..
owh.. okay2..
Code:
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Cmp : public std::binary_function<std::string,std::string,bool> {
public:
bool operator()(const std::string& s1, const std::string& s2){ //????
}
};
//typedef std::map<std::string, int, Cmp> MAP;
typedef enum {s1, s2} STATUS;
struct Count {
int N;
Count() : N(0) {}
operator int() { return N; }
void operator++() { N++; }
};
typedef map<string,Count> Map;
int main(void){
char c;
map<string,Count> m;
std::string buffer;
STATUS status=s1;
// while( cin >> str ) stringCounts[str]++;
while(std::cin >> c){
switch(status){
case s1:
if(isalpha(c)){
// m[string]++;
buffer=c;
status=s2;
}
break;
case s2:
if(isalnum(c)){
buffer+=c;
}else{
//std::cout << s1 << s2 << std::endl;
std::cout << c << buffer << std::endl;
status=s1;
}
break;
}
}
if(status=s2){
Map::iterator i = m.begin();
while ( i != m.end() ) {
cout << i->first << ':'
<< i->second << endl;
++i;
std::cout << c << buffer << std::endl;
}
}
return 0;
}
-
January 10th, 2007, 02:41 AM
#4
Re: word freq and length sorting.. need help.. stuck..
okay.. ive been working on it.. and i have this..
Code:
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Cmp : public std::binary_function<std::string,std::string,bool> {
public:
bool operator()(const std::string& s1, const std::string& s2){
}
};
typedef std::map<std::string, int, Cmp> MAP;
typedef enum {s1, s2} STATUS;
int main(void){
char c;
MAP m;
std::string buffer;
STATUS status=s1;
while(std::cin.get(c)){
switch(status){
case s1:
if(isalpha(c)){
buffer=c;
//m[buffer]++;
status=s2;
}
break;
case s2:
if(isalnum(c)){
//m[buffer]++;
buffer+=c;
}else{
// std::cout << buffer << std::endl;
//m[buffer]++;
status=s1;
}
break;
}
}
if(status=s2){
// std::cout << buffer << std::endl;
//m[buffer]++;
}
/*MAP::iterator i = m.begin();
while ( i != m.end() ) {
cout << i->first << ':'
<< i->second << endl;
++i;
*/
for(MAP::iterator i=m.begin(); i!=m.end(); ++i){
std::cout << i->first << ":" << i->second << std::endl;
}
return 0;
}
some one pleaseeeeeeeeeee help........
-
January 10th, 2007, 07:21 AM
#5
Re: word freq and length sorting.. need help.. stuck..
It is still not clear to what what your requirements are. I don't
quite understand the numeric vs non-numeric. Maybe this will
get you started ...
Code:
#include <iostream>
#include <string>
#include <map>
#include <functional>
using namespace std;
class Cmp : public std::binary_function<std::string,std::string,bool> {
public:
bool operator()(const std::string& s1, const std::string& s2)
{
// first sort by size
if (s1.size() > s2.size()) return true;
if (s1.size() < s2.size()) return false;
// if sizes are equal ... sort alphabetically
return s1 < s2;
}
};
typedef std::map<std::string, int, Cmp> MAP;
int main()
{
MAP m;
++m["test"];
++m["a test"];
++m["test"];
++m["v test"];
++m["f test"];
++m["abcd"];
for (MAP::iterator i=m.begin(); i!=m.end(); ++i)
{
std::cout << i->first << ":" << i->second << std::endl;
}
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|