C语言程序编程出现问题不知道怎么改

img


C语言程序编程,计算教授副教授以及职工的工资问题,工资等于基本工资➕课时工资,但是出现了问题,请问怎么办


#include <stdio.h>

// 定义基类
typedef struct {
    double base_salary;
} Employee;

// 定义三个子类,分别为教授、副教授和职工
typedef struct {
    Employee employee;
    double teaching_hours;
} Professor;

typedef struct {
    Employee employee;
    double teaching_hours;
} AssociateProfessor;

typedef struct {
    Employee employee;
    double work_hours;
} Staff;

// 定义子类的方法,用于计算课时工资
double calculate_teaching_salary(double teaching_hours) {
    return teaching_hours * 50.0;  // 假设每个小时的课时工资为 50 元
}

double calculate_work_salary(double work_hours) {
    return work_hours * 30.0;  // 假设每个小时的工时工资为 30 元
}

int main() {
    // 实例化三个子类对象,并调用子类的方法计算工资
    Professor professor = {{5000.0}, 80.0};
    double professor_salary = professor.employee.base_salary + calculate_teaching_salary(professor.teaching_hours);
    printf("Professor salary: %f\n", professor_salary);

    AssociateProfessor associate_professor = {{4000.0}, 60.0};
    double associate_professor_salary = associate_professor.employee.base_salary + calculate_teaching_salary(associate_professor.teaching_hours);
    printf("Associate professor salary: %f\n", associate_professor_salary);

    Staff staff = {{3000.0}, 120.0};
    double staff_salary = staff.employee.base_salary + calculate_work_salary(staff.work_hours);
    printf("Staff salary: %f\n", staff_salary);

    return 0;
}


img

把这些类的定义给出来看下你纯虚函数继承的时候是不是没有实现,还是其他什么错误

  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7791391
  • 这篇博客也不错, 你可以看下C语言打印多颜色字体,多功能打印,协助开发调试
  • 除此之外, 这篇博客: C语言预处理详解中的 当宏参数在宏的定义中出现超过一次的时候,如果参数带有副作用,那么你在使用这个宏的时候就可能出现危险,导致不可预测的后果。副作用就是表达式求值的时候出现的永久性效果 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • #include<stdio.h>
    //x + 1;//不带副作用
    //x++;//带有副作用
    #define MAX(a, b) ((a) > (b) ? (a) : (b))
    int main()
    {
    	int x = 5;
    	int y = 8;
    	int z = MAX(x++, y++);
    	printf("x=%d y=%d z=%d\n", x, y, z);//输出的结果是什么?
    	return 0;
    }
  • 您还可以看一下 贺利坚老师的C语言及程序设计初步课程中的 程序设计语言小节, 巩固相关知识点