(u_u)

構造体の中で列挙型を使う

Categories: [blog]
Tags: [C], [enum], [typedef], [列挙体], [構造体]

例:

enum DIRECTION{
    FOWARD,
    BACK
};
enum SPEED{
    HIGH,
    MIDDLE,
    LOW
};

typedef struct{
    enum DIRECTION direction;
    enum SPEED speed;
}MOTOR;

MOTOR leftMotor;
MOTOR rightMotor;

int main(void){
    while(true){
        switch(leftMotor.direction){
            case FORWARD:
                左モーターを正転();
                break;

            case BACK:
                左モーターを逆転();
                break;

            default:
                error();
                break;
        }
    }
}

void 左モーターを正転(){
    switch(leftMotor.speed){
        case HIGH:
            xxxヘルツで回転();
            break;

        case MIDDLE:
            yyyヘルツで回転();
            break;

        case LOW:
            zzzヘルツで回転();
            break;

        default:
            error();
            break;
    }
}

続く....