Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Sorting from a file
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
marshymell0




PostPosted: Mon Apr 24, 2006 9:13 pm   Post subject: Sorting from a file

Ok..the problem I'm having is basically, I don't know where to start. Basicially, a program is made in which a text file is examined, and the results of the file is shown. The results show specific results, such as how many consonants, which constants were in the file, speical symbols, etc.

For example, if the text file is
code:
36eWE^$32

My output would be 4 numbers total, numbers are 2,3,6
and 3 letters, which are W,E,e, and 2 speical symbols are ^,$

My friend taught me how to open the file, etc. This is what the code looked like.

code:
%declare variables
var filename, lineOut : string
var lineIn : flexible array 1 .. 0 of string
var fileIn, fileOut, lineNumber : int

var num : char
var total, position : int := 0
var tempString : string

%%%%%%%%%%%
%opening file
%get filename from user
put "Enter filename " ..
get filename
%open file
open : fileIn, filename, get
%check file was opened
assert fileIn > 0
%set lineNumber
lineNumber := 1
%read each line of text file and put them into an array of string ( lineIn() )
loop
    new lineIn, lineNumber %resize array
    %exit when end of file (EOF)
    exit when eof (fileIn)
    get : fileIn, lineIn (lineNumber) : *
    put lineNumber : 3, ": ", lineIn (lineNumber)
    lineNumber += 1
end loop

%close file
close : fileIn


Now, for the actual sorting, this is what I'm sorta thinking. Can I have a for loop, in which I go from like characters a-Z, whatever ASCII values those are, and set is as a boolean. So, if chr (x)=A, then I add one to the storage array for the letter "A"

code:

var store array:1..100 of int
for x :64..128 %or whatever the right numbers are for a-Z
               chr (x)
if chr(x)=true then
store(x)+=1
end for


That sort of the basic idea, but the problem is u can't have the chr(x)=true.
store (x)
end for [code]
Sponsor
Sponsor
Sponsor
sponsor
NikG




PostPosted: Mon Apr 24, 2006 10:31 pm   Post subject: (No subject)

Sorry, I don't have much time right now to give a full response, but here's something that might help you if you're going to be using for loop comparisons. The for loop is actually more capable than you think:
code:
for i : '0' .. '9'
    put i ..
end for

for i : 'a' .. 'z'
    put i ..
end for
NikG




PostPosted: Mon Apr 24, 2006 10:49 pm   Post subject: (No subject)

Sorry about the vague previous response, I have a few more minutes now. You don't need an array. All you need is 3 string variables: one for the numbers, one for the letters, one for the special characters.
Then go through each character of each line and keep adding the character to one of the strings depending on which test it passes:
code:
for i : 1 .. length (line)
    if line (i) >= 'a' and line (i) <= 'z' then
        TotalLetters = TotalLetter & line (i)
    end if
    %...
end for
And too see how many are in each string, just use the length function
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: