Results 1 to 4 of 4

Thread: A C programmer

  1. #1
    xperia2995 is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Smile 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();
    }

  2. #2
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default 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!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,421
    Rep Power
    20

    Default 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 CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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

  1. Which books should i buy new programmer
    By enzo80 in forum Forum Lobby
    Replies: 3
    Last Post: 10-05-2012, 12:31 PM
  2. Java Programmer
    By ITpraca in forum Jobs Offered
    Replies: 0
    Last Post: 06-21-2011, 10:46 AM
  3. Sr. Java programmer
    By Getsy in forum Jobs Offered
    Replies: 3
    Last Post: 08-11-2009, 04:11 PM
  4. Becoming a better programmer.
    By AnGuRuSO in forum Advanced Java
    Replies: 1
    Last Post: 11-21-2008, 05:39 PM
  5. Programmer Gone Bad
    By sixohseven in forum Introductions
    Replies: 2
    Last Post: 08-09-2007, 07:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •