convert C struct to java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • acap
    New Member
    • Feb 2007
    • 2

    #1

    convert C struct to java

    my problem is converting C to java, pls help.....

    this the code....

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>

    #define FIRST_NAME_LEN 31
    #define SECOND_NAME_LEN 51
    #define NUMBER_LEN 16
    #define MAX_NUMBERS 50
    #define TRUE 1
    #define FALSE 0


    struct Name
    {
    char firstname[FIRST_NAME_LEN];
    char secondname[SECOND_NAME_LEN];
    };


    struct PhoneRecord
    {
    struct Name name;
    char number[NUMBER_LEN];
    };

    struct Name read_name();
    void show(struct PhoneRecord record);
    int has_name(struct PhoneRecord record, struct Name name);

    void main()
    {
    char answer = 'n';
    struct PhoneRecord records[MAX_NUMBERS];
    struct Name aName;
    int count = 0;
    int found = FALSE;
    int i = 0;


    do
    {
    records[count].name = read_name();
    printf("Enter the number for this name: ");
    scanf(" %[ 0123456789]",records[count++].number);

    printf("Do you want to enter another(y or n)?: ");
    scanf(" %c", &answer);
    }while(count<=M AX_NUMBERS && tolower(answer) == 'y');


    do
    {
    printf("Enter a name for which you want the number.");
    aName =read_name();
    for(i = 0 ; i<count ; i++)
    {
    if(has_name(rec ords[i], aName))
    {
    if(!found)
    {
    found = TRUE;
    printf("The numbers for this name are:\n");
    }
    printf("%s\n", records[i].number); /
    }
    }
    if(found)
    found = FALSE;
    else
    printf("No numbers found for this name.\n");
    printf("Do you want to search for another (y or n)? ");
    scanf(" %c" , &answer);
    }while(tolower( answer) == 'y');

    for(i = 0 ; i<count ; i++)
    show(records[i]);
    printf("\n");

    }

    /* Function to read a name and store it in a Name structure */
    struct Name read_name()
    {
    struct Name name;
    printf("Enter a first name: ");
    scanf(" %s", &name.firstname );
    printf("Enter a second name: ");
    scanf(" %s", &name.secondnam e);
    return name;
    }

    /* Function to output a record */
    void show(struct PhoneRecord record)
    {
    printf("\n%s %s %s", record.name.fir stname,record.n ame.secondname, record.number);
    }

    /* Function to test whether the name is the same as in a record */
    int has_name(struct PhoneRecord record, struct Name name)
    {
    return (strcmp(name.fi rstname, record.name.fir stname)==0 && strcmp(name.sec ondname, record.name.sec ondname)==0);
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by acap
    my problem is converting C to java, pls help.....

    this the code....

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>

    #define FIRST_NAME_LEN 31
    #define SECOND_NAME_LEN 51
    #define NUMBER_LEN 16
    #define MAX_NUMBERS 50
    #define TRUE 1
    #define FALSE 0


    struct Name
    {
    char firstname[FIRST_NAME_LEN];
    char secondname[SECOND_NAME_LEN];
    };


    struct PhoneRecord
    {
    struct Name name;
    char number[NUMBER_LEN];
    };

    struct Name read_name();
    void show(struct PhoneRecord record);
    int has_name(struct PhoneRecord record, struct Name name);

    void main()
    {
    char answer = 'n';
    struct PhoneRecord records[MAX_NUMBERS];
    struct Name aName;
    int count = 0;
    int found = FALSE;
    int i = 0;


    do
    {
    records[count].name = read_name();
    printf("Enter the number for this name: ");
    scanf(" %[ 0123456789]",records[count++].number);

    printf("Do you want to enter another(y or n)?: ");
    scanf(" %c", &answer);
    }while(count<=M AX_NUMBERS && tolower(answer) == 'y');


    do
    {
    printf("Enter a name for which you want the number.");
    aName =read_name();
    for(i = 0 ; i<count ; i++)
    {
    if(has_name(rec ords[i], aName))
    {
    if(!found)
    {
    found = TRUE;
    printf("The numbers for this name are:\n");
    }
    printf("%s\n", records[i].number); /
    }
    }
    if(found)
    found = FALSE;
    else
    printf("No numbers found for this name.\n");
    printf("Do you want to search for another (y or n)? ");
    scanf(" %c" , &answer);
    }while(tolower( answer) == 'y');

    for(i = 0 ; i<count ; i++)
    show(records[i]);
    printf("\n");

    }

    /* Function to read a name and store it in a Name structure */
    struct Name read_name()
    {
    struct Name name;
    printf("Enter a first name: ");
    scanf(" %s", &name.firstname );
    printf("Enter a second name: ");
    scanf(" %s", &name.secondnam e);
    return name;
    }

    /* Function to output a record */
    void show(struct PhoneRecord record)
    {
    printf("\n%s %s %s", record.name.fir stname,record.n ame.secondname, record.number);
    }

    /* Function to test whether the name is the same as in a record */
    int has_name(struct PhoneRecord record, struct Name name)
    {
    return (strcmp(name.fi rstname, record.name.fir stname)==0 && strcmp(name.sec ondname, record.name.sec ondname)==0);
    }
    Change your struct to class and your printf to System.out.prin tln statements. Do you know any Java at all?

    Comment

    • acap
      New Member
      • Feb 2007
      • 2

      #3
      Originally posted by r035198x
      Change your struct to class and your printf to System.out.prin tln statements. Do you know any Java at all?
      sorry, but i know a little bit. i just starting to learn for myself. anyway, how about the code i gave, i know tthat you have the class person, name and phonerecord. i confused about writing the main app. could you help?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by acap
        sorry, but i know a little bit. i just starting to learn for myself. anyway, how about the code i gave, i know tthat you have the class person, name and phonerecord. i confused about writing the main app. could you help?
        The code you gave is the C code. You need to write the code for the Person class and post it explaining what you want the main app to be able to do.

        Comment

        • hirak1984
          Contributor
          • Jan 2007
          • 316

          #5
          Well dude first make urself clear.

          You want to rewrite the code in java?(then follow what r035198x said)
          or yoyu want to migrate from c++ to java?(then it will need lots of reading in your side.You may start with Herbert Schildt)

          Comment

          Working...