C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old April 7th, 2004, 08:00 PM
gonzopb gonzopb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 1 gonzopb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
alphabetically sort a file in c++

i have a file of company names and i need to sort them alphabetically. i am using borland c++ 5.1.
thanks for the help
gonzopb

Reply With Quote
  #2  
Old April 12th, 2004, 01:04 AM
fisch fisch is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 5 fisch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
using an array-name[], of length-length


for (int i =0;i<length<i++){
for (int j=0; j<length-1-i; j++){
if name[j]>name[j+1]{
swap the names;
}//end if
}//end for j
}//end for i

I think that works, been years since i've done it.

Reply With Quote
  #3  
Old June 2nd, 2004, 07:15 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Binary tree implementation

Got bored so knocked up a binary tree version of this as an alternative.

Hope it helps someone,

-KM-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINE_LEN 128

typedef struct T
{
char *data;
struct T *left;
struct T *right;
} node;

node *makeNode (char *data)
{
node *temp = (node *) malloc (sizeof (node));

temp->data = calloc (LINE_LEN, sizeof (char));
strcpy (temp->data, data);
temp->left = NULL;
temp->right = NULL;

return temp;
}

node *addNode (char *line, node *root)
{
if (root == NULL)
return makeNode (line);

if (strcmp (line, root->data) < 0)
root->left = addNode (line, root->left);
else
root->right = addNode (line, root->right);

return root;
}

node *readFile (FILE *in_fp)
{
node *root = NULL;
char *line = (char *) calloc (LINE_LEN, sizeof (char));

while (!feof (in_fp))
{
fscanf (in_fp, "%s", line);
root = addNode (line, root);
}

return root;
}

void writeFile (node *root, FILE *out_fp)
{
if (root->left != NULL)
writeFile (root->left, out_fp);

fprintf (out_fp, "%s\n", root->data);

if (root->right != NULL)
writeFile (root->right, out_fp);
}

void sort (char *in, char *out)
{
FILE *in_fp = fopen (in, "r");
FILE *out_fp = fopen (out, "w");
node *root = readFile (in_fp);

writeFile (root, out_fp);
fclose (in_fp);
fclose (out_fp);
}

int main (int argc, char **argv)
{
if (argc != 3)
printf ("Usage: sort unsorted_filename sorted_filename\n");
else
sort (argv [1], argv [2]);

return 0;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > alphabetically sort a file in c++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway