Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 17th, 2007, 8:40 AM   #1
bengt23648
Newbie
 
Join Date: Aug 2007
Posts: 6
Rep Power: 0 bengt23648 is on a distinguished road
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 is offline   Reply With Quote
Old Aug 17th, 2007, 8:44 AM   #2
bengt23648
Newbie
 
Join Date: Aug 2007
Posts: 6
Rep Power: 0 bengt23648 is on a distinguished road
help pls.. thanks
bengt23648 is offline   Reply With Quote
Old Aug 17th, 2007, 9:41 AM   #3
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Why don't you find a book on Java and start from there!!
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Aug 17th, 2007, 3:25 PM   #4
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
Why do you want this code in Java? Is this an assignment?
jaeusm is offline   Reply With Quote
Old Aug 19th, 2007, 9:42 AM   #5
alcdotcom
Programmer
 
Join Date: Jan 2006
Location: Dallas, TX
Posts: 49
Rep Power: 0 alcdotcom is on a distinguished road
Start here and then, as you learn, try to convert your program. It will help you learn Java in the process.
__________________
Java Blog
alcdotcom is offline   Reply With Quote
Old Sep 4th, 2007, 1:18 PM   #6
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 248
Rep Power: 2 Fall Back Son is on a distinguished road
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.
Fall Back Son is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB 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
Programming with Java: Tutorial ReggaetonKing Java 7 May 20th, 2008 10:58 AM
Special browser in Java (Project) stalefish Java 3 Feb 9th, 2008 4:22 PM
Java code.....null pointer exceptions(again sorry) ELHEK Java 2 Oct 25th, 2006 1:56 PM
Java code to convert binary to decimal and hexadecimal to binary prince_haldir Java 1 Mar 7th, 2006 1:51 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:28 PM.

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