All pastes #2049209 Raw Edit

Unnamed

public text v1 · immutable
#2049209 ·published 2011-04-22 03:41 UTC
rendered paste body
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "wordCount.h"


#define MAXWORD 100



int main(int argc, char **argv) 
	{
	FILE* input;
	int Result;

	if (argc > 1)
		{
		input = fopen(argv[1], "r");

		if (!input)
			{
			fprintf(stderr, "Unable to open input file \"%s\". Exiting.\n", argv[1]);
			return -1;
			}
		}
	else
		{
		fprintf(stderr, "No input file specified on command line.\n");
		return -1;
		}
	

	char word[MAXWORD] = "";
	int rc;
	struct tnode *root = NULL;
	while ((rc = fscanf(input, "%31s", word)) != EOF)
		{
		if (rc)
			{
		
		//printf ("%s\n", word);

			if (isalpha(word[0]))
				root = addtree(root, word);
		
		
			
			}
		}
	Result = treeprint(root);
	printf("%i", Result);

	return 0;
	}







/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w)
	{
		
		int cond;
		if (p == NULL)
			 { 	/* a new word has arrived */
				p = talloc(); /* make a new node */
				p->word = strdup(w);
				p->count = 1;
				p->left = p->right = NULL;				
			} 
		else if ((cond = strcmp(w, p->word)) == 0)
			p->count++; /* repeated word */
		else if (cond < 0) /* less than into left subtree */
			p->left = addtree(p->left, w);
		else /* greater than into right subtree */
			p->right = addtree(p->right, w);
		return p;
	}

/* treeprint: in-order print of tree p */
int treeprint(struct tnode *p)
	{
            static int uniqueCount = 0;
		if (p != NULL) 
			{
				treeprint(p->left);
				printf("%4d %s\n", p->count, p->word);
				treeprint(p->right);
			}
            uniqueCount++;
            return uniqueCount;
	}