Thread: Item Library(business library)

  1. #1
    Registered User LísiasDeCastro's Avatar
    Join Date
    Aug 2013
    Location
    Brasil/São Paulo
    Posts
    27

    Wink Item Library(business library)

    This library is a part of a system that I am doing.
    It's to control the item in stock of a sort of business.
    If someone misses some variable and could suggest I
    would be thankfull.

    Item.h
    Code:
    #ifndef ITEM_H
    #define ITEM_H
    
    
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    
    #ifdef __cplusplus
    extern "C" {
    #endif /// __cplusplus
    
    
        typedef enum {
            Off, On
        } STATUS;
    
    
        typedef struct Date DATE;
    
    
        struct Date {
            char day, month;
            int year;
        };
    
    
        DATE date_set(char day, char month, int year);
    
    
        typedef struct Item ITEM;
    
    
        struct Item {
            char * description;
            double price, interest;
            long int barcode, lot, amount;
            DATE manufacture, maturity, buy;
            ITEM * preview, *next;
        };
    
    
        ITEM * item_set(char * description, double price, double interest, long int lot, long int amount, long int barcode, DATE manufacture, DATE maturity, DATE buy);
        STATUS item_next_front(ITEM ** to, ITEM * next);
        STATUS item_next_back(ITEM ** to, ITEM * next);
        ITEM * item_search(ITEM * to, long int barcode);
        void item_show_console(ITEM * to);
        STATUS item_remove(ITEM ** to, long int barcode);
        STATUS item_destroy(ITEM * to);
    
    
    #ifdef __cplusplus
    }
    #endif /// __cplusplus
    
    
    #endif /// ITEM_H
    Item.c
    Code:
    #include "item.h"
    
    
    DATE date_set(char day, char month, int year) {
        DATE to;
        to.day = day;
        to.month = month;
        to.year = year;
        return (to);
    }
    
    
    ITEM * item_set(char * description, double price, double interest, long int lot, long int amount, long int barcode, DATE manufacture, DATE maturity, DATE buy) {
        ITEM * to = (ITEM *) malloc(sizeof (ITEM));
        to->description = (char *) calloc((1 + strlen(description)), sizeof (char));
        sprintf(to->description, "%s", description);
        to->price = price;
        to->interest = interest;
        to->barcode = barcode;
        to->amount = amount;
        to->lot = lot;
        to->manufacture = manufacture;
        to->maturity = maturity;
        to->buy = buy;
        to->preview = to->next = NULL;
        return (to);
    }
    
    
    STATUS item_next_front(ITEM ** to, ITEM * next) {
        if (next == NULL || item_search(*to, next->barcode))return (Off);
        next->next = *to;
        if (*to != NULL) {
            (*to)->preview = next;
        }
        *to = next;
        return (On);
    }
    
    
    STATUS item_next_back(ITEM ** to, ITEM * next) {
        if (next == NULL || item_search(*to, next->barcode))return (Off);
        if (*to != NULL) {
            ITEM * update = *to, * preview = NULL;
            while (update->next != NULL) {
                preview = update;
                update = update->next;
            }
            update->preview = preview;
            update->next = next;
            return (On);
        }
        *to = next;
        return (On);
    }
    
    
    ITEM * item_search(ITEM * to, long int barcode) {
        if (to != NULL) {
            ITEM * update;
            for (update = to; update != NULL; update = update->next) {
                /// printf("###%li %li\n",update->id,id);
                if (update->barcode == barcode) {
                    return (update);
                }
            }
        }
        return (NULL);
    }
    
    
    void item_show_console(ITEM * to) {
        printf("Description: %s\n"
                "Interest(%%): %lf\n"
                "Price: %lf\n"
                "Lot: %ld\n"
                "Amount: %li\n"
                "Subtotal: %lf\n"
                "Manufacture: (%d/%d/%d)\n"
                "Maturity: (%d/%d/%d)\n"
                "Buy: (%d/%d/%d)\n", to->description, to->interest, to->price, to->lot, to->amount,(to->price*to->amount)
                , to->manufacture.day, to->manufacture.month, to->manufacture.year
                , to->maturity.day, to->maturity.month, to->maturity.year
                , to->buy.day, to->buy.month, to->buy.year);
    }
    
    
    STATUS item_remove(ITEM ** to, long int barcode) {
        ITEM * preview = NULL, *update = *to;
        while (update != NULL && update->barcode != barcode) {
            preview = update;
            update = update->next;
        }
        if (update == NULL)return (Off);
        if (preview == NULL)*to = update->next;
        else preview->next = update->next;
        return (On);
    }
    
    
    STATUS item_destroy(ITEM * to) {
        if (to == NULL)return (Off);
        ITEM * next = NULL;
        while (to != NULL) {
            next = to;
            free(to), to = to->next;
        }
        return (On);
    }
    Main.c
    Code:
    /* 
     * File:   main.c
     * Author: Bean Dragon
     *
     * Created on 5 de Junho de 2015, 21:57
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #include "item.h"
    
    
    int main(int c, char ** v) {
        double price = 5.0, interest = 30.0;
        long int lot = 0, amount = 10, barcode = 0;
        ITEM * item = item_set("Item 0", price, interest, lot, amount, barcode, date_set(5, 6, 2015), date_set(5, 7, 2015), date_set(5, 6, 2015)), * next;
        interest = 20.0,amount = 25,barcode = 1;
        item_next_front(&item, item_set("Item 1", price, interest, lot, amount, barcode, date_set(5, 6, 2015), date_set(5, 7, 2015), date_set(5, 6, 2015)));
        for (next = item; next != NULL; next = next->next)
            item_show_console(next), printf("\n");
        return (0);
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    FAQ > Casting malloc - Cprogramming.com

    On line 16 of Item.c why do you use sprintf for simple strcpy? It is overkill. sprintf is rather complex function.

    Code:
    next = to;
    free(to), to = to->next;
    should be

    Code:
    next = to->next;
    free(to);
    to = next;
    Your original code is accessing freed pointer - the result is undefined behaviour

    by preview pointer do you mean previous? If yes - preview name is somewhat misleading
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User LísiasDeCastro's Avatar
    Join Date
    Aug 2013
    Location
    Brasil/São Paulo
    Posts
    27

    Cool

    well, about the sprintf I would consider. Because it should speed the code up a little bit. About the free that was my mistake, I didn't see that I setted to->next up instead next. so I am gona replace the code here.

    fixed
    Code:
    STATUS item_destroy(ITEM * to) {
        if (to == NULL)return (Off);
        ITEM * next = NULL;
        while (to != NULL) {
            next = to;
            free(to->name);
            free(to), to = next;
        }
        return (On);
    }
    Thanks for suggestions

    Ahhh. And on the main I forgot to call item_destroy to deallocate the whole thing causing a leck memory. Sorry about that.

    so.
    Code:
    /* 
     * File:   main.c
     * Author: Bean Dragon
     *
     * Created on 5 de Junho de 2015, 21:57
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #include "item.h"
    
    
    int main(int c, char ** v) {
        double price = 5.0, interest = 30.0;
        long int lot = 0, amount = 10, barcode = 0;
        ITEM * item = item_set("Item 0", price, interest, lot, amount, barcode, date_set(5, 6, 2015), date_set(5, 7, 2015), date_set(5, 6, 2015)), * next;
        interest = 20.0,amount = 25,barcode = 1;
        item_next_front(&item, item_set("Item 1", price, interest, lot, amount, barcode, date_set(5, 6, 2015), date_set(5, 7, 2015), date_set(5, 6, 2015)));
        for (next = item; next != NULL; next = next->next)
            item_show_console(next), printf("\n");
        item_destroy(item);
        return (0);
    }
    Last edited by LísiasDeCastro; 06-08-2015 at 12:15 AM.

  4. #4
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Also, there is no point of having parenthesis around your return values - 'return' is not a function so unless you are casting the return value you shouldn't have them.

  5. #5
    Registered User LísiasDeCastro's Avatar
    Join Date
    Aug 2013
    Location
    Brasil/São Paulo
    Posts
    27
    I don't think this affects the behavior of the program. At least I could see no problem at all since I changed to this style.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    while (to != NULL) {
            next = to;
            free(to->name);
            free(to), to = next;
        }
    should be next = to->next;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User LísiasDeCastro's Avatar
    Join Date
    Aug 2013
    Location
    Brasil/São Paulo
    Posts
    27
    Exacly. That was my fault too. I've fixed that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Library Symbol (String library)
    By LísiasDeCastro in forum C Programming
    Replies: 0
    Last Post: 06-06-2015, 01:35 PM
  2. Replies: 9
    Last Post: 02-08-2012, 10:23 PM
  3. Static library vs Dynamic library
    By Alegomaster in forum C Programming
    Replies: 5
    Last Post: 03-17-2011, 07:26 PM
  4. Replies: 19
    Last Post: 01-12-2006, 11:04 AM
  5. XML library
    By Massive in forum C Programming
    Replies: 9
    Last Post: 07-04-2003, 07:14 AM