
/* 
(c) 1995 by Dan Goldwater.  12/16/95
computes word value for a word puzzle contest.

reads input from stdin.
*/

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>



/*** this defines the value of each letter ***/

// puzzle 1: "video game" from Pandemonium, Inc.
// enum {NUL = 0, f,u,a,i,o, e,y,x,d,g, s,m,b,l,r, v,k,z,t,c, n,p,j,h,w, q};

// puzzle 2: "media play" from Pandemonium, Inc.
// enum {NUL = 0, x,p,u,y,l, a,o,i,e,m, v,b,j,k,q, t,r,z,h,c, d,s,n,w,f, g};

// puzzle 3: "computer" from Pandemonium, Inc.
enum {NUL = 0, v,g,k,a,u, y,e,i,b,o, x,f,d,n,q, m,r,c,w,z, t,s,l,p,h, j};



// lookup array for letter values
int letter_value[26] = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};

int main(int argc, char *argv[])
{
	int i;
	char str1[40];			// input word before converting to lowercase
	char *word1;			// the input word
	int min_value = 1;		// filters out nul words.
	int max_value = 1<<30;		// a pretty long word...
	float min_ratio = -1.0;
	float max_ratio = 100.0;	// no ratio should be higher than 26.0
	int print_word = 0;			// print the input word
	int print_value = 0;		// print the value of the input word
	int print_ratio = 0;		// print the value of input word per letter in length
	int word_value;
	int word_length;
	float word_ratio;
	char *stopstr;		// for command line parsing
	int word_is_ok;
	
	/* inputs must be valid */
	for(i=1; i<argc; i++)
	{
		if (!strcmp(argv[i],"-min"))
			min_value = strtol(argv[i+1], &stopstr, 10);

		if (!strcmp(argv[i],"-max"))
			max_value = strtol(argv[i+1], &stopstr, 10);

		if (!strcmp(argv[i],"-minr"))
			min_ratio = strtod(argv[i+1], &stopstr);

		if (!strcmp(argv[i],"-maxr"))
			max_ratio = strtod(argv[i+1], &stopstr);

		if (!strcmp(argv[i],"-w"))
			print_word = 1;

		if (!strcmp(argv[i],"-v"))
			print_value = 1;

		if (!strcmp(argv[i],"-r"))
			print_ratio = 1;
	}	
	
	if ((!print_word) & (!print_value))
	{
		print_word = 1;
		print_value = 1;
	}

	i=0;
	while(!feof(stdin))
	{
		i++;
		scanf("%s", str1);
		
		word1 = _strlwr(_strdup(str1));  // convert to lower case
		word_length = strlen(word1);

		/* compute value and ratio of the word */
		word_value = 0;
		for(i=0; i<word_length; i++)
		{
			word_value += letter_value[word1[i] - 97];  // 97 is the ascii value of "a"
		}
		word_ratio = (word_value*(1.0/word_length));	// (value/length)

		/* check if word is within specified min and max values */
		if ((word_value >= min_value) & \
			(word_value <= max_value) & \
			(word_ratio >= min_ratio) & \
			(word_ratio <= max_ratio))
			word_is_ok = 1;
		else
			word_is_ok = 0;

		/* print output */
		if (word_is_ok)
		{
			if (print_word)
				printf("%s ", word1);
			if (print_value)
				printf("%u ", word_value);
			if (print_ratio)
				printf("%3.2f ", word_ratio);
			printf("\n");
		}
		_strset(str1, 0);
	}
	return 0;
}
