Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   C++ file sorting (http://www.programmingforums.org/showthread.php?t=15520)

jack0987 Mar 31st, 2008 2:43 PM

C++ file sorting
 
Can somebody give me an idea how to start doing this??

Tasks:
1. There is one input file which contains a list of data with a number of columns and rows.
2. The file should be sorted according to different column(ranks, location...) depending on the input of the user.


My problem is that how should i read the rows so that it can be stored it in a structure, such that the file can be sorted according to one column at a time(e.g. according to the rank).

Should spaces be taken into consideration while reading the file??

thanks in advance for any help would be appreciated!

Freaky Chris Mar 31st, 2008 3:17 PM

Re: C++ file sorting
 
Perhaps you could read the file line by line, and then split the string and store each value into and array, this way you can then access each value and say store it into an array for each coloumn. Checking for white space is a good way to know where to split your string. This may be helpful to you as to how you can cut the string. Also combine this with white space checking and you should have a solution to your problem.

Chris

Jimbo Mar 31st, 2008 10:00 PM

Re: C++ file sorting
 
Like Chris said, you basically need to decide on a delimiting character to separate your columns. You could use a comma, which is fairly common. This of course means that none of your values can contain a comma (or whatever character you choose) unless you provide another marker to escape that value, which then makes the marker invalid unless you have a sequence to add it (think of how escaping with \ works; you can probably just use that, really).

lectricpharaoh Apr 1st, 2008 4:25 PM

Re: C++ file sorting
 
How is the file laid out? Are the columns padded with spaces or tabs to line up nicely for a human reader, or is there some other delimiter used? Is the file even plain text, or is it a binary file made up of fixed-size records?

If it's a plain text file, perhaps you could post a small piece (half a dozen lines or so) of it here inside [code] [/code] tags to preserve the formatting, and we can offer more helpful suggestions.

jack0987 Apr 1st, 2008 4:57 PM

Re: C++ file sorting
 
thanks again!!
here is how the file is laid out
:

World Rank        Institution                                            Region        Regional Rank        Country                National Rank        Score on Alumni        Score on Award        Score on HiCi        Score on N&S        Score on SCI        Score on Size        Total Score
1                Harvard Univ                                            Americas        1        USA                        1                100                100                100                100                100                72.4                100
2              Univ Cambridge                                            Europe                1        UK                        1                99.8                93.4                53.3                56.6                70.9                66.9                73.6
3              Stanford Univ                                            Americas        2        USA                        2                41.1                72.2                88.5                70.9                72.3                65                73.4
4              Univ California - Berkeley                            Americas        3        USA                        3                71.8                76                69.4                73.9                72.2                52.7                72.8
5            Massachusetts Inst Tech (MIT)                          Americas        4        USA                        4                74                80.6                66.7                65.8                64.3                53                70.1
6              California Inst Tech                                  Americas        5        USA                        5                59.2                68.6                59.8                65.8                52.5                100                67.1
7                Columbia Univ                                        Americas        6        USA                        6                79.4                60.6                56.1                54.2                69.5                45.4                62.3
8                  Princeton Univ                                Americas        7        USA                        7                63.4                76.8                60.9                48.7                48.5                59.1                60.9
9                    Univ Chicago                                Americas        8        USA                        8                75.6                81.9                50.3                44.7                56.4                42.2                60.1
10                    Univ Oxford                                        Europe                2        UK                        2                64.3                59.1                48.4                55.6                68.4                53.2                59.7
11                    Yale Univ                                  Americas        9        USA                        9                52.1                44.5                60.3                57.2                63.9                49.3                56.9
12                    Cornell Univ                                Americas        10        USA                        10                46.5                52.4                55                48.8                66.3                39.8                54.6
13        Univ California - San Diego                                Americas        11        USA                        11                17.7                34.7                59.8                56.5                64.5                46.6                51


mbd Apr 3rd, 2008 3:14 AM

Re: C++ file sorting
 
here is a start. it copies standard input into a vector, sorts it, then copies the vector to standard output. this just uses a lexicographical string comparison.
:

  1. #include <string>
  2. #include <vector>
  3. #include <iterator>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. int main()
  8. {
  9.         std::vector< std::string > v;
  10.         std::copy(std::istream_iterator< std::string >(std::cin), std::istream_iterator< std::string >(), std::back_insert_iterator< std::vector <std::string > >(v));
  11.         std::sort(v.begin(), v.end());
  12.         std::copy(v.begin(), v.end(), std::ostream_iterator< std::string >(std::cout, "\n"));
  13.         return 0;
  14. }

a good place for you to begin would to be overriding the < operator for std::string.


All times are GMT -5. The time now is 4:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC