-1
/* Example in unions */

#include <stdio.h>   // standard header file
#include <ctype.h>   // for atoi function
#include <stdlib.h>  // for malloc function
#include <assert.h>  // for assert macro

#define MAX 512      // for fgets()

typedef enum {National, Alien} PERSON_KIND;

typedef struct date {
    int month;
    int day;
    int year;
} DATE;

typedef struct {
    char countryOfOrigin[30];
    DATE * dateOfEntry;  // so you get comfortable with pointers
    enum { Worker, Tourist, Student} visaStatus;
    char portOfEntry[30];
} ALIEN;

typedef struct {
    char placeOfBirth[30];
    char ssn[20];
} CITIZEN;

typedef union {
    ALIEN alien;
    CITIZEN citizen;
} PERSON_INFO;

typedef struct person {
    char name[30];
    DATE * dateOfBirth;  // just to make it interesting
    PERSON_KIND personKind;
    PERSON_INFO info;
} PERSON;

void fillInfo(PERSON *);
void printInfo(const PERSON *);

int main()  {


    printf("sizeof(int) is %u\n", (unsigned) sizeof(int) );
    printf("sizeof(PERSON_KIND) is %u\n", (unsigned) sizeof(PERSON_KIND) );
    printf("sizeof(DATE) is %u\n", (unsigned) sizeof(DATE) );
    printf("sizeof(ALIEN) is %u\n", (unsigned) sizeof(ALIEN) );
    printf("sizeof(CITIZEN) is %u\n", (unsigned) sizeof(CITIZEN) );
    printf("sizeof(PERSON_INFO) is %u\n", (unsigned) sizeof(PERSON_INFO) );
    printf("sizeof(PERSON) is %u\n", (unsigned) sizeof(PERSON) );

    /* fillInfo(&p1);
    printf("\nThe infomation you entered is given below:\n");
    printInfo(&p1);

    fillInfo(&p2);
    printf("\nThe infomation you entered is given below:\n");
    printInfo(&p2);  */

    return 0;
}

/* void fillInfo(PERSON *p)   {
    char buf[MAX], i;



    printf("Please enter the person's name: ");


    printf("Please enter the birth month: ");

    printf("Please enter the birth date: ");

    printf("Please enter the birth year: ");

    printf("Please enter 1 if the person is a National or 2\n");
    printf("if the person is an alien: ");
    // get the value from the user and store it as an integer in i

    if (i == 1) p -> personKind = National;
    else p -> personKind = Alien;

    if (p -> personKind == National) {
        printf("Please enter the place of birth: ");

        printf("Please enter the ssn: ");

    }

    if (p -> personKind == Alien) {
        printf("Please enter the country of origin: ");

        printf("Please enter the month the person entered this country: ");


        printf("Please enter the day of the month the person entered this country: ");


        printf("Please enter the year the person entered this country: ");

        printf("Please enter the person's immigration status : \n");
        printf("\t1 for Worker:\n");
        printf("\t2 for Tourist:\n");
        printf("\t3 for Student: \n");
        fgets(buf, MAX, stdin);
        i = atoi(buf);

        switch (i) {
             case 1 :
                p -> info.alien.visaStatus = Worker;
                break;
             case 2 :
                p -> info.alien.visaStatus = Tourist;
                break;
             case 3 :
                p -> info.alien.visaStatus = Student;
        }

        printf("Please enter the port of entry: ");

    }
}

void printInfo(const PERSON *p)  {
    printf("Name:         %s\n", p -> name);

    printf("Date of Birth: %2d/%2d/%4d\n", );

    printf("Status: ");
    if ( p -> personKind == National) printf("National\n");
    else printf("Alien\n");

    if ( p -> personKind == National) {
        printf("Place of birth: %s\n", );
        printf("SSN: %s\n", );
    }

    if ( p -> personKind == Alien) {
        printf("Country of origin: %s\n", );

        printf("Date of Entry: %2d/%2d/%4d\n",  );

         printf("Immigration status: ");

        switch (p -> info.alien.visaStatus) {
             case Worker :
                printf("Worker\n");
                break;
             case Tourist :
                printf("Tourist\n");
                break;
             case Student :
                printf("Student\n");
        }
        printf("Port of Entry: %s\n\n", );
    }
} // print_info */

预期输出:

sizeof(int) is 4
sizeof(PERSON_KIND) is 4
sizeof(DATE) is 12
sizeof(ALIEN) is 80
sizeof(CITIZEN) is 50
sizeof(PERSON_INFO) is 80
sizeof(PERSON) is 128
Please enter the person's name: John Doe
Please enter the birth month: 10
Please enter the birth date: 31
Please enter the birth year: 1989
Please enter 1 if the person is a National or 2
if the person is an alien: 1
Please enter the place of birth: Worcester
Please enter the ssn: 012345678

The infomation you entered is given below:
Name:         John Doe

Date of Birth: 10/31/1989
Status: National
Place of birth: Worcester

SSN: 012345678

Please enter the person's name: Jane Smith
Please enter the birth month: 2
Please enter the birth date: 20
Please enter the birth year: 1970
Please enter 1 if the person is a National or 2
if the person is an alien: 2
Please enter the country of origin: Libya
Please enter the month the person entered this country: 10
Please enter the day of the month the person entered this country: 23
Please enter the year the person entered this country: 2009
Please enter the person's immigration status : 
    1 for Worker:
    2 for Tourist:
    3 for Student: 
1
Please enter the port of entry: New York

The infomation you entered is given below:
Name:         Jane Smith

Date of Birth:  2/20/1970
Status: Alien
Country of origin: Libya

Date of Entry: 10/23/2009
Immigration status: Worker
Port of Entry: New York

取消注释所有注释代码时的编译器错误:

Example_union.c: In function ‘main’:
Example_union.c:56:12: error: ‘p1’ undeclared (first use in this function)
Example_union.c:56:12: note: each undeclared identifier is reported only once for each function it appears in
Example_union.c:60:12: error: ‘p2’ undeclared (first use in this function)
Example_union.c:62:20: error: expected expression before ‘/’ token
Example_union.c: In function ‘printInfo’:
Example_union.c:132:41: error: expected expression before ‘)’ token
Example_union.c:139:34: error: expected expression before ‘)’ token
Example_union.c:140:23: error: expected expression before ‘)’ token
Example_union.c:144:37: error: expected expression before ‘)’ token
Example_union.c:146:43: error: expected expression before ‘)’ token
Example_union.c:160:35: error: expected expression before ‘)’ token
Example_union.c:65:1: warning: control reaches end of non-void function [-Wreturn-type]

我不熟悉工会……因为我只熟悉结构。想知道如何完成上面的代码/联合示例,以便产生正确的输出,如上所示。我还列出了未完成代码的当前编译器错误。希望你能帮忙,因为我急需。谢谢。

4

1 回答 1

1

p1 未声明,但有调用将指针 p1 取消引用为 &p1 与 p2 相同。先解决这个问题。

于 2013-04-12T04:18:29.653 回答