c code to java

Discussion in 'Java' started by bengt23648, Aug 17, 2007.

Thread Status:
Not open for further replies.
  1. bengt23648

    bengt23648 New Member

    Joined:
    Mar 20, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    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..
    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();
    }
    
    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..
     
    Last edited by a moderator: Aug 18, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
Thread Status:
Not open for further replies.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice