-
A C programmer
I am a noob programmer in C
i hav the fol C code I would like to run in Java also but it is too hard to convert although C and java have many similarities.
Can someone help where conversions are required
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
float convert(char a[],int b)
{
int i,d,temp,pos=0,decpoint=0,negative=0;
float sum=0;
if(a[0]=='-')
{
negative=1;
for(i=0;a[i]!='\0';i++)
a[i]=a[i+1];
}
for(i=0;a[i]!='\0';i++)
if(a[i]=='.') /*storing loc of decimalpoint or \0 into pos*/
{
pos=i;
decpoint=1; /*flag that there is a decimal point*/
break;
}
if(pos==0)
pos=i;
for(i=pos-1,d=0;i>=0;i--,d++) /*left side of dec point*/
{
if(a[i]>='0'&&a[i]<='9')
temp=(int)a[i]-48;
else if(a[i]>='a'&&a[i]<='f')
temp=(int)a[i]-87;
else exit(0);
sum+=temp*pow(b,d);
}
if(decpoint!=0) /*Right side of dec point*/
for(i=pos+1,d=-1;a[i]!='\0';i++,d--)
{
if(a[i]>='0'&&a[i]<='9')
temp=(int)a[i]-48;
else if(a[i]>='a'&&a[i]<='f')
temp=(int)a[i]-87;
else exit(0);
sum+=temp*pow(b,d);
}
if(negative==0)
return sum;
else return(0.0-sum);
}
char* reconvert(float n,int base)
{
float decpart;
int whole,i,negative=0;
char wholechar[50],decchar[10],stack[50];
if(n<0)
{
n=fabs(n);
negative=1;
}
whole=(int)n;
decpart=n-whole;
for(i=0;i<4;i++)
{
decpart*=base;
decchar[i]=(char)((int)decpart+48);
decpart-=(int)decpart;
}
decchar[i]='\0';
i=0;
do
{
switch(whole%base)
{
case 10: stack[i]='a'; break;
case 11: stack[i]='b'; break;
case 12: stack[i]='c'; break;
case 13: stack[i]='d'; break;
case 14: stack[i]='e'; break;
case 15: stack[i]='f'; break;
default: stack[i]=(char)((whole%base)+48);
}
i++;
whole/=base;
}while(whole>=base);
stack[i++]=(char)(whole+48);
stack[i]='\0';
if(negative==1)
strcat(stack,"-");
strrev(stack);
strcat(stack,".");
strcat(stack,decchar);
return(stack);
}
void main()
{
char a[25],b[25],temp[50];
float num1,num2;
char ch,op,choice='n';
int i;
do
{
clrscr();
printf("Input base - b , h , o\n");
scanf(" %c",&ch);
printf("Input first ");
scanf("%s",&a);
printf("Input operator ");
scanf(" %c",&op);
printf("Input second ");
scanf("%s",&b);
/*---------------inputting is over------------------------*/
if(ch=='b')
{
num1=convert(a,2);
num2=convert(b,2);
i=2;
}
else if(ch=='h')
{
num1=convert(a,16);
num2=convert(b,16);
i=16;
}
else if(ch=='o')
{
num1=convert(a,8);
num2=convert(b,8);
i=8;
}
else
exit(0);
switch(op)
{
case '+':strcpy(temp,reconvert((num1+num2),i)); break;
case '-':strcpy(temp,reconvert((num1-num2),i)); break;
case '*':strcpy(temp,reconvert((num1*num2),i)); break;
case '/':strcpy(temp,reconvert((num1/num2),i)); break;
default :printf("\nInvalid operator");
}
printf("%s",temp);
printf("\nContinue?(y/n} ");
scanf(" %c",&choice);
}while(choice=='y'||choice=='Y');
getch();
}
-
Re: A C programmer
So your homework is to write a simple calculator in Java but you can't find one on the Internet to plagiarise but you have found one written in 'C' and you hope we are mugs and will do the conversion for you. Nice try!
-
Re: A C programmer
Off topic, closing.
xperia2995, if you have a Java question, please feel free to start another thread. Note that 'do this for me' is not a Java question.
And before you start another thread or post any Java code, go through the following links:
Forum Rules
http://www.java-forums.org/forum-gui...w-members.html
http://www.java-forums.org/misc.php?do=bbcode#code
db
THREAD CLOSED
-
Re: A C programmer
I disagree. It's not off-topic, since he wishes to create a Java program from the C program, no it's unethical. Regardless, you're right in closing it.