所以,我们有这个家庭作业,建立在上周。我们应该创建单独的数组,其中包含指向特定作业类型的所有人的指针。这是我到目前为止的代码。我想知道是否有人可以帮助我。谢谢!
#include <stdio.h>
#include <stdlib.h>
#include "Person.h"
#include "data.h"
#define new
char * randomVal(char * arr[], int elements)
{
int value = (int)(rand() % elements);
char * name = arr[value];
return name;
}
int main (int argc, const char * argv[])
{
Person * Population[100];
int i;
for (i = 0; i < 100; i++)
{
Population[i] = new person(randomVal(Names, 10), randomVal(Vehicles, 5), randomVal(Jobs, 5), randomVal(Job_Pace, 3), randomVal(Education, 4), randomVal(Work_Ethic, 3), randomVal(Sleep_Habits, 3));
Population[i]->print(Population[i]);
}
for (i=0; i<100; i++) {
destroy(Population[i]);
}
//Person * p1 = new person(randomName(), "Camero", "Lawyer", "Fast", "Doctorate", "Poor", "Poor");
//p1->print(p1);
return 0;
}
/*
* Person.h
* Person2
*
* Created by Tracie Marshall on 10/15/09.
* Copyright 2009 Me. All rights reserved.
*
*/
typedef struct
{
//data
//dynamic sized vars
char * name;
char * vehicle;
char * job;
char * job_pace;
char * education;
char * work_ethic;
char * sleep_habits;
//static-sized vars
double bank_account;
int leadership;
int IQ;
int aggro;
int outfit;
int alertness;
//methods
void (*print)(void *);
} Person;
Person * person(char * n, char * v, char * j, char * jp, char * ed, char * w, char * s);
void printPerson(Person* p);
void destroy(Person * p);
/*
* Person.c
* Person2
*
* Created by Tracie Marshall on 10/15/09.
* Copyright 2009 Me. All rights reserved.
*
*/
#include <string.h>
#include <stdlib.h>
#include "Person.h"
#include <stdio.h>
char * newString(char * dest, char * source)
{
dest = (char *)malloc((strlen(source) + 1) * sizeof(char));
strcpy(dest, source);
return dest;
}
void printPerson(Person* p)
{
printf("\n%s:\n\tvehicle:\t%s\n\tjob:\t\t%s\n\tjob pace:\t%s\n\teducation:\t%s\n\twork ethic:\t%s\n\tsleep habit:\t%s", p->name, p->vehicle, p->job, p->job_pace, p->education, p->work_ethic, p->sleep_habits);//, p->age, p->job, p->spouse);
}
Person * person(char * n, char * v, char * j, char * jp, char * ed, char * w, char * s)
{
//allocate self
Person * p = (Person *) malloc(sizeof(Person));
//allocate dynamic variables on stack
p->name = newString(p->name, n);
p->vehicle = newString(p->vehicle, v);
p->job = newString(p->job, j);
p->job_pace = newString(p->job_pace, jp);
p->education = newString(p->education, ed);
p->work_ethic = newString(p->work_ethic, w);
p->sleep_habits = newString(p->sleep_habits, s);
//initalize static-size variables
p->bank_account; // = ba;
p->IQ;
p->aggro;
p->outfit;
p->alertness;
//initialize function pointers
p->print = &printPerson;
return p;
}
void destroy(Person * p)
{
free(p->name);
free(p->vehicle);
free(p->job);
free(p->job_pace);
free(p->education);
free(p->work_ethic);
free(p->sleep_habits);
free(p);
}
/*
* data.h
* Person2
*
* Created by Tracie Marshall on 10/15/09.
* Copyright 2009 Me. All rights reserved.
*
*/
char * Names[10] = {"Lisa", "Pamela", "Amanda", "Gerard", "Dan", "Patrick", "Peter", "Michael", "Sally", "Tracie"};
char * Vehicles[5] = {"Feet", " Old Bike","New Bike", "Cheap Car", "Expensive Car"};
char * Jobs[5] = {"Unemployed", "Burger World", "Junior Programmer", "Senior Programmer", "Academia"};
char * Job_Pace[3] = {"Easy Going", "9-5 Grind", "Whirl Wind"};
char * Education[4] = {"Bum", "High School", "College", "Graduate School"};
char * Work_Ethic[3] = {"Drifter", "Hard Worker", "Workaholic"};
char * Sleep_Habits[3] = {"Good", "Medium", "Poor"};
答案 0 :(得分:3)
我不确定您的问题是什么,但我会在您更新问题时尝试更新我的答案。
需要注意的一点是,您使用的是C ++(或C99)样式注释(//comment here
)。如果您使用严格的ANSI C90支持进行编译,那么这将失败。另外,不要强制转换malloc
(这源于C90及以上编译器中不存在的一个非常古老的问题)。
此外,这不是ANSI C.你有这一行:
Population[i] = new person(randomVal(Names, 10), ... /* snipped for brevity */
ANSI C中没有“new”关键字。这对于C ++来说甚至都不是正确的语法(只需看一眼就可以看出你可以放弃new
和{{1函数返回正确的东西)。
修改强>
好的,这是对评论的回应。有一个很多的方法可以做到这一点。一种简单的方法是为可以拥有特定工作的人数设置限制。然后你可以创建一个具有相同大小的Jobs数组的数组,并且每个索引到这个新数组(比如HasJob)可以将索引与Jobs数组匹配。因此,HasJob [0]可以存储每个拥有“失业”工作的人。您可以为每个HasJob条目保留一个单独的计数,因此当您插入时,您知道在哪里粘贴新的Person对象,或者您可以将所有值初始化为NULL,然后每次插入时,都进行线性搜索。
所以你可以:
person()
现在,您可以通过执行类似
的操作将每个Person插入HasJob数组中/* 5 is the number of possible Jobs and 100 is the max persons per job */
Person *HasJob[5][100];
memset(HasJob, 0, 5*100*sizeof(Person *));
所以,我没有尝试编译它,可能存在一些问题,但这是这种方式的一般过程。从本质上讲,你会完成每份工作,然后每个工作都要经过每个人;如果那个人在那份工作,在HasJob找到一个空位并将那个人放在那里。
同样,有很多方法可以做到这一点。您可以为每个作业保留计数,然后您不必进行线性扫描,但是此方法可以防止您必须跟踪并实例化这些变量。另一种方法是使用链表。这可能会给你一个想法。当然,您应该对每个常量值使用int i, j, k;
for(i=0; i < 5; i++)
{
for(j=0; j < 100; j++)
{
if(strcmp(Population[j]->job, Jobs[i]) == 0)
{
k = 0;
while(((p = HasJob[i][k]) != NULL) && (k < 100) k++;
if(k != 100) /* if k == 100, somehow too many persons have this job */
{
HasJob[i][k] = Population[j];
}
}
}
}
'(最大值,例如5和100可以是#define
,MAX_JOBS
等。