2014-11-24 121 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 2014-11-24 12:21:43

+0

爲您的結構分配內存並存儲輸入的值 – Gopi 2014-11-24 12:22:04

回答

0

C不像C++不能有嵌套類型,你將不得不單獨聲明它們。

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 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 2014-11-24 12:26:03

+0

嵌套結構:OK。嵌套類型(他試圖定義的方式):不行。 OP想要爲每個代理機構節省數量的員工,所以他需要一個指向員工或VLA的指針。 – 2014-11-24 12:27:47

0

考慮這個實施例:

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 2014-11-24 13:59:01