Link to home
Start Free TrialLog in
Avatar of PALKTA
PALKTA

asked on

text file sorting

I need some help on this,
i need to read in text from a file called test.txt sort this info alphabetically and print the result out to report.txt

The input file would look like this;

when will we go to
the aid of the party


The output file should look like this;

aid
go
of
party
to
the
we
when
will

If a word appears twice it should only be printed once.

The problem is that i cannot use vectors, therefore it must be done using either binary trees or singly linked lists.

This is very important thanks in advance!!
ASKER CERTIFIED SOLUTION
Avatar of SimesA
SimesA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lukasz Zielinski
Just another homework... SimesA posted Delphi code..if You want to use Delphi use TStringList Sort method already implemented there (QuickSort)
ziolko.
Avatar of SimesA
SimesA

Hey even homework can be fun!

I purposely didn't use TStringList.Sort, since it wouldn't be possible to convert to other languages. Also Palkta requested linked list or binary trees.
I'm not saying its not fun :-) I still use BSTs :-)) not only funny but usefull.
ziolko.
Hope you get an A.

mlmcc
Avatar of PALKTA

ASKER

Sorry for not saying which language but it's c++

completly slipped my mind and i hope i get an A as well cas i need it!!!
Delphi -> C++ shouldn't be too hard for you!
>> Hey even homework can be fun!

Completing homework problems is a violation of the EE membership agreement. :-)

Exceter
Palkta
When you have the program translated and almost running check back.

mlmcc
Sorry if this sounds really lame compared to the above (!) but how about importing the file into an MS Access table (you could use OLE) and running a query (select distinct ... order by ... )?  I reckon it would take fewer lines of code and might work fine.  I'm just lazy - I'd always use somebody else's database engine to do this stuff, rather than trying to code it myself!

Good luck

Ed
All that work - a full running program and you give him a C?

mlmcc
<shrug>Some kids are *so* ungrateful</shrug>

I don't suppose anybody's listening now, but I coded up my MS Access solution as an exercise and it works fine.  For me it's *considerably* more comprehensible than the accepted answer above!

This VB code behind a button:

Private Sub Command1_Click()

    Dim db As Database
    Dim tb As TableDef
    Dim fd As Field
    Dim ws As Workspace
    Dim con As DAO.Connection
    Dim qry As QueryDef
    Dim qry1 As QueryDef
    Dim rs As Recordset
    Dim intFile As Integer
    Dim strText As String
    Dim varText As Variant
    Dim intCounter As Integer
    Dim intFile1 As Integer
    Dim intFile2 As Integer
    Dim x As String
   
    'create MS Access DB, table and field
    Set db = CreateDatabase("MyDB", dbLangGeneral)
    Set tb = db.CreateTableDef("tb")
    Set fd = tb.CreateField("word", dbText)
    tb.Fields.Append fd
    db.TableDefs.Append tb
   
    'create queries
    Set qry = db.CreateQueryDef("qry")
    Set qry1 = db.CreateQueryDef("qry1", "select distinct word from tb order by word")
   
    'get text data from file
    intFile1 = FreeFile
    Open txtFile For Input As #intFile1
    Input #intFile1, strText
   
    'make array of words
    varText = Split(strText, " ")
   
    'insert words into table
    For intCounter = 0 To UBound(varText)
        qry.SQL = "insert into tb (word) values('" & varText(intCounter) & "')"
        qry.Execute
    Next
   
    'sort alphabetically
    Set rs = qry1.OpenRecordset
   
    'open new text file
    intFile2 = FreeFile
    Open "c:\myfile.txt" For Append As #intFile2
   
    Do While Not rs.EOF
        Print #intFile2, rs!word
        rs.MoveNext
    Loop
   
    db.Close
    Close #intFile1
    Close #intFile2
    Set db = Nothing
    x = CurDir
    Kill x & "/MyDB.mdb"
   
End Sub

You need DAO and Access in the project references.
Place a textbox on the form to key the path/filename of the input file.  The output file is hardcoded.

Oh well, I enjoyed writing it!

ED
Ed2003, I think you missed the point.

The point isn't to get a list of the words sorted into alphabetical order. It's an execise in solving a problem programatically, withing certain constraints.

So remove the Access DB, add some data storage (linked list or binary tree if possible) and sort the result. Oh, and do it all in C++.
SimesA

I can see that you took it as such and, no doubt, produced an excellent solution from an intellectual standpoint.  However, there's nothing I can see in the original question to indicate that it's a test exercise, rather than a practical one.  In the real world, as I'm sure you're aware, we need to produce a cost-effective solution using the tools available.  One would not, therefore, normally choose to write pages of code, when optimised compiled solutions already exist that can be harnessed with a little effort.  I used VB because I know it - you may well be able to generate an equivalent solution in C even more economically.

Regards

Ed
Yep, you're absolutely right. I humbly apologise.
Accepted, and sorry if I was a bit OTT there!