2015-01-18 1 views
0
struct student { 

    char   *s_name; 

    struct student_id s_id; 

    /** Number of references to this student. */ 
    unsigned int   s_ref; 

    /** Transcript (singly-linked list, NULL terminator). */ 
    struct transcript_entry *s_transcript; 

    /** Whether or not this student has completed his/her program. */ 
    student_complete  s_complete; 
}; 

struct student* student_grad_create(const char *name, size_t namelen, 
    struct student_id, int phd); 

struct student* student_undergrad_create(const char *name, size_t namelen, 
    struct student_id); 

: 나는 그나마동일한 기능을하지만 서로 다른 매개 변수 전달이라는 기능을 구현해야 학생 세 종류의 <strong>마스터 학생 및 박사 과정 학생 및 학부 학생</strong> .I이 있습니다

int student_add_entry(struct student *, struct transcript_entry *); 

을 학생 유형을 어떻게 결정할 수 있는지 알고 있습니까?

다음을 수행해야합니까?

int student_add_entry(struct student *undergrad_create, struct transcript_entry *){} 
int student_add_entry(struct student *grad_create, struct transcript_entry *){} 

감사합니다.

+0

그건 아주 재미있는 생각입니다. 그걸로 무엇을 할 수 있겠습니까? –

+4

'C'를 원하겠습니까? 어쨌든, 학생 타입을 나타내는'enum '은 어떨까요? –

+0

C에 함수 오버로드가 없습니다. –

답변

2

당신은 구조체

struct student { 
    char     *s_name; 
    struct student_id  s_id; 
    /** Number of references to this student. */ 
    unsigned int    s_ref; 
    /** Transcript (singly-linked list, NULL terminator). */ 
    struct transcript_entry *s_transcript; 
    /** Whether or not this student has completed his/her program. */ 
    student_complete   s_complete; 
    enum student_type  s_type; 
}; 

student_type 필드를 추가 할 수 있으며, 당신은 것 인 enum 같은

enum student_type 
{ 
    UndergraduateStudent, 
    MasterStudent, 
    PHDStudent 
}; 

당신이 s_type 필드를 설정하고 사용하는 것이 struct student의 인스턴스를 생성 s_type 다른 곳에서는 학생 유형을 결정할 수 있습니다.

또한 가능한 모든 매개 변수에 struct student passign의 인스턴스를 생성하는 제네릭 함수를 작성하고 편의를 원하는 경우 기본 매개 변수를 제네릭에 전달할 수있는 각 특정 유형에 대한 함수를 만들 수도 있습니다 기능.

+0

감사합니다. 구조체 학생에게 enum student_type을 추가했습니다. 나는 방금 C를 배우기 시작했습니다. 저는 학생 등록 ​​시스템에 관한 몇 가지 문제를 해결하기 위해 노력하고 있습니다. – Peter

+0

아래에 다른 질문이 있습니다. 제발 좀 도와주세요. – Peter

+0

@Peter 다른 게시물에 대한 답변으로 질문을 게시 할 수 없으면 새로운 질문을 게시해야합니다. 답변을 삭제하고 질문으로 게시하십시오. –

0

일부 학생 등록 ​​시스템 문제가 있습니다. 1. 학생 유형의 세 가지 다른 종류의, 2. 대학원에 학생이 50 시험에 합격하고, 마스터와 박사 학위는 40 개 과목을 완료해야 65 명 3. 학부생으로 통과 멩 학생 5 및 박사 과정 학생들이있다 2.

이것은 구현해야하는 세 가지 기능입니다.

struct transcript_entry { 
    enum faculty   te_faculty; 
    unsigned int   te_number; 
    unsigned int   te_grade; 
    /** Number of references to this entry. */ 
    unsigned int   te_ref; 
    /** Next entry in the singly-linked list (or NULL terminator). */ 
    struct transcript_entry *te_next; 
}; 

나는 다음과 같은 기능을 구현 :

struct transcript_entry* transcript_entry(enum faculty faculty, unsigned int course_number, unsigned int grade){ 
    struct transcript_entry *trans_entry; 
    trans_entry = malloc(sizeof(struct transcript_entry)); 
    trans_entry->te_faculty = faculty; 
    trans_entry->te_number = course_number; 
    trans_entry->te_grade = grade; 
    trans_entry->te_ref = 1; 
    trans_entry->te_next = NULL; 
    return trans_entry; 
} 

/** Deallocate a @ref transcript_entry. */ 
void transcript_entry_free(struct transcript_entry *trans_entry){ 
    free(trans_entry); 
} 

/** Increment a @ref transcript_entry's refcount. */ 
void tehold(struct transcript_entry *trans_entry){ 
    trans_entry-> te_ref++; 
} 

내가 참조 횟수에 대한 몇 가지 자습서를보고 아래 ' 은 transcript_entry의 구조체이다. 내 코드가 이해가 안되는지 모르겠다. 그래서 평균은 ++를 카운팅하거나 te_ref--만큼 감소시키는 것을 의미합니다.