c1 exercises

This commit is contained in:
Michael Thomson 2024-12-11 15:27:34 -05:00
parent 1abc5b8e52
commit 6b81c1678a
No known key found for this signature in database
GPG Key ID: 8EFECCD347C72F7D
8 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.a.out</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

View File

@ -0,0 +1,7 @@
---
triple: 'arm64-apple-darwin'
binary-path: a.out
relocations:
- { offset: 0x26, size: 0x8, addend: 0x0, symName: _main, symObjAddr: 0x0, symBinAddr: 0x100003F38, symSize: 0x30 }
- { offset: 0x41, size: 0x8, addend: 0x0, symName: _main, symObjAddr: 0x0, symBinAddr: 0x100003F38, symSize: 0x30 }
...

View File

@ -0,0 +1 @@
[]

56
c1/exercises/main.c Normal file
View File

@ -0,0 +1,56 @@
#include "util.h"
#include <string.h>
#include <stdio.h>
typedef struct tree *T_tree;
struct tree {
T_tree left;
string key;
T_tree right;
};
T_tree Tree(T_tree l, string k, T_tree r)
{
T_tree t = checked_malloc(sizeof(*t));
t->left = l;
t->key = k;
t->right = r;
return t;
}
T_tree insert(string key, T_tree t)
{
if (t == NULL)
return Tree(NULL, key, NULL);
else if (strcmp(key, t->key) < 0)
return Tree(insert(key, t->left), t->key, t->right);
else if (strcmp(key, t->key) > 0)
return Tree(t->left, t->key, insert(key, t->right));
else
return Tree(t->left, key, t->right);
}
bool member(string key, T_tree t)
{
if (t == NULL) {
return FALSE;
}
if (strcmp(t->key, key) == 0) {
return TRUE;
}
return member(key, t->left) || member(key, t->right);
}
int main(int argc, char *argv[])
{
string values[] = { "t", "s", "p", "i", "p", "f", "b", "s", "t" };
T_tree tree;
for (int i = 0; i < 9; i++) {
tree = insert(values[i], tree);
}
printf("member: %d\n", member("t", tree));
}

11
c1/exercises/makefile Normal file
View File

@ -0,0 +1,11 @@
all: main.o util.o
gcc -g main.o util.o
main.o: main.c util.h
gcc -g -c main.c util.h
util.o: util.c util.h
gcc -g -c util.c
clean:
rm -f a.out util.o prog1.o slp.o main.o

16
c1/exercises/util.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdlib.h>
#include <strings.h>
#include "util.h"
string String(char *s)
{
string p = checked_malloc(strlen(s)+1);
strcpy(p,s);
return p;
}
void *checked_malloc(int len)
{
void *p = malloc(len);
assert(p);
return p;
}

16
c1/exercises/util.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef UTIL_H
#define UTIL_H
#include <assert.h>
typedef char *string;
string String(char *);
typedef char bool;
#define TRUE 1;
#define FALSE 0;
void *checked_malloc(int);
#endif // !UTIL_H

BIN
c1/exercises/util.h.gch Normal file

Binary file not shown.