Results 1 to 4 of 4
Thread: A C programmer
- 12-28-2012, 10:28 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 2
- Rep Power
- 0
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();
}
- 12-28-2012, 10:45 AM #2
Member
- Join Date
- Jul 2012
- Location
- Earth
- Posts
- 75
- Rep Power
- 0
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!
- 12-28-2012, 11:46 AM #3
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
Guide For New Members
http://www.java-forums.org/misc.php?do=bbcode#code
db
THREAD CLOSEDWhy do they call it rush hour when nothing moves? - Robin Williams
-
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.
Similar Threads
-
Which books should i buy new programmer
By enzo80 in forum Forum LobbyReplies: 3Last Post: 10-05-2012, 11:31 AM -
Java Programmer
By ITpraca in forum Jobs OfferedReplies: 0Last Post: 06-21-2011, 09:46 AM -
Sr. Java programmer
By Getsy in forum Jobs OfferedReplies: 3Last Post: 08-11-2009, 03:11 PM -
Becoming a better programmer.
By AnGuRuSO in forum Advanced JavaReplies: 1Last Post: 11-21-2008, 04:39 PM -
Programmer Gone Bad
By sixohseven in forum IntroductionsReplies: 2Last Post: 08-09-2007, 06:18 PM


LinkBack URL
About LinkBacks

Bookmarks