1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
typedef struct Bunny Bunny;
struct Bunny{
//specified attributes
//char *gender;
char gender[8] ;
// char *name;
char name[16] ;
// char *color;
char color[16] ;
bool radioactive_mutant_vampire;
int age;
// ptobunny *next;
Bunny* next ;
bool alive;
};
const char *genders() // literal string is const
{
static const char* gender_str[] = { "Male", "Female" } ;
return gender_str[ rand() % 2 ] ;
}
const char *names( const char *gender )
{
if( strcmp( gender, "Male" ) == 0 ) // compare strings with strcmp
{
static const char* name_str[] = { "Thumper", "Jack", "Bugs", "Julius" } ;
static const int num_names = sizeof(name_str) / sizeof( name_str[0] ) ;
return name_str[ rand() % num_names ] ;
}
else
{
static const char* name_str[] = { "Pink", "Lulu", "Fluffy", "Mini" } ;
static const int num_names = sizeof(name_str) / sizeof( name_str[0] ) ;
return name_str[ rand() % num_names ] ;
}
}
const char *colors()
{
static const char* clr_str[] = { "white", "brown", "black", "spotted" } ;
static const int num_clrs = sizeof(clr_str) / sizeof( clr_str[0] ) ;
return clr_str[ rand() % num_clrs ] ;
}
bool radioActivity() { return rand()%100 < 20 ; } // 20%
Bunny* newbunny()
{
Bunny* pb = malloc( sizeof(Bunny) ) ; // allocate memory
// initialise bunny
strcpy( pb->gender, genders() ) ;
strcpy( pb->name, names(pb->gender) ) ;
strcpy( pb->color, colors() ) ;
pb->radioactive_mutant_vampire = radioActivity() ;
pb->age = 0 ;
pb->next = NULL ;
pb->alive = true ;
return pb ;
}
int main()
{
srand((unsigned)time(NULL));
// create 11 bunnies
Bunny* first = newbunny() ;
int i = 0 ;
for( Bunny* p = first ; i<10 ; ++i )
{
p->next = newbunny() ;
p = p->next ;
}
// print out info
for( Bunny* p = first ; p != NULL; p = p->next )
{
const char* mutant = p->radioactive_mutant_vampire ? "Radioactive" : "" ;
printf( "%s %s %s %s\n", p->name, p->color, p->gender, mutant ) ;
}
// free the memoty
Bunny* p = first ;
while( p != NULL )
{
Bunny* next = p->next ;
free(p) ;
p = next ;
}
return 0 ;
}
|