473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

word freq and length sorting.. need help.. stuck..

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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. using namespace std;
  5. class Cmp : public std::binary_function<std::string,std::string,bool> {
  6. public:
  7.   bool operator()(const std::string& s1, const std::string& s2){ //????
  8.   }
  9. };
  10. //typedef std::map<std::string, int, Cmp> MAP;
  11.  
  12. typedef enum {s1, s2} STATUS;
  13. struct Count {
  14.     int N;
  15.     Count() : N(0) {}
  16.     operator int() { return N; }
  17.     void operator++() { N++; }
  18. };
  19. typedef map<string,Count> Map;
  20.  
  21. int main(void){
  22.   char c;
  23.   map<string,Count> m;
  24.   std::string  buffer;
  25.     STATUS status=s1;
  26. //    while( cin >> str ) stringCounts[str]++; 
  27.   while(std::cin >> c){
  28.  
  29.     switch(status){
  30.     case s1:
  31.       if(isalpha(c)){
  32. //    m[string]++;
  33.     buffer=c;
  34.     status=s2;
  35.  
  36.       }
  37.       break;
  38.     case s2:
  39.       if(isalnum(c)){
  40.     buffer+=c;
  41.       }else{
  42.     //std::cout << s1 << s2 << std::endl;
  43.  
  44.     std::cout << c << buffer << std::endl;
  45.     status=s1;
  46.       }
  47.  
  48.       break;
  49.     }
  50.   }
  51.   if(status=s2){
  52.  
  53. Map::iterator i = m.begin();
  54.     while ( i != m.end() ) {
  55.       cout << i->first << ':'
  56.            << i->second << endl;
  57.       ++i;
  58.    std::cout << c << buffer << std::endl;
  59.   }
  60.  
  61.  
  62. }    
  63.   return 0;
  64. }
can you guys please help me..
Jan 10 '07 #1
2 2051
DeMan
1,806 1GB
Just to make sure we're on the same page, do you mean you want to count the frequency of characters (alphabetic and numeral only) in a given input, and output them according to the most frequently occuring, or do you actually want to find the most common word (sequence of alphabetic/numeral characters),

I have assumed the first, and would suggest you make an array of 36 char/int values. Then choose to assign each possible numeral or alpha-character one of these array items. Using a case statement you can populate the array something along the lines of (in pseudocode of course, so I can't be accused of making syntactic mistakes):
Expand|Select|Wrap|Line Numbers
  1. int[] counts = new int[36] to initialise each to 0
  2. while (we have more input)
  3. {
  4.   currentChar = getChar();
  5.   switch (currentChar)
  6.   {
  7.     case '0'
  8.       counts[0]++;
  9.       break;
  10.     case '1': //note the quote marks, we are referring to character not int value
  11.       counts[1]++;
  12.       break;
  13.  /* (same for other numerals) */
  14.  
  15.     case 'a':
  16.     case 'A':
  17.       counts[10]++;
  18.       break;
  19.     case 'b':
  20.     case 'B':
  21.       counts[11]++;
  22. /* Same for other alpha's */
  23.   }
  24. }
  25.  
There are many ways to sort some easier than others, btu I'll leave you with this much so far (in case I got the wrong end of the stick) and you can ask more questions later, which I'm sure someone (possibly me again) can answer if you get stuck....
Jan 10 '07 #2
rehet
2
hye.. thanksss.. :d

but i finished it already..

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. using namespace std;
  5. class Cmp : public std::binary_function<std::string,std::string,bool> {
  6. public:
  7. bool operator()(const std::string& s1, const std::string& s2){
  8.  
  9. int l1 = s1.length();
  10. int l2 = s2.length();
  11. if(l1==l2){
  12.           return s1>s2;
  13.           }
  14.           else{
  15.                return l1>l2;
  16.                } 
  17.  
  18. }
  19. };
  20. typedef std::map<std::string, int, Cmp> MAP;
  21.  
  22. typedef enum {s1, s2} STATUS;
  23.  
  24. int main(void){
  25.   char c;
  26.   MAP m;
  27.   std::string  buffer;
  28.     STATUS status=s1;
  29.  
  30.   while(std::cin.get(c)){
  31.  
  32.     switch(status){
  33.     case s1:
  34.       if(isalpha(c)){
  35.  
  36.     buffer=c;
  37. //m[buffer]++;
  38.     status=s2;
  39.  
  40.       }
  41.       break;
  42.     case s2:
  43.  
  44.       if(isalnum(c)){
  45.  
  46.     buffer+=c;
  47.       }else{
  48.  
  49. m[buffer]++;
  50.     status=s1;
  51.       }
  52.  
  53.       break;
  54.     }
  55.   }
  56.   if(status=s2){
  57.  
  58.  
  59. m[buffer]++;
  60.   }
  61.  
  62.  
  63.  
  64. for(MAP::iterator i=m.begin(); i!=m.end(); ++i){
  65.  
  66.                   std::cout << i->first << ":" << i->second << std::endl;
  67.  
  68.  
  69. }    
  70.   return 0;
  71. }
  72.  
Jan 11 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Case Nelson | last post by:
Hi there I've just been playing around with some python code and I've got a fun little optimization problem I could use some help with. Basically, the program needs to take in a random list of no...
4
by: suzy | last post by:
hello. how can i sort data in a dataset? all the examples i have seen on msdn, etc are sorting a dataview. this works fine, but i want to return the results in xml and the dataview doesn't...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
8
by: Frost | last post by:
Hi All, I am a newbie i have written a c program on unix for line by line comparison for two files now could some one help on how i could do word by word comparison in case both lines have the...
1
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD:...
8
by: Andrew Savige | last post by:
I'm learning Python by reading David Beazley's "Python Essential Reference" book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not...
0
by: alivip | last post by:
I write code to get most frequent words in the file I won't to implement bigram probability by modifying the code to do the following: How can I get every Token (word) and ...
5
by: alivip | last post by:
How can I get every Token (word) and PreviousToken(Previous word) From multube files and frequency of each two word my code is trying to get all single word and double word (every Token (word) and...
11
by: arnuld | last post by:
C takes input character by character. I did not find any Standard Library function that can take a word as input. So I want to write one of my own to be used with "Self Referential Structures" of...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.