Click to See Complete Forum and Search --> : word freq and length sorting.. need help.. stuck..


rehet
January 10th, 2007, 12:07 AM
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..


#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.

laasunde
January 10th, 2007, 12:32 AM
Please use code tags.

rehet
January 10th, 2007, 12:59 AM
owh.. okay2..


#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;
}

rehet
January 10th, 2007, 01:41 AM
okay.. ive been working on it.. and i have this..


#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........

Philip Nicoletti
January 10th, 2007, 06:21 AM
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 ...


#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;
}