#include <stdio.h>
// กำหนดค่าของแต่ละสถานะ
typedef enum {
STATE_INITIAL,
STATE_RUNNING,
STATE_STOPPED,
STATE_ERROR
} State;
// ฟังก์ชั่นสำหรับเปลี่ยนสถานะของเครื่อง
void transition(State *state, char input) {
switch (*state) {
case STATE_INITIAL:
if (input == 'S') {
*state = STATE_RUNNING;
printf("Transitioning to RUNNING state.\n"); } else {
*state = STATE_ERROR;
printf("Error! Invalid input.\n"); }
break;
case STATE_RUNNING:
if (input == 'P') {
*state = STATE_STOPPED;
printf("Transitioning to STOPPED state.\n"); } else if (input == 'S') {
*state = STATE_RUNNING;
printf("Already in RUNNING state.\n"); } else {
*state = STATE_ERROR;
printf("Error! Invalid input.\n"); }
break;
case STATE_STOPPED:
if (input == 'S') {
*state = STATE_RUNNING;
printf("Transitioning to RUNNING state.\n"); } else if (input == 'P') {
*state = STATE_STOPPED;
printf("Already in STOPPED state.\n"); } else {
*state = STATE_ERROR;
printf("Error! Invalid input.\n"); }
break;
case STATE_ERROR:
printf("Machine is in ERROR state. Cannot transition further.\n"); break;
}
}
int main() {
// เริ่มต้นเครื่องในสถานะ STATE_INITIAL
State currentState = STATE_INITIAL;
char input;
printf("Machine is starting...\n");
// ทำงานในลูปจนกว่าจะเกิด error
while (currentState != STATE_ERROR) {
switch (currentState) {
case STATE_INITIAL
: printf("INITIAL\n"); break; case STATE_RUNNING
: printf("RUNNING\n"); break; case STATE_STOPPED
: printf("STOPPED\n"); break; default: break;
}
printf("Enter command (S: Start, P: Stop): "); scanf(" %c", &input
); // รับค่าจากผู้ใช้
transition(¤tState, input); // เปลี่ยนสถานะตามคำสั่งที่รับ
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgovLyDguIHguLPguKvguJnguJTguITguYjguLLguILguK3guIfguYHguJXguYjguKXguLDguKrguJbguLLguJnguLAKdHlwZWRlZiBlbnVtIHsKICAgIFNUQVRFX0lOSVRJQUwsCiAgICBTVEFURV9SVU5OSU5HLAogICAgU1RBVEVfU1RPUFBFRCwKICAgIFNUQVRFX0VSUk9SCn0gU3RhdGU7CgovLyDguJ/guLHguIfguIHguYzguIrguLHguYjguJnguKrguLPguKvguKPguLHguJrguYDguJvguKXguLXguYjguKLguJnguKrguJbguLLguJnguLDguILguK3guIfguYDguITguKPguLfguYjguK3guIcKdm9pZCB0cmFuc2l0aW9uKFN0YXRlICpzdGF0ZSwgY2hhciBpbnB1dCkgewogICAgc3dpdGNoICgqc3RhdGUpIHsKICAgICAgICBjYXNlIFNUQVRFX0lOSVRJQUw6CiAgICAgICAgICAgIGlmIChpbnB1dCA9PSAnUycpIHsKICAgICAgICAgICAgICAgICpzdGF0ZSA9IFNUQVRFX1JVTk5JTkc7CiAgICAgICAgICAgICAgICBwcmludGYoIlRyYW5zaXRpb25pbmcgdG8gUlVOTklORyBzdGF0ZS5cbiIpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgKnN0YXRlID0gU1RBVEVfRVJST1I7CiAgICAgICAgICAgICAgICBwcmludGYoIkVycm9yISBJbnZhbGlkIGlucHV0LlxuIik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgY2FzZSBTVEFURV9SVU5OSU5HOgogICAgICAgICAgICBpZiAoaW5wdXQgPT0gJ1AnKSB7CiAgICAgICAgICAgICAgICAqc3RhdGUgPSBTVEFURV9TVE9QUEVEOwogICAgICAgICAgICAgICAgcHJpbnRmKCJUcmFuc2l0aW9uaW5nIHRvIFNUT1BQRUQgc3RhdGUuXG4iKTsKICAgICAgICAgICAgfSBlbHNlIGlmIChpbnB1dCA9PSAnUycpIHsKICAgICAgICAgICAgICAgICpzdGF0ZSA9IFNUQVRFX1JVTk5JTkc7CiAgICAgICAgICAgICAgICBwcmludGYoIkFscmVhZHkgaW4gUlVOTklORyBzdGF0ZS5cbiIpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgKnN0YXRlID0gU1RBVEVfRVJST1I7CiAgICAgICAgICAgICAgICBwcmludGYoIkVycm9yISBJbnZhbGlkIGlucHV0LlxuIik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgY2FzZSBTVEFURV9TVE9QUEVEOgogICAgICAgICAgICBpZiAoaW5wdXQgPT0gJ1MnKSB7CiAgICAgICAgICAgICAgICAqc3RhdGUgPSBTVEFURV9SVU5OSU5HOwogICAgICAgICAgICAgICAgcHJpbnRmKCJUcmFuc2l0aW9uaW5nIHRvIFJVTk5JTkcgc3RhdGUuXG4iKTsKICAgICAgICAgICAgfSBlbHNlIGlmIChpbnB1dCA9PSAnUCcpIHsKICAgICAgICAgICAgICAgICpzdGF0ZSA9IFNUQVRFX1NUT1BQRUQ7CiAgICAgICAgICAgICAgICBwcmludGYoIkFscmVhZHkgaW4gU1RPUFBFRCBzdGF0ZS5cbiIpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgKnN0YXRlID0gU1RBVEVfRVJST1I7CiAgICAgICAgICAgICAgICBwcmludGYoIkVycm9yISBJbnZhbGlkIGlucHV0LlxuIik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgY2FzZSBTVEFURV9FUlJPUjoKICAgICAgICAgICAgcHJpbnRmKCJNYWNoaW5lIGlzIGluIEVSUk9SIHN0YXRlLiBDYW5ub3QgdHJhbnNpdGlvbiBmdXJ0aGVyLlxuIik7CiAgICAgICAgICAgIGJyZWFrOwogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIC8vIOC5gOC4o+C4tOC5iOC4oeC4leC5ieC4meC5gOC4hOC4o+C4t+C5iOC4reC4h+C5g+C4meC4quC4luC4suC4meC4sCBTVEFURV9JTklUSUFMCiAgICBTdGF0ZSBjdXJyZW50U3RhdGUgPSBTVEFURV9JTklUSUFMOwogICAgY2hhciBpbnB1dDsKICAgIAogICAgcHJpbnRmKCJNYWNoaW5lIGlzIHN0YXJ0aW5nLi4uXG4iKTsKCiAgICAvLyDguJfguLPguIfguLLguJnguYPguJnguKXguLnguJvguIjguJnguIHguKfguYjguLLguIjguLDguYDguIHguLTguJQgZXJyb3IKICAgIHdoaWxlIChjdXJyZW50U3RhdGUgIT0gU1RBVEVfRVJST1IpIHsKICAgICAgICBwcmludGYoIkN1cnJlbnQgc3RhdGU6ICIpOwogICAgICAgIHN3aXRjaCAoY3VycmVudFN0YXRlKSB7CiAgICAgICAgICAgIGNhc2UgU1RBVEVfSU5JVElBTDogcHJpbnRmKCJJTklUSUFMXG4iKTsgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgU1RBVEVfUlVOTklORzogcHJpbnRmKCJSVU5OSU5HXG4iKTsgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgU1RBVEVfU1RPUFBFRDogcHJpbnRmKCJTVE9QUEVEXG4iKTsgYnJlYWs7CiAgICAgICAgICAgIGRlZmF1bHQ6IGJyZWFrOwogICAgICAgIH0KICAgICAgICAKICAgICAgICBwcmludGYoIkVudGVyIGNvbW1hbmQgKFM6IFN0YXJ0LCBQOiBTdG9wKTogIik7CiAgICAgICAgc2NhbmYoIiAlYyIsICZpbnB1dCk7ICAvLyDguKPguLHguJrguITguYjguLLguIjguLLguIHguJzguLnguYnguYPguIrguYkKICAgICAgICAKICAgICAgICB0cmFuc2l0aW9uKCZjdXJyZW50U3RhdGUsIGlucHV0KTsgIC8vIOC5gOC4m+C4peC4teC5iOC4ouC4meC4quC4luC4suC4meC4sOC4leC4suC4oeC4hOC4s+C4quC4seC5iOC4h+C4l+C4teC5iOC4o+C4seC4mgogICAgfQoKICAgIHJldHVybiAwOwp9Cg==