Linux sort used for C++ program
Apr 5, 2010 at 5:20am UTC
I am quite confused as I was told that I need to redirect the output to be the
input of the Linux sort program and sort the results from high count
to low. I haven't figured out this aspect of of Linux yet as I have only used it for going on a week now. If anyone could point me in the right direction as to how I would redirect my output it would be appreciated! Thank you!
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
#include <iostream>
#include <string.h>
using namespace std;
struct wl
{
char *word;
int count;
struct wl * next;
};
int main()
{
struct wl *start, *p1, *p2, *p3;
char tmp[256], *s1;
int p7;
start = NULL;
while (1){
cin>>tmp;
if (cin.eof()) break ;
for (p1 = start; p1 != NULL; p1 = p1 -> next)
{
if (strcmp(tmp,p1->word)==0){
p1->count++;
break ;
}
}
if (p1 != NULL) continue ;
p1 = new struct wl;
p1->count=1;
s1= new char [strlen(tmp)+1];
strcpy(s1,tmp);
p1->word=s1;
p1->next=start;
start=p1;
}
for (p1=start; p1 != NULL; p1=p1->next)
{
p7=261;
if (p1->count >p7)
{
cout<<p1->count<<" " <<p1->word<<endl;
}
}
return 0;
}
Apr 5, 2010 at 11:15am UTC
Apr 5, 2010 at 12:10pm UTC
Via command line:
$ yourprogam | sort
To answer the next question,
Last edited on Apr 5, 2010 at 12:10pm UTC
Apr 9, 2010 at 6:10am UTC
The above will give you result in ascending order. to get the result in descending order use:
Topic archived. No new replies allowed.