fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX 100
  6.  
  7. char booker[MAX][50];
  8. char package[MAX][50];
  9. char date[MAX][20];
  10. int people[MAX];
  11. int count = 0;
  12.  
  13. void loadCSV() {
  14. FILE *fp = fopen("booking.csv", "r");
  15. if (!fp) return;
  16.  
  17. while (fscanf(fp, "%49[^,],%49[^,],%19[^,],%d\n",booker[count], package[count], date[count], &people[count]) == 4) {
  18. count++;
  19. }
  20. fclose(fp);
  21. }
  22.  
  23. void saveCSV() {
  24. FILE *fp = fopen("booking.csv", "w");
  25. for (int i = 0; i < count; i++) {
  26. fprintf(fp, "%s,%s,%s,%d\n", booker[i], package[i], date[i], people[i]);
  27. }
  28. fclose(fp);
  29. }
  30.  
  31. void displayAll() {
  32. for (int i = 0; i < count; i++) {
  33. printf("%d. %s | %s | %s | %d คน\n",i + 1, booker[i], package[i], date[i], people[i]);
  34. }
  35. }
  36.  
  37. void addBooking() {
  38. printf("ชื่อลูกค้า: ");
  39. scanf(" %[^\n]", booker[count]);
  40. printf("แพ็คเกจทัวร์: ");
  41. scanf(" %[^\n]", package[count]);
  42. printf("วันที่จอง: ");
  43. scanf(" %[^\n]", date[count]);
  44. printf("จำนวนคน: ");
  45. scanf("%d", &people[count]);
  46. count++;
  47. saveCSV();
  48. }
  49.  
  50. void searchBooking() {
  51. char name[50];
  52. printf("กรอกชื่อที่ต้องการค้นหา: ");
  53. scanf(" %[^\n]", name);
  54.  
  55. for (int i = 0; i < count; i++) {
  56. if (strcmp(booker[i], name) == 0) {
  57. printf("เจอ: %s | %s | %s | %d คน\n", booker[i], package[i], date[i], people[i]);
  58. return;
  59. }
  60. }
  61. printf("ไม่พบข้อมูล\n");
  62. }
  63.  
  64. void updateBooking() {
  65. char name[50];
  66. printf("กรอกชื่อที่ต้องการแก้ไข: ");
  67. scanf(" %[^\n]", name);
  68.  
  69. for (int i = 0; i < count; i++) {
  70. if (strcmp(booker[i], name) == 0) {
  71. printf("จำนวนคนใหม่: ");
  72. scanf("%d", &people[i]);
  73. saveCSV();
  74. printf("อัพเดทเรียบร้อย\n");
  75. return;
  76. }
  77. }
  78. printf("ไม่พบข้อมูล\n");
  79. }
  80.  
  81. void deleteBooking() {
  82. char name[50];
  83. printf("กรอกชื่อที่ต้องการลบ: ");
  84. scanf(" %[^\n]", name);
  85.  
  86. for (int i = 0; i < count; i++) {
  87. if (strcmp(booker[i], name) == 0) {
  88. for (int j = i; j < count - 1; j++) {
  89. strcpy(booker[j], booker[j + 1]);
  90. strcpy(package[j], package[j + 1]);
  91. strcpy(date[j], date[j + 1]);
  92. people[j] = people[j + 1];
  93. }
  94. count--;
  95. saveCSV();
  96. printf("ลบเรียบร้อย\n");
  97. return;
  98. }
  99. }
  100. printf("ไม่พบข้อมูล\n");
  101. }
  102.  
  103. void menu() {
  104. int ch;
  105. do {
  106. printf("\n===== เมนูจองทัวร์ =====\n");
  107. printf("1. แสดงข้อมูลทั้งหมด\n");
  108. printf("2. เพิ่มข้อมูลการจอง\n");
  109. printf("3. ค้นหาข้อมูล\n");
  110. printf("4. แก้ไขจำนวนคน\n");
  111. printf("5. ลบข้อมูล\n");
  112. printf("0. ออก\n");
  113. printf("เลือก: ");
  114. scanf("%d", &ch);
  115.  
  116. switch (ch) {
  117. case 1: displayAll(); break;
  118. case 2: addBooking(); break;
  119. case 3: searchBooking(); break;
  120. case 4: updateBooking(); break;
  121. case 5: deleteBooking(); break;
  122. }
  123. } while (ch != 0);
  124. }
  125.  
  126. int main() {
  127. loadCSV();
  128. menu();
  129. return 0;
  130. }
  131.  
  132.  
  133.  
  134.  
Success #stdin #stdout 0.03s 25676KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 100

char booker[MAX][50];
char package[MAX][50];
char date[MAX][20];
int people[MAX];
int count = 0;

void loadCSV() {
    FILE *fp = fopen("booking.csv", "r");
    if (!fp) return;

    while (fscanf(fp, "%49[^,],%49[^,],%19[^,],%d\n",booker[count], package[count], date[count], &people[count]) == 4) {
        count++;
    }
    fclose(fp);
}

void saveCSV() {
    FILE *fp = fopen("booking.csv", "w");
    for (int i = 0; i < count; i++) {
        fprintf(fp, "%s,%s,%s,%d\n", booker[i], package[i], date[i], people[i]);
    }
    fclose(fp);
}

void displayAll() {
    for (int i = 0; i < count; i++) {
        printf("%d. %s | %s | %s | %d คน\n",i + 1, booker[i], package[i], date[i], people[i]);
    }
}

void addBooking() {
    printf("ชื่อลูกค้า: ");
    scanf(" %[^\n]", booker[count]);
    printf("แพ็คเกจทัวร์: ");
    scanf(" %[^\n]", package[count]);
    printf("วันที่จอง: ");
    scanf(" %[^\n]", date[count]);
    printf("จำนวนคน: ");
    scanf("%d", &people[count]);
    count++;
    saveCSV();
}

void searchBooking() {
    char name[50];
    printf("กรอกชื่อที่ต้องการค้นหา: ");
    scanf(" %[^\n]", name);

    for (int i = 0; i < count; i++) {
        if (strcmp(booker[i], name) == 0) {
            printf("เจอ: %s | %s | %s | %d คน\n", booker[i], package[i], date[i], people[i]);
            return;
        }
    }
    printf("ไม่พบข้อมูล\n");
}

void updateBooking() {
    char name[50];
    printf("กรอกชื่อที่ต้องการแก้ไข: ");
    scanf(" %[^\n]", name);

    for (int i = 0; i < count; i++) {
        if (strcmp(booker[i], name) == 0) {
            printf("จำนวนคนใหม่: ");
            scanf("%d", &people[i]);
            saveCSV();
            printf("อัพเดทเรียบร้อย\n");
            return;
        }
    }
    printf("ไม่พบข้อมูล\n");
}

void deleteBooking() {
    char name[50];
    printf("กรอกชื่อที่ต้องการลบ: ");
    scanf(" %[^\n]", name);

    for (int i = 0; i < count; i++) {
        if (strcmp(booker[i], name) == 0) {
            for (int j = i; j < count - 1; j++) {
                strcpy(booker[j], booker[j + 1]);
                strcpy(package[j], package[j + 1]);
                strcpy(date[j], date[j + 1]);
                people[j] = people[j + 1];
            }
            count--;
            saveCSV();
            printf("ลบเรียบร้อย\n");
            return;
        }
    }
    printf("ไม่พบข้อมูล\n");
}

void menu() {
    int ch;
    do {
        printf("\n===== เมนูจองทัวร์ =====\n");
        printf("1. แสดงข้อมูลทั้งหมด\n");
        printf("2. เพิ่มข้อมูลการจอง\n");
        printf("3. ค้นหาข้อมูล\n");
        printf("4. แก้ไขจำนวนคน\n");
        printf("5. ลบข้อมูล\n");
        printf("0. ออก\n");
        printf("เลือก: ");
        scanf("%d", &ch);

        switch (ch) {
            case 1: displayAll(); break;
            case 2: addBooking(); break;
            case 3: searchBooking(); break;
            case 4: updateBooking(); break;
            case 5: deleteBooking(); break;
        }
    } while (ch != 0);
}

int main() {
    loadCSV();
    menu();
    return 0;
}