






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







//Gabriel Martinez //CPSC 223C Assignment 3 #pragma once #ifndef wlnode_h #define wlnode_h #include <stdio.h> #include <stdbool.h> struct wlnode { char* word; int count; int linenumbers; int prev_lineno; char* list_lineno; struct wlnode* left; struct wlnode* right; }; typedef struct wlnode wlnode; #define MAXLINE 1000 char* rtrim(char* p); void strcpy_(char* dest, char* src); char* strdup_(char* s); int strcmp_(char* s1, char* s2); char* strcat_(char * dest, char* src); int strlen_(char* s); bool checknoise(char* w); wlnode* wlalloc(); wlnode* wlnodedup(wlnode* other); wlnode* addtree_wl(wlnode* p, char* w, int lineno); wlnode* build_tree_wl();
void inorder_traversal_wl(wlnode* p, wlnode* array[], int* pi); void printtreenode_wl(wlnode* p); void printtree_wl(wlnode* p); // shellsort: sort v[0], v[1], ..., v[n-1] in ascending order void shellsort_wlnode(wlnode* v[], int n); int comp_wlnode(wlnode* lhs, wlnode* rhs); void test_lineread(); int getch(); void ungetch(int c); int getword(char* word, int lim); void itoa_(int n, char s[]); void reverse(char* s); #endif /* wlnode_h */ #include "wlnode.h" #include <stdbool.h> #include <string.h> #include <ctype.h> #include <stdlib.h> typedef struct wlnode wlnode; #define NUMNOISEWORDS 17 #define BUFSIZE 100 #define MAXWORD 100
return p; } int strcmp_(char* s1, char* s2) { for (; s1 == s2; s1++, s2++) if (s1 == '\0') { return 0; } return (s1 - s2); } char strcat_(char *dest, char src) { size_t i, j; for (i = 0; dest[i] != '\0'; i++){ for (j = 0; src[j] != '\0'; j++) { dest[i + j] = src[j]; } } dest[i + j] = '\0'; return dest; } int strlen_(char s) { char p = s; while (p != '\0') { p++; } return (p - s); }
bool checknoise(char * w) { for (int a = 0; a < NUMNOISEWORDS; a++) { if (strcmp_(w, NOISEWORDS[a]) == 0) { return true; } } return false; } wlnode * wlalloc() { return (wlnode*)malloc(sizeof(wlnode)); } wlnode * wlnodedup(wlnode * other){ wlnode * newnode = wlalloc(); newnode->count = other->count; newnode->left = other->left; newnode->right = other->left; newnode->linenumbers = other->linenumbers; newnode->prev_lineno = other->prev_lineno; newnode->word = strdup_(other->word); newnode->list_lineno = strdup_(other->list_lineno); return newnode; } wlnode * addtree_wl(wlnode * p, char * w, int lineno) { int cond; char buf[100];
p->right = addtree_wl(p->right, w, lineno); } return p; } wlnode * build_tree_wl() { wlnode root = NULL; char word[100]; while (getword(word, MAXWORD) != EOF) { if (isalpha(word[0]) && !checknoise(word)) { root = addtree_wl(root, word, linenum); } } return root; } void inorder_traversal_wl(wlnode * p, wlnode * array[], int * pi) { if (p == NULL) { return; } inorder_traversal_wl(p->left, array, pi); array[(pi)++] = p; inorder_traversal_wl(p->right, array, pi); } void printtreenode_wl(wlnode * p) {printf("%d: %-7s%s\n", p->count, p->word, p->list_lineno);} void printtree_wl(wlnode * p) { if (p != NULL) {
printtree_wl(p->left); printtreenode_wl(p); printtree_wl(p->right); } } void shellsort_wlnode(wlnode * v[], int n) { int gap, i, j; wlnode * temp; for (gap = n / 2; gap > 0; gap /= 2) { for (i = gap; i < n; i++) { for (j = i - gap; j >= 0 && v[j]->linenumbers<v[j + gap]->linenumbers; j - = gap) { temp = v[j]; v[j] = v[j + gap]; v[j + gap] = temp; } } } } int comp_wlnode(wlnode * lhs, wlnode * rhs) { return (lhs->linenumbers - rhs->linenumbers); } void test_lineread() { printf("================TESTING lineread===============\n"); wlnode *wlnodes[500]; wlnode root = build_tree_wl(); int sizeNodeArray = sizeof(wlnodes) / sizeof(wlnode);
void itoa_(int n, char s[]) { int i, sign; if ((sign = n) < 0) { n = - n; } i = 0; do { s[i++] = n % 10 + '0'; } while ((n /= 10) > 0); if (sign < 0) { s[i++] = '-'; } s[i] = '\0'; reverse(s); } int getch() { return ((bufp > 0)? buf[--bufp] : getchar()); } void ungetch(int c) { if (bufp >= BUFSIZE) { printf("ungetch: too many characters\n"); } else { buf[bufp++] = c; }
int getword(char* word, int limit) { int c; char* w = word; while (isspace(c = getch())) if (c == '\n') ++linenum; if (c != EOF) w++ = c; if (!isalpha(c)) { w = '\0'; return c; } for (; --limit > 0; w++) if (!isalnum(w = getch())) { ungetch(w); break; } *w = '\0'; return word[0]; } #include "wlnode.h" int main() { test_lineread(); return 0; }