【cs 50】lab5 problemset5
(1)lab5 - inheritance
// Simulate genetic inheritance of blood type #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <time.h> // Each person has two parents and two alleles typedef struct person { struct person *parents[2]; char alleles[2]; } person; const int GENERATIONS = 3; const int INDENT_LENGTH = 4; person *create_family(int generations); void print_family(person *p, int generation); void free_family(person *p); char random_allele(); int main(void) { // Seed random number generator srand(time(0)); // Create a new family with three generations person *p = create_family(GENERATIONS); // Print family tree of blood types print_family(p, 0); // Free memory free_family(p); } // Create a new individual with `generations` person *create_family(int generations) { // TODO: Allocate memory for new person person *new = malloc(sizeof(person)); //char mid = "0"; int random = 0; // If there are still generations left to create if (generations > 1) { // Create two new parents for current person by recursively calling create_family person *parent0 = create_family(generations - 1); person *parent1 = create_family(generations - 1); // TODO: Set parent pointers for current person new->parents[0] = parent0; new->parents[1] = parent1; // TODO: Randomly assign current person's alleles based on the alleles of their parents //mid = alleles[1]; random = rand()%2; new->alleles[0] = parent0->alleles[random]; random = rand()%2; new->alleles[1] = parent1->alleles[random]; } // If there are no generations left to create else { // TODO: Set parent pointers to NULL new->parents[0] = NULL; new->parents[1] = NULL; // TODO: Randomly assign alleles new->alleles[0] = random_allele(); new->alleles[1] = random_allele(); } // TODO: Return newly created person return new; // return NULL; } // Free `p` and all ancestors of `p`. void free_family(person *p) { // TODO: Handle base case if(p == NULL){ return; } // TODO: Free parents recursively free_family(p->parents[0]); free_family(p->parents[1]); // TODO: Free child free(p); } // Print each family member and their alleles. void print_family(person *p, int generation) { // Handle base case if (p == NULL) { return; } // Print indentation for (int i = 0; i < generation * INDENT_LENGTH; i++) { printf(" "); } // Print person if (generation == 0) { printf("Child (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]); } else if (generation == 1) { printf("Parent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]); } else { for (int i = 0; i < generation - 2; i++) { printf("Great-"); } printf("Grandparent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]); } // Print parents of current generation print_family(p->parents[0], generation + 1); print_family(p->parents[1], generation + 1); } // Randomly chooses a blood type allele. char random_allele() { int r = rand() % 3; if (r == 0) { return 'A'; } else if (r == 1) { return 'B'; } else { return 'O'; } }
(2)problemset5 speller
// Implements a dictionary's functionality #include <ctype.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include "dictionary.h" // Represents a node in a hash table typedef struct node { char word[LENGTH + 1]; struct node *next; } node; // TODO: Choose number of buckets in hash table const unsigned int N = 26; unsigned int count = 0; // Hash table node *table[N]; // Returns true if word is in dictionary, else false bool check(const char *word) { // TODO char copy[strlen(word)+1]; for(int i=0;i<strlen(word);i++){ copy[i] = tolower(word[i]); } int n = hash(copy); node *tmp = table[n]; /*for(node *tmp = table[n];tmp!=NULL;tmp = tmp->next){ if(strcasecmp(tmp->word,word)==0){ return true; } else{ continue; } }*/ while(tmp!=NULL){ if(strcasecmp(word,tmp->word)==0){ return true; } tmp = tmp->next; } return false; } // Hashes word to a number unsigned int hash(const char *word) { // TODO: Improve this hash function return toupper(word[0]-'A'); } // Loads dictionary into memory, returning true if successful, else false bool load(const char *dictionary) { // TODO FILE *dic = fopen(dictionary,"r"); if(dic == NULL){ return false; } char c; int index = 0; int n = 0; char dic_word[LENGTH+1]; while (fscanf(dic,"%s",dic_word)!=EOF){ node* h = malloc(sizeof(node)); if(h==NULL){ return false; } strcpy(h->word,dic_word); h->next = NULL; n = hash(dic_word); table[n] = h; //free(h); count++; } fclose(dic); //free(h); return true; } // Returns number of words in dictionary if loaded, else 0 if not yet loaded unsigned int size(void) { // TODO return count; } // Unloads dictionary from memory, returning true if successful, else false bool unload(void) { // TODO for(int i=0;i<N;i++){ while(table[i]!=NULL){ node *temp = table[i]->next; free(table[i]); table[i] = temp; } } return true; }
提示: