2014-11-24 1 views
-1

목표 : 사용자에게 대행사 수를 요청하고 대행사를 만들고 각 대행사에 대해 직원 수를 요청하고 해당 직원을 생성합니다. 이것의C의 중첩 구조

부분은, 내 생각,

typedef struct agence agence; 
struct agence 
{ 
    char nom[20]; 
    int nmbrEmp; 
    struct employe 
    { 
     char mat[20]; 
     int nmbrEnf; 
     int ANC; 
     double SB; 
     double RCNSS; 
    } 
}; 

같은 것을 중첩 된 구조를이 N 옳은 길입니다 필요하고, 어떻게 기관의 수를 만들기 위해 진행 할 것입니다/사용자 한번 직원을 제공합니다 각각에 필요한 번호.

+0

이전에 'malloc'을 사용 했습니까? – doctorlove

+0

구조체에 메모리를 할당하고 입력 된 값을 저장하십시오. – Gopi

답변

0

C++과 달리 중첩 형식을 사용할 수 없으므로 별도로 선언해야합니다.

struct agence * agencies; 
size_t num_agencies = 100; 

agencies = calloc(sizeof(*agencies), num_agencies); 

for (/* read egancies somehow */) { 
    agencies[i].nmbrEmp = number_employees; 
    agencies[i].employees = calloc(sizeof(agencies[i].employees[0]), number_employees); 

    // write agencies[i].employees[j] ... 
} 
+2

중첩 구조가 잘못되었습니다. – Gopi

+0

중첩 구조 : OK. 중첩 형식 (엠을 정의하는 방식) : 좋지 않습니다. 영업 담당자는 대행사 당 여러 직원을 저장하려고하므로 직원 또는 VLA에 대한 포인터가 필요합니다. –

0

이 예 고려 :

struct employe 
{ 
    char mat[20]; 
    int nmbrEnf; 
    int ANC; 
    double SB; 
    double RCNSS; 
}; 

struct agence 
{ 
    char nom[20]; 
    int nmbrEmp; 
    struct employe * employees; // pointer to an array of employees 
}; 

, 동적 메모리를 사용하고 그들을 채우 여기

struct Employee 
{ 
    char ename[20]; 
    int ssn; 
    float salary; 
    struct date 
     { 
     int date; 
     int month; 
     int year; 
     }doj; 
}emp1; 

Accessing Nested Members : 

Accessing Month Field : emp1.doj.month 
Accessing day Field : emp1.doj.day 
Accessing year Field : emp1.doj.year 

하면 위의 예를 설명 할 것이다 코드 패드이다

enter link description here

0

c에서는 이름이없는 중첩 구조 [다른 경우 중첩 형식]를 사용할 수 없습니다. 그들은 명명 된 구조 여야합니다. 당신은 그러나

typedef struct agency 
{ 
    char nom[20]; 
    int nmbrEmp; 
    struct employee 
    { 
     char mat[20]; 
     int nmbrEnf; 
     int ANC; 
     double SB; 
     double RCNSS; 
    } emp [20]; 
}agency; 

같은 것을 작성해야합니다, 여기 당신이 의미 크기 20의 정적 입력에 제한됩니다, 당신은 20 개 이상의 직원 기록을 가질 수 없습니다. 이 제한에서 벗어나려면 가장 좋은 방법은 employee 구조체에 대한 포인터를 사용하는 것입니다. 런타임시 사용자가 지정한 직원 수를 기준으로 메모리를 할당하십시오.

0

다음은 사용자가 고려할 수있는 예입니다. 이것은 당신이해야 할 일의 템플리트입니다. 직원, 대행사 이름 등을 조작해야합니다. (오류 검사가 없다는 점에 유의하십시오.)

#include <stdio.h> 
#include <stdlib.h> 

/* TODO: modify members */ 
typedef struct { 
    int id; 
    int salary; 
} employee; 

/* TODO: modify members */ 
typedef struct { 
    char name[20]; 
    employee* emps; 
    int emps_count; 
} agency; 

/* return sum of id and salary for first employee from agency */ 
int dumb_calc(agency ag) { 
    return ag.emps[0].id + ag.emps[0].salary; 
} 

int main(void) 
{ 
    int num_ag; 
    int num_emps; 
    int i, j; 
    printf("enter num of agencies:\n"); 
    scanf("%d", &num_ag); 
    agency* agencies = malloc(sizeof(agency) * num_ag); 

    for (i = 0; i < num_ag; ++i) { 
     /* TODO: modify single agency name */ 
     sprintf(agencies[i].name, "agency %d", i+1); 
     printf("enter num of employees for agency %d\n", i+1); 
     scanf("%d", &num_emps); 
     agencies[i].emps = malloc(sizeof(employee) * num_emps); 
     agencies[i].emps_count = num_emps; 
     for (j = 0; j < num_emps; ++j) { 
      /* TODO: modify single employee */ 
      agencies[i].emps[j].id = j+1; 
      agencies[i].emps[j].salary = 1000*(j+1); 
     } 
    } 

    /* TODO: change printing style */ 
    for (i = 0; i < num_ag; ++i) { 
     printf("agency name: %s\n", agencies[i].name); 
     printf("num of employees: %d\n", agencies[i].emps_count); 
     /* result will always be the same */ 
     printf("sum of id and salary for 1st emp: %d\n", dumb_calc(agencies[i])); 
    } 

    /* remember to free what you've alloc'd */ 
    for (i = 0; i < num_ag; ++i) { 
     free(agencies[i].emps); 
    } 
    free(agencies); 

    return 0; 
} 
+0

두 개의 int 값으로 함수로 전달하거나 함수 인수로'agency'를 가져 와서 함수 내의 멤버에 액세스하고 결과를 계산하고 반환 할 수 있습니다. 나는 당신에게 그것을하기의 어떤 방법을 보여주기 위하여 응답을 편집했다. – macfij