fork download
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. int i, j;
  6. long dec; /* ให้รับค่าอินพุทแบบ Long Integer - เลขจำนวนเต็มแบบยาว */
  7. int bit[32]; /* จองพื้นที่ในการเก็บข้อมูลเลขฐาน 2 ลงใน Array */
  8.  
  9. clrscr(); /* เคลียร์หน้าจอ */
  10. printf("Decimal Number : "); /* แจ้งผู้ใช้เพื่อเตรียมป้อนค่าเลขฐาน 10 */
  11. scanf("%ld", &dec); /* ต้องใช้ ld เพราะ Input มันเป็นแบบ Long Integer */
  12. i = 0; /* กำหนดค่าเริ่มต้นของ Array */
  13. /* ทำตามที่ได้ออกแบบโปรแกรมเอาไว้ ... ยังไงยังงั้นเลย 55555+ */
  14. do {
  15. bit[i++] = dec % 2; /* การหารเอาเศษ เพื่อให้เป็นคำตอบ */
  16.  
  17. /* การหารทั่วไป แต่ตัวแปร dec ของภาษา C มันเป็น Integer หรือ เลขจำนวนเต็ม */
  18. /* ดังนั้นมันจึงตัดเศษ (หรือทศนิยม) ทิ้งไปโดยอัตโนมัติ */
  19. dec = dec / 2;
  20.  
  21. } while (dec > 0); /* เงื่อนไขที่ทำจนกระทั่ง dec = 0 ก็ออกจากวังวนเงื่อนไข */
  22.  
  23. /* การแสดงผลของการแปลงเลขฐาน 10 เป็นเลขฐาน 2*/
  24. /* j = i - 1 และให้ j ลดค่าลงทีละ 1 ... ก็คืออ่านข้อมูลถอยหลังกลับเท่านั้นเองครับ */
  25. /* เพราะตัวแปรแบบ Array ในภาษา C มันเก็บข้อมูลจากซ้ายไปขวา */
  26. /* ทำให้ LSB มันไปอยู่ทางซ้าย ส่วน MSB มันไปอยู่ทางขวา */
  27. for(j = i - 1; j >= 0; j--)
  28. printf("%d", bit[j]);
  29.  
  30. printf("\n");
  31. return 0;
  32.  
  33. }
Success #stdin #stdout 0.02s 25552KB
stdin
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_USERS 100
#define MAX_PRODUCTS 5
#define MAX_CART 10

// โครงสร้างข้อมูลผู้ใช้
typedef struct {
    char name[50];
    char phone[15];
    char password[20];
} Customer;

// โครงสร้างสินค้า
typedef struct {
    char name[50];
    float price;
} Product;

// โครงสร้างตะกร้าสินค้า
typedef struct {
    Product items[MAX_CART];
    int quantities[MAX_CART];
    int count;
} Cart;

// ตัวแปรเก็บข้อมูล
Customer customers[MAX_USERS];
int userCount = 0;
Product products[MAX_PRODUCTS] = {
    {"เสื้อยืด", 250.00},
    {"กางเกงยีนส์", 500.00},
    {"รองเท้า", 1200.00},
    {"หมวก", 150.00},
    {"กระเป๋า", 800.00}
};
Cart cart = { .count = 0 };

// ฟังก์ชันสมัครสมาชิก
void registerUser() {
    printf("=== สมัครสมาชิก ===\n");
    printf("ชื่อ: ");
    scanf(" %[^\n]", customers[userCount].name);
    printf("เบอร์โทร: ");
    scanf("%s", customers[userCount].phone);
    printf("รหัสผ่าน: ");
    scanf("%s", customers[userCount].password);

    userCount++;
    printf("สมัครสมาชิกสำเร็จ!\n");
}

// ฟังก์ชันล็อกอิน
int loginUser() {
    char phone[15], password[20];
    printf("=== ล็อกอิน ===\n");
    printf("เบอร์โทร: ");
    scanf("%s", phone);
    printf("รหัสผ่าน: ");
    scanf("%s", password);

    for (int i = 0; i < userCount; i++) {
        if (strcmp(customers[i].phone, phone) == 0 && strcmp(customers[i].password, password) == 0) {
            printf("เข้าสู่ระบบสำเร็จ! ยินดีต้อนรับ %s\n", customers[i].name);
            return 1;
        }
    }
    printf("เบอร์โทรหรือรหัสผ่านผิด!\n");
    return 0;
}

// ฟังก์ชันเลือกสินค้าใส่ตะกร้า
void placeOrder() {
    int choice, qty;
    printf("=== เลือกสินค้าที่ต้องการซื้อ ===\n");
    for (int i = 0; i < MAX_PRODUCTS; i++) {
        printf("%d. %s - %.2f บาท\n", i + 1, products[i].name, products[i].price);
    }

    while (1) {
        printf("เลือกสินค้า (1-%d, 0 = เสร็จสิ้น): ", MAX_PRODUCTS);
        scanf("%d", &choice);
        
        if (choice == 0) break;
        if (choice < 1 || choice > MAX_PRODUCTS) {
            printf("เลือกสินค้าไม่ถูกต้อง!\n");
            continue;
        }

        printf("จำนวน: ");
        scanf("%d", &qty);
        
        if (qty <= 0) {
            printf("จำนวนต้องมากกว่า 0\n");
            continue;
        }

        int found = 0;
        for (int i = 0; i < cart.count; i++) {
            if (strcmp(cart.items[i].name, products[choice - 1].name) == 0) {
                cart.quantities[i] += qty;
                found = 1;
                break;
            }
        }
        
        if (!found && cart.count < MAX_CART) {
            cart.items[cart.count] = products[choice - 1];
            cart.quantities[cart.count] = qty;
            cart.count++;
        }

        printf("เพิ่ม %s จำนวน %d ชิ้น ลงในตะกร้า\n", products[choice - 1].name, qty);
    }
}

// ฟังก์ชันแสดงรายการสินค้าในตะกร้า
void viewCart() {
    if (cart.count == 0) {
        printf("ตะกร้าสินค้าว่างเปล่า\n");
        return;
    }

    printf("=== ตะกร้าสินค้า ===\n");
    float total = 0;
    for (int i = 0; i < cart.count; i++) {
        float price = cart.items[i].price * cart.quantities[i];
        printf("%d. %s x %d ชิ้น - %.2f บาท\n", i + 1, cart.items[i].name, cart.quantities[i], price);
        total += price;
    }
    printf("ราคารวม: %.2f บาท\n", total);
}

// ฟังก์ชันเพิ่ม/ลดจำนวนสินค้าในตะกร้า
void updateCart() {
    int choice, qty;
    if (cart.count == 0) {
        printf("ตะกร้าสินค้าว่างเปล่า\n");
        return;
    }

    viewCart();
    printf("เลือกสินค้าที่ต้องการอัปเดต (1-%d, 0 = ยกเลิก): ", cart.count);
    scanf("%d", &choice);
    
    if (choice == 0) return;
    if (choice < 1 || choice > cart.count) {
        printf("เลือกสินค้าไม่ถูกต้อง!\n");
        return;
    }

    printf("ป้อนจำนวนใหม่ (0 = ลบออก): ");
    scanf("%d", &qty);

    if (qty == 0) {
        for (int i = choice - 1; i < cart.count - 1; i++) {
            cart.items[i] = cart.items[i + 1];
            cart.quantities[i] = cart.quantities[i + 1];
        }
        cart.count--;
        printf("ลบสินค้าจากตะกร้าเรียบร้อย!\n");
    } else {
        cart.quantities[choice - 1] = qty;
        printf("อัปเดตจำนวนสินค้าเรียบร้อย!\n");
    }
}

// ฟังก์ชันพิมพ์ใบเสร็จ
void printReceipt() {
    if (cart.count == 0) {
        printf("ไม่มีสินค้าที่ต้องออกใบเสร็จ\n");
        return;
    }

    printf("=== ใบเสร็จ ===\n");
    float total = 0;
    for (int i = 0; i < cart.count; i++) {
        float price = cart.items[i].price * cart.quantities[i];
        printf("%d. %s x %d ชิ้น - %.2f บาท\n", i + 1, cart.items[i].name, cart.quantities[i], price);
        total += price;
    }
    printf("ราคารวมทั้งหมด: %.2f บาท\n", total);
    printf("ขอบคุณที่ใช้บริการ!\n");

    cart.count = 0; // ล้างตะกร้าหลังออกใบเสร็จ
}

// ฟังก์ชันเมนูหลัก
void mainMenu() {
    int choice;
    do {
        printf("\n=== ระบบผู้ซื้อ ===\n");
        printf("1. สมัครสมาชิก\n");
        printf("2. ล็อกอิน\n");
        printf("3. เลือกซื้อสินค้า\n");
        printf("4. ดูตะกร้าสินค้า\n");
        printf("5. แก้ไขตะกร้าสินค้า\n");
        printf("6. พิมพ์ใบเสร็จ\n");
        printf("7. ออกจากระบบ\n");
        printf("เลือกเมนู: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1: registerUser(); break;
            case 2: loginUser(); break;
            case 3: placeOrder(); break;
            case 4: viewCart(); break;
            case 5: updateCart(); break;
            case 6: printReceipt(); break;
            case 7: exit(0);
            default: printf("เลือกเมนูไม่ถูกต้อง!\n");
        }
    } while (1);
}

int main() {
    mainMenu();
    return 0;
}
stdout
#include <stdio.h>

int main(void)
{
int i, j;
long dec;  /* ให้รับค่าอินพุทแบบ Long Integer - เลขจำนวนเต็มแบบยาว */
int bit[32];  /* จองพื้นที่ในการเก็บข้อมูลเลขฐาน 2 ลงใน Array */

    clrscr();  /* เคลียร์หน้าจอ */
    printf("Decimal Number : ");  /* แจ้งผู้ใช้เพื่อเตรียมป้อนค่าเลขฐาน 10 */
    scanf("%ld", &dec);  /* ต้องใช้ ld เพราะ Input มันเป็นแบบ Long Integer */
    i = 0;  /* กำหนดค่าเริ่มต้นของ Array */
    /* ทำตามที่ได้ออกแบบโปรแกรมเอาไว้ ... ยังไงยังงั้นเลย 55555+ */
    do {
        bit[i++] = dec % 2;  /* การหารเอาเศษ เพื่อให้เป็นคำตอบ */

        /* การหารทั่วไป แต่ตัวแปร dec ของภาษา C มันเป็น Integer หรือ เลขจำนวนเต็ม */
        /* ดังนั้นมันจึงตัดเศษ (หรือทศนิยม) ทิ้งไปโดยอัตโนมัติ */
        dec = dec / 2;

    } while (dec > 0);  /* เงื่อนไขที่ทำจนกระทั่ง dec = 0 ก็ออกจากวังวนเงื่อนไข */

    /* การแสดงผลของการแปลงเลขฐาน 10 เป็นเลขฐาน 2*/
    /* j = i - 1 และให้ j ลดค่าลงทีละ 1 ... ก็คืออ่านข้อมูลถอยหลังกลับเท่านั้นเองครับ */
    /* เพราะตัวแปรแบบ Array ในภาษา C มันเก็บข้อมูลจากซ้ายไปขวา */
    /* ทำให้ LSB มันไปอยู่ทางซ้าย ส่วน MSB มันไปอยู่ทางขวา */
    for(j = i - 1; j >= 0; j--)
        printf("%d", bit[j]);

printf("\n");
return 0;

}