Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   convert c code to a java code (http://www.programmingforums.org/showthread.php?t=13787)

bengt23648 Aug 17th, 2007 9:40 AM

convert c code to a java code
 
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..

:

#include <iostream.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();
}



the code above does this task:

it opens a txt file and then does this..

Sample Input

8 //test cases or the # of lines to be changed
(a+(b*c))
((a+b)*c)
(a*(b*c))
(a*(b/c)*d)
((a/(b/c))/d)
((x))
(a+b)-(c-d)-(e/f)
(a+b)+(c-d)-(e+f)


Sample Output

a+b*c
(a+b)*c
a*b*c
a*b/c*d
a/(b/c)/d
x
a+b-(c-d)-e/f
a+b+c-d-(e+f)



can you help me with this..

bengt23648 Aug 17th, 2007 9:44 AM

help pls.. thanks

ReggaetonKing Aug 17th, 2007 10:41 AM

Why don't you find a book on Java and start from there!!

jaeusm Aug 17th, 2007 4:25 PM

Why do you want this code in Java? Is this an assignment?

alcdotcom Aug 19th, 2007 10:42 AM

Start here and then, as you learn, try to convert your program. It will help you learn Java in the process.

Fall Back Son Sep 4th, 2007 2:18 PM

There is no automatic conversion method. Java is based on objects & methods/actions you can take on those objects, the interaction of objects, etc, whereas C is based on data and manipulating data using functions. Basically, I'm saying the same as everybody else: you're going to have to learn some Java before you can 'convert' your program, because the two languages are different, although they may have similar features.


All times are GMT -5. The time now is 2:55 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC