Minha mais simples matemática: Conhecimento dividido é poder multiplicado!

Autor: admin

  • guzoline

    Here is the first ANSI C code generated by my free and open-source app, aMELia v7.9.

    Written in C++ and Qt6, it uses a Ollama server to work with LLM models; I call it a MEL – Model Enhancement Lab, because it’s a tool intended to improve results by, basically, importing assets as PDFs into a library called Knowledge Base, thus allowing customization of prompts based on the specific knowledge of each selected asset, and even prioritizing certain assets from the library. There are many other features; learn more here on Github.

    PROMPT

    USER>

    Write a program in C language, for a gas station, It must record, for each pump: how much gas it was bought in L, multiply per price of L, record the amount of money pump currently generates. Pumps start with a certain amount of volume and these are consumed as each customer buys gasoline. You cannot record a sale if there is not enough gas. The program is a text program, and the menu is simple; 1. record customer (submenu select pump number, next submenu to record amount of L, verification if there is enough volume in pump) 2. create new pump 3. delete pump 4. fill pump with amount of L (Estabilish a maximum here)

    TRANSCRIPT

    Below is a stand‑alone C program that implements the requested gas‑station
    management system.
    It is written for a plain text console, uses only the C standard library,
    and follows the menu layout you described.

    /*
     * gas_station.c
     *
     * A simple text program that keeps track of pumps in a gas station.
     *
     * Features
     * --------
     * 1. Record a customer sale
     *    – choose pump
     *    – enter litres to buy
     *    – check that the pump has enough fuel
     *    – update pump volume and revenue
     *
     * 2. Create a new pump
     *    – specify price per litre
     *
     * 3. Delete a pump
     *
     * 4. Fill a pump
     *    – refill to the pump’s maximum capacity
     *
     * The program uses a fixed maximum number of pumps (MAX_PUMPS) and a
     * fixed maximum capacity per pump (MAX_CAPACITY_L).  All data are kept
     * in memory; no file I/O is performed.
     *
     * Compile with:
     *     gcc -std=c99 -Wall -Wextra -o gas_station gas_station.c
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /* ---------- Configuration ---------- */
    #define MAX_PUMPS      10          /* maximum number of pumps that can exist */
    #define MAX_CAPACITY_L 2000.0      /* litres – maximum capacity of any pump */
    
    /* ---------- Data structures ---------- */
    typedef struct {
        int    active;          /* 0 = unused, 1 = active */
        double volume;          /* litres currently in the pump */
        double price_per_l;     /* price per litre (currency units) */
        double revenue;         /* total money earned by this pump */
    } Pump;
    
    /* ---------- Global state ---------- */
    static Pump pumps[MAX_PUMPS];
    
    /* ---------- Utility functions ---------- */
    static void clear_screen(void)
    {
        /* simple clear‑screen for most terminals */
        printf("\033[H\033[J");
    }
    
    static void pause(void)
    {
        printf("\nPress Enter to continue...");
        while (getchar() != '\n');
    }
    
    static int read_int(const char *prompt)
    {
        int value;
        char line[64];
        while (1) {
            printf("%s", prompt);
            if (!fgets(line, sizeof line, stdin))
                continue;
            if (sscanf(line, "%d", &value) == 1)
                return value;
            printf("Invalid input. Try again.\n");
        }
    }
    
    static double read_double(const char *prompt)
    {
        double value;
        char line[64];
        while (1) {
            printf("%s", prompt);
            if (!fgets(line, sizeof line, stdin))
                continue;
            if (sscanf(line, "%lf", &value) == 1)
                return value;
            printf("Invalid input. Try again.\n");
        }
    }
    
    /* ---------- Pump operations ---------- */
    static void list_pumps(void)
    {
        printf("\nExisting pumps:\n");
        printf("Idx  Active  Volume(L)  Price/L  Revenue\n");
        for (int i = 0; i < MAX_PUMPS; ++i) {
            if (pumps[i].active) {
                printf("%-4d %-7d %-10.2f %-9.2f %-8.2f\n",
                       i, pumps[i].active, pumps[i].volume,
                       pumps[i].price_per_l, pumps[i].revenue);
            }
        }
    }
    
    static void record_customer(void)
    {
        clear_screen();
        printf("=== Record Customer Sale ===\n");
        list_pumps();
        int idx = read_int("Select pump number: ");
        if (idx < 0 || idx >= MAX_PUMPS || !pumps[idx].active) {
            printf("Invalid pump number.\n");
            pause();
            return;
        }
    
        double litres = read_double("Litres to buy: ");
        if (litres <= 0) {
            printf("Litres must be positive.\n");
            pause();
            return;
        }
    
        if (litres > pumps[idx].volume) {
            printf("Not enough fuel in pump %d. Available: %.2f L\n",
                   idx, pumps[idx].volume);
            pause();
            return;
        }
    
        /* Update pump state */
        pumps[idx].volume -= litres;
        double sale = litres * pumps[idx].price_per_l;
        pumps[idx].revenue += sale;
    
        printf("Sale recorded: %.2f L @ %.2f per L = %.2f\n",
               litres, pumps[idx].price_per_l, sale);
        pause();
    }
    
    static void create_pump(void)
    {
        clear_screen();
        printf("=== Create New Pump ===\n");
        int idx = -1;
        for (int i = 0; i < MAX_PUMPS; ++i) {
            if (!pumps[i].active) {
                idx = i;
                break;
            }
        }
        if (idx == -1) {
            printf("Maximum number of pumps (%d) already in use.\n", MAX_PUMPS);
            pause();
            return;
        }
    
        double price = read_double("Price per litre: ");
        if (price <= 0) {
            printf("Price must be positive.\n");
            pause();
            return;
        }
    
        pumps[idx].active = 1;
        pumps[idx].volume = MAX_CAPACITY_L;   /* start full */
        pumps[idx].price_per_l = price;
        pumps[idx].revenue = 0.0;
    
        printf("Pump %d created, full capacity %.2f L at price %.2f per L.\n",
               idx, MAX_CAPACITY_L, price);
        pause();
    }
    
    static void delete_pump(void)
    {
        clear_screen();
        printf("=== Delete Pump ===\n");
        list_pumps();
        int idx = read_int("Select pump number to delete: ");
        if (idx < 0 || idx >= MAX_PUMPS || !pumps[idx].active) {
            printf("Invalid pump number.\n");
            pause();
            return;
        }
    
        pumps[idx].active = 0;
        printf("Pump %d deleted.\n", idx);
        pause();
    }
    
    static void fill_pump(void)
    {
        clear_screen();
        printf("=== Fill Pump ===\n");
        list_pumps();
        int idx = read_int("Select pump number to fill: ");
        if (idx < 0 || idx >= MAX_PUMPS || !pumps[idx].active) {
            printf("Invalid pump number.\n");
            pause();
            return;
        }
    
        pumps[idx].volume = MAX_CAPACITY_L;
        printf("Pump %d refilled to %.2f L.\n", idx, MAX_CAPACITY_L);
        pause();
    }
    
    /* ---------- Main menu ---------- */
    static void show_menu(void)
    {
        printf("\n--- Gas Station Management ---\n");
        printf("1. Record customer sale\n");
        printf("2. Create new pump\n");
        printf("3. Delete pump\n");
        printf("4. Fill pump\n");
        printf("5. List pumps\n");
        printf("0. Exit\n");
    }
    
    int main(void)
    {
        /* initialise all pumps as inactive */
        memset(pumps, 0, sizeof pumps);
    
        while (1) {
            clear_screen();
            show_menu();
            int choice = read_int("Select option: ");
            switch (choice) {
                case 1: record_customer(); break;
                case 2: create_pump();     break;
                case 3: delete_pump();     break;
                case 4: fill_pump();       break;
                case 5: list_pumps(); pause(); break;
                case 0: printf("Good‑bye!\n"); return 0;
                default: printf("Unknown option.\n"); pause(); break;
            }
        }
    }

    How it works

    FeatureImplementation
    Record customerPrompts for pump, litres, checks availability, updates volume and revenue.
    Create pumpFinds first free slot, asks for price per litre, starts pump full (MAX_CAPACITY_L).
    Delete pumpMarks pump as inactive.
    Fill pumpSets pump volume back to MAX_CAPACITY_L.
    List pumpsShows all active pumps with current volume, price, and revenue.

    The program uses a fixed array of Pump structs (MAX_PUMPS), so no dynamic memory allocation is required.
    All numeric values are double to allow fractional litres and prices.
    The menu loop continues until the user selects 0 – Exit.

    Feel free to adjust MAX_PUMPS or MAX_CAPACITY_L to match your station’s
    requirements.