0

Sevone での最近のインタビューで質問されましたが、私がどのように回答したかについての回答はありませんでした。チャレンジを完了するのに2時間しかなく、性別で並べ替えることができませんでした. これが私が始めたものであり、さらに下に私の解決策がありました

// Sevone Programming Challenge!

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

//! This is the sex of a human.
typedef enum { MALE, FEMALE } Gender;

//! This is a human person.
typedef struct Person {
    //! The given name.
    char* firstName;
    //! The family name.
    char* lastName;
    //! The age, in calendar years.
    int age;
    //! The sex (see above).
    Gender gender;
} Person;

/** This is the bonus function.
  **/
void bonusFunction();

/** This is the core of the program.
  **/
int main( int argc, char** argv ) {

    // INSTRUCTIONS:
    //    Please refer to: http://developer.gnome.org/glib/2.30/glib-Double-ended-Queues.html 
    // 1. Open "people.csv".
    // 2. Read in the list of people (format is "firstName lastName,age,{male|female}").
    // 3. Create a "Person" for each one read.
    // 4. Insert each "Person" into a GLib GQueue (this is a "double-ended queue", but think of it like a list).
    // 5. Sort the list (by using "g_queue_sort") by:
    //       1. Gender
    //       2. Last name
    // 6. Print out the list (by using "g_queue_foreach").  The format should be:
    //       (male/female) Last name, First name (age)
    // 7. Free up all memory (we're gonna valgrind this afterward).


// Ready for the bonus?
    bonusFunction();

    // KTHXBYE
    return( 0 );
}

/** This is the bonus function.
  **/
void bonusFunction() {
    //! This is the bonus array!
    int arrayOfInts[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34 };


    // BONUS!
    // 1. Loop through the bonus array and print it without using square brackets ("[" and "]").


    // All done.
    return;
}

これが私の解決策です。性別またはその他の最適化で並べ替える方法を教えてください。

// Programming Challenge!

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

//! This is the sex of a human.
typedef enum { MALE, FEMALE } Gender;

//! This is a human person.
typedef struct Person {
    //! The given name.
    char* firstName;
    //! The family name.
    char* lastName;
    //! The age, in calendar years.
    int age;
    //! The sex (see above).
    Gender gender;
} Person;

/** This is the bonus function.
  **/
void bonusFunction();

gint sort_lastName(gconstpointer a, gconstpointer b, gpointer data) {
    return strcmp( ((Person*)a)->lastName, ((Person*)b)->lastName );
}
void prt(gpointer per) {
 printf("%s, %s %d  \n ", ((Person*)per)->lastName, ((Person*)per)->firstName,((Person*)per)->age);
}

/** This is the core of the program.
  **/
int main( int argc, char** argv ) {
    puts("Starting SevOne Test\n");
    // INSTRUCTIONS:
    //    Please refer to: http://developer.gnome.org/glib/2.30/glib-Double-ended-Queues.html 
    // 1. Open "people.csv".
    // 2. Read in the list of people (format is "firstName lastName,age,{male|female}").
    // 3. Create a "Person" for each one read.
    // 4. Insert each "Person" into a GLib GQueue (this is a "double-ended queue", but think of it like a list).
    // 5. Sort the list (by using "g_queue_sort") by:
    //       1. Gender
    //       2. Last name
    // 6. Print out the list (by using "g_queue_foreach").  The format should be:
    //       (male/female) Last name, First name (age)
    // 7. Free up all memory (we're gonna valgrind this afterward).


    char buffer[100];
    int counter=0;
    char * token;
    GQueue* q = g_queue_new();

    FILE *fp;
    fp=fopen("people.csv", "r");

    if( fp == NULL )
    {
        puts("Failed to open file");
        return 0;
    }



    while(fgets(buffer, sizeof(buffer), fp) != NULL)
    {
        Person *ptr_one;
        ptr_one = (Person *) malloc (sizeof(Person));

        // Get first name
        token = strtok(buffer," ");
        ptr_one->firstName=(char *)malloc(sizeof(char)*sizeof(token));
        strcpy(ptr_one->firstName,token);

        // Get last name
        token = strtok(NULL,",");
        ptr_one->lastName=(char *)malloc(sizeof(char)*sizeof(token));
        strcpy(ptr_one->lastName,token);

        // Get age
        token = strtok(NULL, ",");
        ptr_one->age=(int *)malloc(sizeof(int));
        sscanf (token, "%d", &ptr_one->age);

        // Get gender
        token = strtok(NULL,",\n");


        g_queue_push_tail(q, ptr_one);

    }

    // Sort list by last name
    g_queue_sort(q, (GCompareDataFunc)sort_lastName, NULL);

    // print the list
    g_queue_foreach(q, (GFunc)prt, NULL);
    if( fclose(fp) != 0 )
    {
        puts("Failed to close file."); /* prints !!!Hello World!!! */
        return 0;
    }
    g_queue_free(q);




// Ready for the bonus?
    bonusFunction();

    // KTHXBYE
    return( 0 );
}

/** This is the bonus function.
  **/
void bonusFunction() {
    //! This is the bonus array!
    int arrayOfInts[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34 };


    // BONUS!
    // 1. Loop through the bonus array and print it without using square brackets ("[" and "]").


    // All done.
    return;
}

そして以下のCSV

Brad Fawcett,24,male
Steve Settlemyre,29,male
Dave Hegenbarth,44,male
Cathy Colapiertro,41,female
Steve Mahoney,23,male
Dave Mulford,26,male
Doug Manley,24,male
Steve Carrington,24,male
Lauren Jordan,31,female
Tanya Bakalov,26,female
4

1 に答える 1

1

まず、性別を適切に解析する必要があります。

    // Get gendor
    token = strtok(NULL,",\n");
    if(strcasecmp(token, "female") == 0)
        ptr_one->gender = FEMALE;
    else
        ptr_one->gender = MALE;

次に、適切な比較関数を使用します。

gint sort_gender(gconstpointer a, gconstpointer b, gpointer data) {
    return ((Person*)a)->gender - ((Person*)b)->gender;
}

そしてボーナス機能:

void bonusFunction() {
    //! This is the bonus array!
    int arrayOfInts[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34 };

    // BONUS!
    // 1. Loop through the bonus array and print it without using square brackets ("[" and "]").
    int i;
    int *n = arrayOfInts;
    for(i = 0; i < sizeof(arrayOfInts)/sizeof(int); i++)
        printf("%d\n", *n++);

    return;
}
于 2013-03-13T03:53:17.857 に答える