From ce9248385c8d02141fb9eee7ea14894d4f323686 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Fri, 4 Mar 2011 18:40:07 -0500 Subject: backup of stuffs --- libraries | 11 +++++ notes | 10 +++++ rpn.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ test_ascii_enum.c | 21 +++++++++ 4 files changed, 171 insertions(+) create mode 100644 libraries create mode 100644 notes create mode 100644 rpn.c create mode 100644 test_ascii_enum.c diff --git a/libraries b/libraries new file mode 100644 index 0000000..e570fe0 --- /dev/null +++ b/libraries @@ -0,0 +1,11 @@ + +diet libc +http://www.fefe.de/dietlibc/ + +libowfat (is libdjb...) +http://www.fefe.de/libowfat/ + +"standard function library" + +glibc + diff --git a/notes b/notes new file mode 100644 index 0000000..8c4954c --- /dev/null +++ b/notes @@ -0,0 +1,10 @@ + +Can use 'const' in function parameters to indicate to the compiler that a +variable will not be modified. + +The order of evaluation (function calls) in an expression is undefined. Eg: + + push(pop() + pop()) is ok + push(pop() - pop()) is NOT ok + + diff --git a/rpn.c b/rpn.c new file mode 100644 index 0000000..d1c72ad --- /dev/null +++ b/rpn.c @@ -0,0 +1,129 @@ +/* rpn.c + * + * Takes a string of reverse polish notation math and pretty-prints it with + * parenthesis. Recognizes the {*, +, -, /} operators and ignores everything + * else. + * + */ + +/* +- tree data structure +- read in integer function (std?) + +*/ + +#include +#include +#include + +#define LINE_LEN 1024 + +struct rpn_node { + char val; + struct rpn_node *left; + struct rpn_node *right; +}; + +int rpn_parse(char s[], struct rpn_node *node); +void rpn_print(struct rpn_node *node); +int rpn_eval(struct rpn_node *node); +int isoper(char c); + +int main(void) { + char line[LINE_LEN] = ""; + struct rpn_node root_node; + char tc; + int i,j; + + // read string + fgets(line, LINE_LEN, stdin); + + // reverse it + j = strlen(line) - 1; + for(i = 0; i <= j/2; i++) { + tc = line[i]; + line[i] = line[j-i]; + line[j-i] = tc; + } + + // parse it down to single char numbers and opers + for(i = j = 0; i < strlen(line); i++) { + if(isdigit(line[i]) || isoper(line[i])) + line[j++] = line[i]; + } + line[j] = '\0'; + //printf("Compressed: %s\n", line); + + // recursively parse down and store result in right; then recursively parse + // down and store result in left + rpn_parse(line, &root_node); + + // spider down the tree to the left, printing + rpn_print(&root_node); + printf(" = %d\n", rpn_eval(&root_node)); + return; + // &new_node = malloc(sizeof(struct node)) +} + +// returns the number of characters that got chomped up +int rpn_parse(char s[], struct rpn_node *node) { + int i=0; + + node->val = s[0]; + i++; + //printf("Got len=%d, isoper=%d: %s\n", (int)strlen(s), isoper(s[0]), s); + + if(isoper(s[0])) { + //printf("Got an oper: %c\n", s[0]); + // load up right and left + node->right = malloc(sizeof(struct rpn_node)); + node->left = malloc(sizeof(struct rpn_node)); + i += rpn_parse(&s[i], node->right); + i += rpn_parse(&s[i], node->left); + } + return i; +} + +void rpn_print(struct rpn_node *node) { + if(isoper(node->val)) { + printf("("); + rpn_print(node->left); + printf(" %c ", node->val); + rpn_print(node->right); + printf(")"); + } else { + printf("%c", node->val); + } +} + +int isoper(char c) { + switch(c) { + case '+': + case '-': + case '*': + case '/': + return 1; + break; + default: + return 0; + } +} + +int rpn_eval(struct rpn_node *node) { + if(isdigit(node->val)) { + return node->val - '0'; + } + switch(node->val) { + case '+': + return rpn_eval(node->left) + rpn_eval(node->right); + case '-': + return rpn_eval(node->left) - rpn_eval(node->right); + case '*': + return rpn_eval(node->left) * rpn_eval(node->right); + case '/': + return rpn_eval(node->left) / rpn_eval(node->right); + default: + printf("ERROR: unexpected character '%c'.", node->val); + exit(-1); + } +} diff --git a/test_ascii_enum.c b/test_ascii_enum.c new file mode 100644 index 0000000..55a5724 --- /dev/null +++ b/test_ascii_enum.c @@ -0,0 +1,21 @@ + +#include +#include + +enum { FIRST, SECOND, THIRD }; + +char list[] = "abcABC123(){}"; + +void main(void) { + int i; + printf("EOF: 0x%X\n", EOF); + printf("FIRST: 0x%X\n", FIRST); + printf("SECOND: 0x%X\n", SECOND); + printf("THIRD: 0x%X\n", THIRD); + for(i = 0; i <= strlen(list); i++) + printf("list[%d]: %c = 0x%X\n", i, list[i], list[i]); + + printf("Does list[%d] == FIRST? %s!\n", (int)strlen(list), + list[strlen(list)] == FIRST ? "YES" : "NO"); +} + -- cgit v1.2.3