fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define STUDENT_COUNT 5
  5.  
  6. // โครงสร้างนักเรียน
  7. typedef struct {
  8. char name[100];
  9. int age;
  10.  
  11. int physicsScore;
  12. int mathScore;
  13. float physicsGPA;
  14. float mathGPA;
  15. } Student;
  16.  
  17. // ฟังก์ชันให้เกรด GPA แบบไทย
  18. float calculateThaiGPA(int score) {
  19. if (score >= 80) return 4.0;
  20. else if (score >= 75) return 3.5;
  21. else if (score >= 70) return 3.0;
  22. else if (score >= 65) return 2.5;
  23. else if (score >= 60) return 2.0;
  24. else if (score >= 55) return 1.5;
  25. else if (score >= 50) return 1.0;
  26. else return 0.0;
  27. }
  28.  
  29. // ฟังก์ชันตรวจสอบคะแนน
  30. int isValidScore(int score) {
  31. return (score >= 0 && score <= 100);
  32. }
  33.  
  34. int main() {
  35. Student students[STUDENT_COUNT];
  36.  
  37. for (int i = 0; i < STUDENT_COUNT; i++) {
  38. printf("กรอกข้อมูลนักเรียนคนที่ %d\n", i + 1);
  39.  
  40. printf("ชื่อ: ");
  41. getchar(); // รับ newline ค้าง
  42. fgets(students[i].name, sizeof(students[i].name), stdin);
  43. students[i].name[strcspn(students[i].name, "\n")] = 0; // ลบ newline
  44.  
  45. printf("อายุ: ");
  46. scanf("%d", &students[i].age);
  47.  
  48. // รับคะแนนฟิสิกส์
  49. do {
  50. printf("คะแนนรวมฟิสิกส์ (เต็ม 100): ");
  51. scanf("%d", &students[i].physicsScore);
  52.  
  53. if (!isValidScore(students[i].physicsScore)) {
  54. printf("❌ คะแนนไม่ถูกต้อง ต้องอยู่ระหว่าง 0 ถึง 100\n");
  55. }
  56.  
  57. } while (!isValidScore(students[i].physicsScore));
  58.  
  59. // รับคะแนนคณิตศาสตร์
  60. do {
  61. printf("คะแนนรวมคณิตศาสตร์ (เต็ม 100): ");
  62. scanf("%d", &students[i].mathScore);
  63.  
  64. if (!isValidScore(students[i].mathScore)) {
  65. printf("❌ คะแนนไม่ถูกต้อง ต้องอยู่ระหว่าง 0 ถึง 100\n");
  66. }
  67.  
  68. } while (!isValidScore(students[i].mathScore));
  69.  
  70. // คำนวณเกรด
  71. students[i].physicsGPA = calculateThaiGPA(students[i].physicsScore);
  72. students[i].mathGPA = calculateThaiGPA(students[i].mathScore);
  73.  
  74. printf("\n");
  75. }
  76.  
  77. // แสดงผล
  78. printf("\n📚 รายงานผลนักเรียนทั้งหมด:\n");
  79. for (int i = 0; i < STUDENT_COUNT; i++) {
  80. printf("\nนักเรียนคนที่ %d\n", i + 1);
  81. printf("ชื่อ: %s\n", students[i].name);
  82. printf("อายุ: %d ปี\n", students[i].age);
  83. printf("คะแนนฟิสิกส์: %d | เกรด: %.1f\n", students[i].physicsScore, students[i].physicsGPA);
  84. printf("คะแนนคณิตศาสตร์: %d | เกรด: %.1f\n", students[i].mathScore, students[i].mathGPA);
  85. printf("----------------------------------\n");
  86. }
  87.  
  88. return 0;
  89. }
  90.  
Success #stdin #stdout 0.03s 25836KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <string.h>

#define STUDENT_COUNT 5

// โครงสร้างนักเรียน
typedef struct {
    char name[100];
    int age;

    int physicsScore;
    int mathScore;
    float physicsGPA;
    float mathGPA;
} Student;

// ฟังก์ชันให้เกรด GPA แบบไทย
float calculateThaiGPA(int score) {
    if (score >= 80) return 4.0;
    else if (score >= 75) return 3.5;
    else if (score >= 70) return 3.0;
    else if (score >= 65) return 2.5;
    else if (score >= 60) return 2.0;
    else if (score >= 55) return 1.5;
    else if (score >= 50) return 1.0;
    else return 0.0;
}

// ฟังก์ชันตรวจสอบคะแนน
int isValidScore(int score) {
    return (score >= 0 && score <= 100);
}

int main() {
    Student students[STUDENT_COUNT];

    for (int i = 0; i < STUDENT_COUNT; i++) {
        printf("กรอกข้อมูลนักเรียนคนที่ %d\n", i + 1);

        printf("ชื่อ: ");
        getchar(); // รับ newline ค้าง
        fgets(students[i].name, sizeof(students[i].name), stdin);
        students[i].name[strcspn(students[i].name, "\n")] = 0; // ลบ newline

        printf("อายุ: ");
        scanf("%d", &students[i].age);

        // รับคะแนนฟิสิกส์
        do {
            printf("คะแนนรวมฟิสิกส์ (เต็ม 100): ");
            scanf("%d", &students[i].physicsScore);

            if (!isValidScore(students[i].physicsScore)) {
                printf("❌ คะแนนไม่ถูกต้อง ต้องอยู่ระหว่าง 0 ถึง 100\n");
            }

        } while (!isValidScore(students[i].physicsScore));

        // รับคะแนนคณิตศาสตร์
        do {
            printf("คะแนนรวมคณิตศาสตร์ (เต็ม 100): ");
            scanf("%d", &students[i].mathScore);

            if (!isValidScore(students[i].mathScore)) {
                printf("❌ คะแนนไม่ถูกต้อง ต้องอยู่ระหว่าง 0 ถึง 100\n");
            }

        } while (!isValidScore(students[i].mathScore));

        // คำนวณเกรด
        students[i].physicsGPA = calculateThaiGPA(students[i].physicsScore);
        students[i].mathGPA = calculateThaiGPA(students[i].mathScore);

        printf("\n");
    }

    // แสดงผล
    printf("\n📚 รายงานผลนักเรียนทั้งหมด:\n");
    for (int i = 0; i < STUDENT_COUNT; i++) {
        printf("\nนักเรียนคนที่ %d\n", i + 1);
        printf("ชื่อ: %s\n", students[i].name);
        printf("อายุ: %d ปี\n", students[i].age);
        printf("คะแนนฟิสิกส์: %d | เกรด: %.1f\n", students[i].physicsScore, students[i].physicsGPA);
        printf("คะแนนคณิตศาสตร์: %d | เกรด: %.1f\n", students[i].mathScore, students[i].mathGPA);
        printf("----------------------------------\n");
    }

    return 0;
}