/* demo.c * A demo C program to explore machine level code including * a recursive function and a binary data structure on the heap. * See ./_readme_demo.txt for all the details. * Jim Mahoney | Oct 2020 | cs.bennington.college | MIT License */ #include #include #include typedef struct _node *node; // a "node" is a pointer to "struct _node" struct _node { // a "struct _node" contains a value and a node. char name; node right; node left; }; node new_node(char name, node left, node right){ // Create and return a new node. node result = malloc(sizeof(struct _node)); result->name = name; // same as (*result).value = value result->left = left; result->right = right; return result; } node make_tree(){ // Create and return the root of a binary tree that looks like this: // a // / \ // b c // / \ // d e node e = new_node('e', NULL, NULL); node d = new_node('d', NULL, NULL); node c = new_node('c', d, e); node b = new_node('b', NULL, NULL); node a = new_node('a', b, c); return a; } void print_tree(node root){ // print a tree as (name node node), e.g. (a (b) (c (e) (f)))" if (root){ printf(" (%c", root->name); print_tree(root->left); print_tree(root->right); printf(")"); } } int fibbo(int n){ if (n < 2) return 1; return fibbo(n-1) + fibbo(n-2); } void swap(int *xp, int *yp){ int t0, t1; t0 = *xp; t1 = *yp; *xp = t1; *yp = t0; } int swap_and_stuff(int* a_ptr, int* b_ptr, int c, int d, int e, int f, int g){ int result; swap(a_ptr, b_ptr); result = c * (*a_ptr) + (*b_ptr) + (3 * d + 4 * e + 5 * f + 6 * g); return result; } void part0(){ char input[100]; int n_inputs; char input_format[] = "%s"; printf("-- part 0 --\n"); printf(" What is your favorite color? "); n_inputs = scanf(input_format, input); if (n_inputs == 1){ printf(" You said '%s'. \n", input); } else { printf(" Oops: scanf error \n"); } } void part1(){ int n1 = 12, n2 = 34, n3 = 5, n4; printf("-- part1 --\n"); printf(" before: n1 = %d, n2 = %d, n3 = %d, n4 = %d \n", n1, n2, n3, n4); n4 = swap_and_stuff(&n1, &n2, n3, 1+n1, 2+n2, 3+n3, 13); printf(" after: n1 = %d, n2 = %d, n3 = %d, n4 = %d \n", n1, n2, n3, n4); } void part2(){ int f5 = fibbo(5); printf("-- part2 --\n"); printf(" fibbo(5) is %d \n", f5); } void part3(){ printf("-- part3 --\n"); node tree = make_tree(); print_tree(tree); printf("\n"); } int main(){ printf("It's demo.c !!!\n"); part0(); part1(); part2(); part3(); printf("(Are we having fun yet?)\n"); return 0; }