C 相同的函数,但传递给函数的参数不同

C 相同的函数,但传递给函数的参数不同,c,polymorphism,function-pointers,C,Polymorphism,Function Pointers,有三种学生,硕士生、博士生和本科生。我需要实现一个函数,该函数名为: 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 terminato

有三种学生,硕士生、博士生和本科生。我需要实现一个函数,该函数名为:

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);
我不知道如何确定学生类型

我应该做以下事情吗

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 *){}
你会有一个
enum

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;
};
创建
struct student
实例时,您需要设置
s_type
字段,然后在其他地方使用
s_type
来确定学生的类型


我还将编写一个泛型函数来创建
struct student
的实例,并将所有可能的参数传递给它,如果您想方便起见,还可以为每个特定类型创建一个函数,您可以将默认参数传递给通用函数。

我正在处理一些学生注册系统问题。 1.有三种不同类型的学生 2.本科生以50分通过考试,硕士和博士以65分通过考试 3.本科生必须完成40门课程,硕士生5门,博士生2门

这是我需要实现的三个功能。 ' 下面是成绩单项目的结构:

enum student_type
{
    UndergraduateStudent,
    MasterStudent,
    PHDStudent
};
我实现以下功能:

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;
};

我看了一些关于参考计数的教程。我不知道我的代码是否有意义。那么这是引用计数++的平均增量,还是te_ref--的减量

这是一个非常有趣的想法。。。你用它能实现什么?你确定你想要
C
?总之,一个指示学生类型的
enum
怎么样?C没有函数重载。@OliverCharlesworth该函数中没有重载,两者都是相同的,参数名称不同。“我不知道如何确定学生类型。”在现实生活中你会怎么做?谢谢iharob。我将enum student_类型添加到struct student。我刚开始学习C。我正在研究一些关于学生注册系统的问题。我还有一些其他问题,你能帮我看看吗?@Peter你不能把一个问题作为另一个帖子的答案发布,你必须发布一个新问题。请删除答案并将其作为问题发布。
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++;
}