#include <stdio.h>
// ฟังก์ชันสำหรับการคำนวณ
float add(float x, float y) {
return x + y;
}
float subtract(float x, float y) {
return x - y;
}
float multiply(float x, float y) {
return x * y;
}
float divide(float x, float y) {
if (y != 0) {
return x / y;
} else {
printf("ไม่สามารถหารด้วยศูนย์ได้!\n"); return 0;
}
}
int main() {
int choice;
float num1, num2;
// แสดงเมนูให้เลือก
printf("กรุณาเลือกการคำนวณ (1/2/3/4): "); scanf("%d", &choice);
// รับค่าตัวเลขจากผู้ใช้
printf("กรุณาใส่ตัวเลขตัวแรก: "); scanf("%f", &num1);
printf("กรุณาใส่ตัวเลขตัวที่สอง: "); scanf("%f", &num2);
// คำนวณตามที่เลือก
switch(choice) {
case 1:
printf("%.2f + %.2f = %.2f\n", num1
, num2
, add
(num1
, num2
)); break;
case 2:
printf("%.2f - %.2f = %.2f\n", num1
, num2
, subtract
(num1
, num2
)); break;
case 3:
printf("%.2f * %.2f = %.2f\n", num1
, num2
, multiply
(num1
, num2
)); break;
case 4:
printf("%.2f / %.2f = %.2f\n", num1
, num2
, divide
(num1
, num2
)); break;
default:
printf("การเลือกไม่ถูกต้อง\n"); break;
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgovLyDguJ/guLHguIfguIHguYzguIrguLHguJnguKrguLPguKvguKPguLHguJrguIHguLLguKPguITguLPguJnguKfguJMKZmxvYXQgYWRkKGZsb2F0IHgsIGZsb2F0IHkpIHsKICAgIHJldHVybiB4ICsgeTsKfQoKZmxvYXQgc3VidHJhY3QoZmxvYXQgeCwgZmxvYXQgeSkgewogICAgcmV0dXJuIHggLSB5Owp9CgpmbG9hdCBtdWx0aXBseShmbG9hdCB4LCBmbG9hdCB5KSB7CiAgICByZXR1cm4geCAqIHk7Cn0KCmZsb2F0IGRpdmlkZShmbG9hdCB4LCBmbG9hdCB5KSB7CiAgICBpZiAoeSAhPSAwKSB7CiAgICAgICAgcmV0dXJuIHggLyB5OwogICAgfSBlbHNlIHsKICAgICAgICBwcmludGYoIuC5hOC4oeC5iOC4quC4suC4oeC4suC4o+C4luC4q+C4suC4o+C4lOC5ieC4p+C4ouC4qOC4ueC4meC4ouC5jOC5hOC4lOC5iSFcbiIpOwogICAgICAgIHJldHVybiAwOwogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGludCBjaG9pY2U7CiAgICBmbG9hdCBudW0xLCBudW0yOwoKICAgIC8vIOC5geC4quC4lOC4h+C5gOC4oeC4meC4ueC5g+C4q+C5ieC5gOC4peC4t+C4reC4gQogICAgcHJpbnRmKCLguYDguKXguLfguK3guIHguIHguLLguKPguITguLPguJnguKfguJM6XG4iKTsKICAgIHByaW50ZigiMS4g4Lia4Lin4LiBICgrKVxuIik7CiAgICBwcmludGYoIjIuIOC4peC4miAoLSlcbiIpOwogICAgcHJpbnRmKCIzLiDguITguLnguJMgKCopXG4iKTsKICAgIHByaW50ZigiNC4g4Lir4Liy4LijICgvKVxuIik7CiAgICBwcmludGYoIuC4geC4o+C4uOC4k+C4suC5gOC4peC4t+C4reC4geC4geC4suC4o+C4hOC4s+C4meC4p+C4kyAoMS8yLzMvNCk6ICIpOwogICAgc2NhbmYoIiVkIiwgJmNob2ljZSk7CgogICAgLy8g4Lij4Lix4Lia4LiE4LmI4Liy4LiV4Lix4Lin4LmA4Lil4LiC4LiI4Liy4LiB4Lic4Li54LmJ4LmD4LiK4LmJCiAgICBwcmludGYoIuC4geC4o+C4uOC4k+C4suC5g+C4quC5iOC4leC4seC4p+C5gOC4peC4guC4leC4seC4p+C5geC4o+C4gTogIik7CiAgICBzY2FuZigiJWYiLCAmbnVtMSk7CiAgICBwcmludGYoIuC4geC4o+C4uOC4k+C4suC5g+C4quC5iOC4leC4seC4p+C5gOC4peC4guC4leC4seC4p+C4l+C4teC5iOC4quC4reC4hzogIik7CiAgICBzY2FuZigiJWYiLCAmbnVtMik7CgogICAgLy8g4LiE4Liz4LiZ4Lin4LiT4LiV4Liy4Lih4LiX4Li14LmI4LmA4Lil4Li34Lit4LiBCiAgICBzd2l0Y2goY2hvaWNlKSB7CiAgICAgICAgY2FzZSAxOgogICAgICAgICAgICBwcmludGYoIiUuMmYgKyAlLjJmID0gJS4yZlxuIiwgbnVtMSwgbnVtMiwgYWRkKG51bTEsIG51bTIpKTsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgY2FzZSAyOgogICAgICAgICAgICBwcmludGYoIiUuMmYgLSAlLjJmID0gJS4yZlxuIiwgbnVtMSwgbnVtMiwgc3VidHJhY3QobnVtMSwgbnVtMikpOwogICAgICAgICAgICBicmVhazsKICAgICAgICBjYXNlIDM6CiAgICAgICAgICAgIHByaW50ZigiJS4yZiAqICUuMmYgPSAlLjJmXG4iLCBudW0xLCBudW0yLCBtdWx0aXBseShudW0xLCBudW0yKSk7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIGNhc2UgNDoKICAgICAgICAgICAgcHJpbnRmKCIlLjJmIC8gJS4yZiA9ICUuMmZcbiIsIG51bTEsIG51bTIsIGRpdmlkZShudW0xLCBudW0yKSk7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgIHByaW50Zigi4LiB4Liy4Lij4LmA4Lil4Li34Lit4LiB4LmE4Lih4LmI4LiW4Li54LiB4LiV4LmJ4Lit4LiHXG4iKTsKICAgICAgICAgICAgYnJlYWs7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K
#include <stdio.h>
// ฟังก์ชันสำหรับการคำนวณ
float add(float x, float y) {
return x + y;
}
float subtract(float x, float y) {
return x - y;
}
float multiply(float x, float y) {
return x * y;
}
float divide(float x, float y) {
if (y != 0) {
return x / y;
} else {
printf("ไม่สามารถหารด้วยศูนย์ได้!\n");
return 0;
}
}
int main() {
int choice;
float num1, num2;
// แสดงเมนูให้เลือก
printf("เลือกการคำนวณ:\n");
printf("1. บวก (+)\n");
printf("2. ลบ (-)\n");
printf("3. คูณ (*)\n");
printf("4. หาร (/)\n");
printf("กรุณาเลือกการคำนวณ (1/2/3/4): ");
scanf("%d", &choice);
// รับค่าตัวเลขจากผู้ใช้
printf("กรุณาใส่ตัวเลขตัวแรก: ");
scanf("%f", &num1);
printf("กรุณาใส่ตัวเลขตัวที่สอง: ");
scanf("%f", &num2);
// คำนวณตามที่เลือก
switch(choice) {
case 1:
printf("%.2f + %.2f = %.2f\n", num1, num2, add(num1, num2));
break;
case 2:
printf("%.2f - %.2f = %.2f\n", num1, num2, subtract(num1, num2));
break;
case 3:
printf("%.2f * %.2f = %.2f\n", num1, num2, multiply(num1, num2));
break;
case 4:
printf("%.2f / %.2f = %.2f\n", num1, num2, divide(num1, num2));
break;
default:
printf("การเลือกไม่ถูกต้อง\n");
break;
}
return 0;
}