GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Aug-2007, 07:28
bengt23648 bengt23648 is offline
New Member
 
Join Date: Mar 2007
Posts: 5
bengt23648 is on a distinguished road

C code to Java


hi.. i'm new in java and i'm not that familiar with the new things in java.. can you help me with this?

i have this c code and i want to have it in java, and i have NO idea what to do coz i don't know what are the equivalents of the built-in methods in c with the built-in methods in java..

thanks a lot..

CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>
#define MAX_SIZE 512

struct Node_t
{
  char oper;
  struct Node_t * left;
  struct Node_t * right;
};

struct Node_t nodes[MAX_SIZE];

char last;

struct Node_t * free_node;

char GetNextChar(void)
{
  char ret;

  do
  {
    ret = getchar();
  } while ((ret == ' ') || (ret == '\t'));
  return ret;
}

struct Node_t * E(void);

struct Node_t * T(void)
{
  struct Node_t * ret_node;

  if (last == '(')
  {
    last = GetNextChar();
    ret_node = E();
    last = GetNextChar(); /* preskocit ')' */
    return ret_node;
  }
  free_node->oper = last;
  last = GetNextChar(); /* preskocit terminator */
  return free_node++;
}

/*struct Node_t * Ec(struct Node_t * left);*/

struct Node_t * Fc(struct Node_t * left)
{
  struct Node_t * node;

  if ((last == '*') || (last == '/'))
  {
    node = free_node++;
    node->oper = last;
    last = GetNextChar();
    node->left = left;
    node->right = T();
    return Fc(node);
  }
  return left;
}

struct Node_t * F(void)
{
  struct Node_t * left_node;

  left_node = T();
  return Fc(left_node);
}

struct Node_t * Ec(struct Node_t * left)
{
  struct Node_t * node;

  if ((last == '+') || (last == '-'))
  {
    node = free_node++;
    node->oper = last;
    last = GetNextChar();
    node->left = left;
    node->right = F();
    return Ec(node);
  }
  node = Fc(left);
  if ((last == '+') || (last == '-'))
    return Ec(node);
  return node;
}

struct Node_t * E(void)
{
  struct Node_t * left_node;

  left_node = T();
  return Ec(left_node);
}

void Print(struct Node_t * node, int prior)
{
  int lpri, rpri;

/*printf("[node: %p], prior: %d\n", node, prior);*/
/*printf("  {left: %p, oper: \'%c\', right: %p}\n", node->left, node->oper, node->right);*/
  switch(node->oper)
  {
  case '+':
    lpri = rpri = 2;
    break;
  case '-':
    lpri = 2;
    rpri = 1;
    break;
  case '*':
    lpri = rpri = 1;
    break;
  case '/':
    lpri = 1;
    rpri = 0;
    break;
  default:
    putchar(node->oper);
    return;
  }
  if (lpri > prior)
    putchar('(');
  Print(node->left, lpri);
  putchar(node->oper);
  Print(node->right, rpri);
  if (lpri > prior)
    putchar(')');

}

int main(void)
{
  struct Node_t * node;
  int n;
  clrscr();
  scanf("%d\n", &n);
  while (n--)
  {
    last = GetNextChar();
    free_node = nodes;
    node = E();
    Print(node, 4);
    printf("\n");

  }
  return 0;
  getch();
}
Last edited by admin : 07-Sep-2007 at 02:13. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 18-Aug-2007, 14:45
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 955
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: c code to java


Look around online for Java linked list examples, such as this user-created example:

http://www.java2s.com/Code/Java/Coll...istexample.htm

EDIT:
Note too, that Java has it's own LinkedList class [in utilities], but it's NOT thread-safe by default, see here:
http://java.sun.com/j2se/1.4.2/docs/api/index.html
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 09-Sep-2007, 05:33
alcedo's Avatar
alcedo alcedo is offline
Member
 
Join Date: Jul 2005
Location: Singapore
Posts: 198
alcedo will become famous soon enough

Re: C code to Java


i rmb theres an online tool that helps you convert from C to java, do google it out
__________________
People should read the rules and regulation before posting!

The Best is yet to be...
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
To post messages / click Buttons of a Java Jar App using code Jun0 C Programming Language 1 06-Jan-2007 14:44
To post messages / click Buttons of a Java Jar App using code Jun0 Java Forum 0 06-Jan-2007 11:14
How to sort random access file? wmmccoy0910 C Programming Language 12 04-Sep-2006 03:40
Here it is again! 35% - 40% off For Life! my-e-space Web Hosting Advertisements & Offers 0 20-Apr-2006 14:48
Problem with int mixed with char,... leitz C++ Forum 17 07-Dec-2004 20:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:04.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.