CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Sep 2004
    Posts
    244

    Post SortNames Program

    I was asked to write a sorting program in c++

    Specification:
    A program to sort a list of names is required.
    The file also contains the person’s age and phone number.
    The main challenge is that the entire list can not fit into
    physical memory at any given time.
    There is no limit to the amount of physical disk space.

    The Input

    An ASCII TSV (Tab Separated values) file containing a list of names.
    Each line is of the format:
    <Last Name>, <First Name><tab><age><tab><Phone Number><CR>

    Sample:

    Kalou, Salomon 30 476-2769
    Ad****or, Emmanuel 31 310-4951
    Morais, Nuno 37 660-6290
    Poom, Mart 34 469-8379
    Shevchenko, Andriy 26 922-2033
    Obi, Mikel 35 888-9370
    Ljungberg, Fredrik 34 492-1755
    Davenport, Calum 28 240-7734
    O'Hara, Jamie 30 333-5168

    The Output

    A file in the same format sorted by Last Name.

    The Program

    The final program must take three arguments

    Maxnames – the maximum number of names which can be loaded
    into memory at any given time
    Input - name of the input file
    Output – name of the results output file

    For example: SortNames.exe 10 input.txt output.txt

    So what I manage to achieve so far is to read 10 record from file, sort them and output the result.

    CodeListing:

    <code>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <algorithm>


    using namespace std;

    ////////////////////////////////////
    //Header
    ////////////////////////////////////
    struct PersonInfo
    {
    string firstName;
    string secondName;
    int age;
    string phoneNumber;

    PersonInfo(const string& fName = "",
    const string& sName = "",
    int personsAge = 0,
    const string& number = ""):
    firstName(fName), secondName(sName), age(personsAge), phoneNumber(number)
    {}

    friend bool operator<(const PersonInfo& person1, const PersonInfo& person2);
    };

    bool operator<(const PersonInfo& person1, const PersonInfo& person2)
    {
    int c1 = person1.firstName.compare(person2.firstName);
    int c2 = person1.secondName.compare(person2.secondName);
    int c3 = person1.phoneNumber.compare(person2.phoneNumber);

    return (c1 < 0) ||
    (c1 == 0 && c2 < 0) ||
    (c1 == 0 && c2 == 0 && person1.age < person2.age) ||
    (c1 == 0 && c2 == 0 && person1.age == person2.age && c3 < 0);
    }

    vector<PersonInfo> PersonInfoslist;

    void Run(const char* infile, int* maxEntries);
    //void WriteToTempFile(const vector<PersonInfo>& p);

    ////////////////////////////////////
    //Source
    ////////////////////////////////////
    void Run(const char* infile,int maxEntries)
    {
    ifstream inputFile(infile, ios::in);

    if (!inputFile)
    {
    cerr << "Input file "<< infile << " could not be opened!"<<endl;
    exit(1);
    }

    for (int i = 0;i < maxEntries;i++)
    {
    PersonInfo pInfo;
    char* buffer;
    inputFile.getline( buffer,2048);
    string line = buffer;

    int itemNum = 0;
    string token;
    size_t p0 = 0, p1 = string::npos;

    while(p0 != string::npos)
    {
    p1 = line.find_first_of(",\t ", p0);
    if(p1 != p0)
    {
    token = line.substr(p0, p1 - p0);
    }
    p0 = line.find_first_not_of(",\t ", p1);

    switch( itemNum )
    {
    case 0:
    pInfo.firstName = token;
    itemNum++;
    break;
    case 1:
    pInfo.secondName = token;
    itemNum++;
    break;
    case 2:
    pInfo.age = atoi(token.c_str());
    itemNum++;
    break;
    case 3:
    pInfo.phoneNumber = token;
    itemNum=0;
    PersonInfoslist.push_back(pInfo);
    break;
    default:
    cout << "Error in tokenizing string!" << endl;
    break;
    }
    }
    }

    //Sort the items in the vector
    sort(PersonInfoslist.begin(), PersonInfoslist.end());

    for(vector<PersonInfo>::const_iterator it = PersonInfoslist.begin(); it != PersonInfoslist.end(); ++it)
    {
    cout << it->firstName << ", " << it->secondName << ", " << it->age << ", " << it->phoneNumber << endl;
    }
    }

    int main(int argc, char** argv)
    {
    if (argc != 4)
    {
    cout << "Usage: SortNames <Number of names to read> <Input file> <Output file>" << endl;
    exit(1);
    }

    //Getting the arguments from command line.
    int maxEntries = atoi(argv[1]);
    const char* inFile = argv[2];
    const string outFile = argv[3];

    Run(inFile,maxEntries);

    return 0;
    }
    </code>

    So I was wondering if a generous C++ programmer out there will be kind enough to help me out on this task. Thanks in advance for any suggestion on improving the current solution
    Last edited by inbugable; December 21st, 2006 at 04:03 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured