資料結構實作

題目要求

  1. Insert a product data: 包含 prod_no, prod_name, price, quantity, total. Total金額為price x quantity 自動計算 · 新增完成後請顯示新增之後表格內的所有資訊 3.Delete a product data: 刪除該品號的所有紀錄,並顯示刪除後表格的結果
  2. Exit system:結束程式執行,離開時釋放所有記憶體,並顯示"程式結束所有記 憶體已釋放!” *請分別以call procedure來進行串列的資料處理. 顯示所有產品資訊:listProducts(); 新增串列資料: addProduct(); 刪除串列資料: deleteProduct();

Untitled

Untitled

Untitled

#include <stdio.h>
#include <stdlib.h>
#include <string.h> // Include the string.h header for strcpy

// Define the product structure
struct product {
    int code;
    char name[50];
    float price;
    int quantity;
    float total;
    struct product* next;
};

// Initialize the start pointer
struct product* start = NULL;

// Function to display all products and their details
void listProduct() {
    struct product* current = start;
    if (current == NULL) {
        printf("No products found.\\n");
        return;
    }

    printf("Product Code\\tName\\tPrice\\tQuantity\\tTotal\\n");
    while (current != NULL) {
    	if(current->code/10!=0 && current->code/100!=0){
		        printf("%d\\t\\t%s\\t%.2f\\t%d\\t\\t%.2f\\n", current->code, current->name, current->price, current->quantity, current->total);
}
    	if(current->code/10!=0 && current->code/100==0){
		        printf("0%d\\t\\t%s\\t%.2f\\t%d\\t\\t%.2f\\n", current->code, current->name, current->price, current->quantity, current->total);
}
    	else{
		        printf("00%d\\t\\t%s\\t%.2f\\t%d\\t\\t%.2f\\n", current->code, current->name, current->price, current->quantity, current->total);
}
        current = current->next;
    }
}

// Function to add a new product
void addProduct() {
    struct product* newProduct = (struct product*)malloc(sizeof(struct product));
    if (newProduct == NULL) {
        printf("Memory allocation failed.\\n");
        return;
    }

    printf("Enter Product Code: ");
    scanf("%d", &newProduct->code);
    printf("Enter Product Name: ");
    scanf("%s", newProduct->name);
    printf("Enter Product Price: ");
    scanf("%f", &newProduct->price);
    printf("Enter Product Quantity: ");
    scanf("%d", &newProduct->quantity);

    newProduct->total = newProduct->price * newProduct->quantity;
    newProduct->next = start;
    start = newProduct;
    printf("Product added successfully.\\n");
}

// Function to delete a product based on code
void deleteProduct() {
    int code;
    printf("Enter the Product Code to delete: ");
    scanf("%d", &code);

    struct product* current = start;
    struct product* prev = NULL;

    while (current != NULL && current->code != code) {
        prev = current;
        current = current->next;
    }

    if (current == NULL) {
        printf("Product not found.\\n");
        return;
    }

    if (prev == NULL) {
        start = current->next;
    } else {
        prev->next = current->next;
    }

    free(current);
    printf("Product with code %d deleted successfully.\\n", code);
}

int main() {
    // Add five initial products
    for (int i = 1; i <= 5; i++) {
        struct product* initialProduct = (struct product*)malloc(sizeof(struct product));
        if (initialProduct == NULL) {
            printf("Memory allocation failed.\\n");
            return 1;
        }

        initialProduct->code = i;
        sprintf(initialProduct->name, "Prod %c", 'A' + i - 1);
        initialProduct->price = 10.0 + i;
        initialProduct->quantity = 5 + i;
        initialProduct->total = initialProduct->price * initialProduct->quantity;
        initialProduct->next = start;
        start = initialProduct;
    }

    int choice;
    while (1) {
        printf("========Product management system========\\n");
        printf("1. Display all items & details \\n");
        printf("2. Add new product record \\n");
        printf("3. Delete Product based on Code \\n");
        printf("4. Exit Program \\n");
        printf("Please enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                listProduct();
                break;
            case 2:
                addProduct();
                break;
            case 3:
                deleteProduct();
                break;
            case 4:
                exit(0);
                break;
            default:
                printf("Wrong Choice.\\n");
                break;
        }
    }
    return 0;
}