我被问到最近在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
答案 0 :(得分: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;
}