CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Jan 2007
    Posts
    3

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured