3 Replies - 2807 Views - Last Post: 01 March 2009 - 06:33 PM Rate Topic: -----

#1 chetah  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 17-November 07

Convert From C to Java

Post icon  Posted 01 March 2009 - 11:22 AM

#include <stdio.h>
#define True 1
#define False 0
#define WordFound 1
#define NewNode 1
#define OldNode 0
#define AlreadyPrinted 2
#define NoMoreWords 0
#define MaxWordSize 20
#define MaxLine 256

typedef struct nodeData {
	char word[MaxWordSize+1];  // '+1' to store '\0'
} NodeData;

NodeData makeNodeData(char str[]) {
	NodeData temp;
	strcpy(temp.word, str);
	return temp;
} //end makeNodeData

int compareTo(NodeData a, NodeData b) {
	return strcmp(a.word, b.word);
}

typedef struct treeNode {
	NodeData data;
	int status;
	struct treeNode *left, *right, *nextSynonym;
} TreeNode, *TreePtr;

TreePtr makeTreeNode(NodeData d) {
//creates a new tree node; returns pointer to node
	TreePtr new = (TreePtr) malloc(sizeof(TreeNode));
	new -> data = d;
	new -> left = NULL;
	new -> right = NULL;
	new -> nextSynonym = NULL;
	new -> status = NewNode;
	return new;
} //end makeTreeNode

FILE * in;
FILE * out;

main() {
	char line[MaxLine];
	TreePtr root, processLine(char [], TreePtr);
	void inOrderTraversal(TreePtr root);
	void inOrderTraversalNoRepeat(TreePtr root);

	in = fopen("input.txt", "r");
	out = fopen("thesaurus.out", "w");
	int currentLine = 0;

	root = NULL;
	while (fgets(line, MaxLine, in) != NULL) {
		fprintf(out, "%3d. %s", ++currentLine, line);
		root = processLine(line, root);
	}
	fprintf(out, "\nWords				Synonyms\n\n");
	inOrderTraversal(root);
	fprintf(out, "\n\nWords				Synonyms\n\n");
	inOrderTraversalNoRepeat(root);
	fclose(in);
	fclose(out);
} //end main

TreePtr processLine(char line[], TreePtr root) {
	TreePtr findOrInsert(TreePtr, NodeData);
	int getWord(char[], int, char[]);
	char newWord[MaxWordSize+1];

	TreePtr thisList = NULL, last;
	int joined = False;
	while (getWord(newWord, MaxWordSize, line) == WordFound) {
		TreePtr node = findOrInsert(root, makeNodeData(newWord));
		if (root == NULL) root = node;
		if (node -> status == NewNode) {
			if (thisList == NULL) thisList = node;
			else last -> nextSynonym = node;
			last = node;
			node -> nextSynonym = thisList; //last node points to first
		}
		else { //word already in tree
			if (thisList == NULL) {
				thisList = node -> nextSynonym;
				last = node;
				joined = True;
			}
			else if (!joined) {
				last -> nextSynonym = node -> nextSynonym;
				node -> nextSynonym = thisList;
				last = node;
			}
		} //end if
	} //end while
	return root;
} //end processLine

int getWord(char word[], int wmax, char buffer[]) {
//get the next word from buffer; convert to lowercase
	static int bpos = 0;

	while (buffer[bpos] != '\0' && !isalpha(buffer[bpos])) ++bpos;

	if (buffer[bpos] == '\0') {
		bpos = 0;
		return 0;
	}

	//The first letter of the word has been found
	int j = 0;
	word[j++] = tolower(buffer[bpos]);
	while (isalpha(buffer[++bpos]))
		if (j < wmax) word[j++] = tolower(buffer[bpos]);
	word[j] = '\0';
	return WordFound;
} //end getWord

TreePtr findOrInsert(TreePtr root, NodeData d) {
//Searches the tree for d; if found, returns a pointer to the node.
//If not found, it is added and a pointer to the new node returned.
	TreePtr curr, new, makeTreeNode(NodeData);
	int cmp;

	if (root == NULL) return makeTreeNode(d);
	curr = root;
	while ((cmp = compareTo(d, curr -> data)) != 0) {
		if (cmp < 0) { //try left
			if (curr -> left == NULL) return curr -> left = makeTreeNode(d);
			curr = curr -> left;
		}
		else { //try right
			if (curr -> right == NULL)  return curr -> right = makeTreeNode(d);
			curr = curr -> right;
		}
	}
	//d is in the tree; set 'status' and return pointer to the node
	curr -> status = OldNode;
	return curr;
} //end findOrInsert

void inOrderTraversal(TreePtr root) {
	void visit(TreePtr nodeptr);

	if (root != NULL) {
		inOrderTraversal(root -> left);
		visit(root);
		inOrderTraversal(root -> right);
	}
} //end inOrderTraversal

void inOrderTraversalNoRepeat(TreePtr root) {
	void visit1(TreePtr nodeptr);

	if (root != NULL) {
		inOrderTraversalNoRepeat(root -> left);
		visit1(root);
		inOrderTraversalNoRepeat(root -> right);
	}
} //end inOrderTraversalNoRepeat

typedef struct listNode {
	char word[MaxWordSize+1];  // '+1' to store '\0'
	struct listNode *next;
} ListNode, *ListPtr;

ListPtr makeListNode(char str[]) {
	ListPtr new = (ListPtr) malloc(sizeof(ListNode));
	strcpy(new -> word, str);
	new -> next = NULL;
	return new;
} //end makeListNode


void visit(TreePtr node) {
//prints a word followed by its synonyms
	ListPtr addInPlace(ListPtr, char[]);
	fprintf(out, "%-20s ", node -> data.word);
	TreePtr curr = node -> nextSynonym;

	//create a sorted linked list of synonyms
	ListPtr top = NULL;
	while (curr != node) { //circular list
		top = addInPlace(top, curr -> data.word);
		curr = curr -> nextSynonym;
	}

	//print and erase nodes in the list
	while (top != NULL) {
		fprintf(out, "%s ", top -> word);
		ListPtr hold = top;
		top = top -> next;
		free(hold);
	}
	fprintf(out, "\n");
} //end visit

void visit1(TreePtr node) {
//prints a word followed by its synonyms; words are not repeated
	ListPtr addInPlace(ListPtr, char[]);

	if (node -> status == AlreadyPrinted) return;

	fprintf(out, "%-20s ", node -> data.word);
	node -> status = AlreadyPrinted;
	TreePtr curr = node -> nextSynonym;

	//create a sorted linked list of synonyms
	ListPtr top = NULL;
	while (curr != node) { //circular list
		top = addInPlace(top, curr -> data.word);
		curr -> status = AlreadyPrinted;
		curr = curr -> nextSynonym;
	}

	//print and erase nodes in the list
	while (top != NULL) {
		fprintf(out, "%s ", top -> word);
		ListPtr hold = top;
		top = top -> next;
		free(hold);
	}
	fprintf(out, "\n");
} //end visit

ListPtr addInPlace(ListPtr top, char str[]) {
//add a node to a sorted linked list
	ListPtr prev = NULL, curr = top;
	while (curr != NULL && strcmp(str, curr -> word) > 0) {
		prev = curr;
		curr = curr -> next;
	}
	ListPtr nn = makeListNode(str);
	nn -> next = curr;
	if (prev == NULL) return nn;
	prev -> next = nn;
	return top;
}



Need help in converting the above to java

Is This A Good Question/Topic? 0
  • +

Replies To: Convert From C to Java

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Convert From C to Java

Posted 01 March 2009 - 11:33 AM

it may be easier first to convert it to C++ replacing char[] with string, pointers with references and implementing NodeData, TreeNode, etc as classes

you could always use a C to Java converter
http://www.download....4-10080009.html

This post has been edited by horace: 01 March 2009 - 11:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Convert From C to Java

Posted 01 March 2009 - 11:35 AM

The biggest things to change are the input output stuff and the objects. If you don't know any java, converting the program is going to be next to impossible. The structs will have to be rewritten as objects, file io will have to be streamed, and console io is done will System.out.println() etcetera. Try looking at some of the differences between C++ and Java as there are several lists of them on this site, which should help you in the rewriting. Thank goodness all the if and loop stuff is the same...

Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#4 chetah  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 17-November 07

Re: Convert From C to Java

Posted 01 March 2009 - 06:33 PM

View Postcrazyjugglerdrummer, on 1 Mar, 2009 - 10:35 AM, said:

The biggest things to change are the input output stuff and the objects. If you don't know any java, converting the program is going to be next to impossible. The structs will have to be rewritten as objects, file io will have to be streamed, and console io is done will System.out.println() etcetera. Try looking at some of the differences between C++ and Java as there are several lists of them on this site, which should help you in the rewriting. Thank goodness all the if and loop stuff is the same...

Hope this helps!


Thanks very much for your advise, but I am not getting the program to work. I read the instructions thoroughtly, but it keeps giving an error:
C2jpp.exe:\s5uo.1:error:Permission denied

Looking forward for further assistance
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1