![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2007
Posts: 6
Rep Power: 0
![]() |
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.. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Aug 2007
Posts: 6
Rep Power: 0
![]() |
help pls.. thanks
|
|
|
|
|
|
#3 |
|
Sexy Programmer
|
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! |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
Why do you want this code in Java? Is this an assignment?
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jan 2006
Location: Dallas, TX
Posts: 49
Rep Power: 0
![]() |
Start here and then, as you learn, try to convert your program. It will help you learn Java in the process.
__________________
Java Blog |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 248
Rep Power: 2
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |