Log   |   Assignments   |   Source   |   Discussion   |   Feedback   |   About Me  |

This page lists the first version (v0.1) of the Vowel Detection program.

/*
 * Advanced Computer Architecture
 * Read a character from the user and report if it is a
 * vowel or not. The catch is, this has to be done
 * without doing any comparisons
 *
 * Author: Kurian John (CS10M035)
 * Revision History
 * ----------------
 * 2011-02-22 - v0.1
 * 	New program.
 */


#include <ncurses.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);

int main(int argc, char *argv[])
{	WINDOW *inputWindow, *resultWindow, *boundingWindow;
	int ch;

	initscr();		
	cbreak();	
	keypad(stdscr, TRUE);	
	refresh();

	boundingWindow = create_newwin (20, 55, 0, 0);
	inputWindow = create_newwin(3, 3, 5, 25);
	resultWindow = create_newwin (3, 20, 5, 30);

	move (18, 1);
	printw("Press F3 to exit");
	move (1, 18);
	printw("Vowel Detector");
	move (2, 1);
	printw("_____________________________________________________");
	move (6, 1);
	printw("Please enter a character");
	move (6, 26);
	int result=0;
	char resultMessage[2][20] = {"Not a vowel", "Vowel"};
	while((ch = getch()) != KEY_F(3))
	{
		for (result=32; result<41; result++)
		{
			move (6, result);
			delch ();
			wrefresh (resultWindow);
		}

		ch = ch | 0x20; // Make everything lower case for comparison
		result = (ch - 'a') * (ch - 'e') * (ch - 'i') * (ch - 'o') * (ch - 'u');

		// result = 0 for vowels and abs(result) > 1 for any other character
		// So, the exponent function will return 1 for vowels and a fraction
		// (which gets truncated by the type cast) for other characters
		result = (int) exp (-abs(result));

		move (6, 32 );
		printw ("%s", resultMessage[result]);
		move (6, 26 );
		refresh();
	}
	endwin();
	return 0;
}

WINDOW *create_newwin(int height, int width, int starty, int startx)
{	
	WINDOW *local_win;
	local_win = newwin(height, width, starty, startx);
	// Draw the window with default characters (line)
	box(local_win, 0 , 0);
	wrefresh(local_win);		
	return local_win;
}

void destroy_win(WINDOW *local_win)
{	
	// Clear all decorations and delete window
	wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
	wrefresh(local_win);
	delwin(local_win);
}