Real Time implementation of stack and file for a bookshop – Coder in Me

File and Stack

you have heard about these words, they are the heartbeat for a DS and C programming. But If you have studied files and stacks, you often thought what is the need of it.Is there any real time application in which file or stack is implemented. So the answer is YES!

Here we will give a real time live example which is needed for a bookshop.

But before this example you should learn or revise these topics.

So, don’t bother I have a solution for you. i will provide you links where you can easily find study materials, concepts and codes related to files and stacks.

  1. for file handling link are
  2. Stack questions

Real Time implementation of  stack and file

Saif-EDU Care is one of the bookshops located in Nazra. The bookshop has all types of books once The administrator of the bookshop decided to convert the manual system into computerized.new sytem allow administrator [staff ] to store book details, information related to books such as borrow date, borrow time, return date, return time, due date, and calculation of fines if books will be returned after the due date. Customers are required to register using the SYStem with a registration fee amounting to two hundred fifty Indian rupee (250 INR). After the amount ispaid a customer will be provided with a unique customer ID and used it for tracking processes.

Aim

Based on the scenario given above, you are required to write the code and test it using C Language.The program should be able to store, display information (customer & book) in a text file, and allrelevant information of the book should be stored in stack order. When a customer returns aborrowed book, the administrator / staff will add at the top of the book shelves and assumes that theshelves are empty to allow books to be arranged in stack order.The library bookshop application program must have a main menu with three sub-menus as listed below:

1. Customer bookshop information –

2. Bookshop stack

3. Exit

The library bookshop system should ask the user to choose a sub-menu from the main menu. Each

Submenu contains:

  1. Customer bookshop information which contains the following information to display:

a. insert: insert information of a customer and book details: This function will add the customer information and book borrowed by them.

  •  Customer name
  • Customer ID .
  • Address
  • Contact number
  • – Book serial number
  • ‘ Book title –
  • Book author name
  • ‘ Date of borrow
  • – Date of publish
  • Date of return
  • Fine amount

b. Search information (if a specific customer in the text file and display it .—Searching the book using customer ID and display the books borrowed by them.

c.List : List all thecustpmer infermatipn in the text file

d.Exit: Te return back to the main menu.

So the solution for this program is here in C language.

Solution in C for file that stores books and customers details
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
 FILE *p= fopen("Book.txt","a+");
 char cname[55],cid[55],caddress[255],btitle[50],baname[55],dob[10],dop[10],dor[10];
 int bsno,fine;
 int i=156900;
 double cont;
 int c;
 printf("\nWelcome to book stack !\n Enter 1 if you want to insert the book detail\n enter 2 if you want to search\n enter 3 if you want list of books\nenter 4 to exit\n");
 scanf("%d",&c);
 
 while(c==1){
  
  printf("Enter Customer Name:\n");
  scanf("%s",cname);
  i=i++;
  printf("Enter Customer Address:\n");
  scanf("%s",caddress);
  printf("Enter Book Title:\n");
  scanf("%s",btitle);
  printf("Enter Book S No:\n");
  scanf("%d",&bsno);
  printf("Enter Author Name:\n");
  scanf("%s",baname);
  printf("Enter Date of Borrow:\n");
  scanf("%s",dob);
  printf("Enter Date of Publish:\n");
  scanf("%s",dop);
  printf("Enter Date of Return:\n");
  scanf("%s",dor); 
  printf("Enter Fine:\n");
  scanf("%d",&fine);
 
  fprintf(p, "%s\t%d\t%s\t%s\t%d\t%s\t%s\t%s\t%s\t%d\n",cname,i,caddress,btitle,bsno,baname,dob,dop,dor,fine);
 
  printf("\n Enter 0 if you want to insert the book detail\n enter 2 if you want to search\n enter 3 if you want list of books\nenter 4 to exit\n");
  scanf("%d",&c);
 }
 while(c==2){
  
  int cust,count=0;
  printf("Enter customer id:\n");
  scanf("%d",&cust);
  while(!feof(p)){
  fscanf(p,"%s\t%d",cname,&i);
  printf("%d",i);
   if(i==cust){
    fscanf(p, "%s\t%d\t%s\t%s\t%d\t%s\t%s\t%s\t%s\t%d\n",cname,&i,caddress,btitle,&bsno,baname,dob,dop,dor,&fine);
    printf("Search Result: Customer name is -%s, Customer Id- %d, Book Name-%s, Fine-%d\n",cname,i,btitle,fine);
    count++;
   } 
  }
  if(count==0)
   printf("No Result Found\n");
  printf("\n Enter 0 if you want to insert the book detail\n enter 2 if you want to search\n enter 3 if you want list of books\nenter 4 to exit\n");
  scanf("%d",&c);
 }
 while(c==3){
  
  while(!feof(p)){
   fscanf(p, "%s\t%d\t%s\t%s\t%d\t%s\t%s\t%s\t%s\t%d\n",cname,&i,caddress,btitle,&bsno,baname,dob,dop,dor,&fine);
   printf("Search Result: Customer name is -%s, Customer Id- %d, Book Name-%s, Fine-%d\n",cname,i,btitle,fine); 
  }
  printf("\n Enter 0 if you want to insert the book detail\n enter 2 if you want to search\n enter 3 if you want list of books\nenter 4 to exit\n");
  scanf("%d",&c);
 }
 if(c==4){
 printf("Thank you!");
 }
 fclose(p);
 return 0;
}
Real Time implementation of stack and file
Real Time implementation of stack and file2. Bookshop stack

a.Push:The customer returns back the book

The administrator add the book’s serial number to the top of the book

stack  contains  Book serial number should be an integer
b. Display: Display the total number of the books in the stack
c. Pop: The administrator removes one book from the top of the book stack
Display the book serial number of this book
Book serial number should be an integer
d. Exit: To return back to the main menu.
3. Exit: To end the program

You have to do one thing before executing that code please create a file”with name bookstore.dat” on that place where you will save your c code.

SOLUTION IN C FOR BOOK Stack For PUSH AND POP
// C program for array implementation of stack
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

struct Stack* stack_resize(struct Stack* stack);
struct Stack* createStack(unsigned capacity);
int isFull(struct Stack* stack);
int size(struct Stack* stack);
void push(struct Stack* stack, int item);
int pop(struct Stack* stack);
int isEmpty(struct Stack* stack);
struct Stack* init() ;
void saveChanges(struct Stack* stack);


// A structure to represent a stack
struct Stack
{
    int top;
    unsigned capacity;
    int* array;
};

// function to create a stack of given capacity. It initializes size of
// stack as 0
struct Stack* createStack(unsigned capacity)
{
    struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
    stack->capacity = capacity;
    stack->top = -1;
    stack->array = (int*) malloc(stack->capacity * sizeof(int));
    return stack;
}

struct Stack* stack_resize(struct Stack* stack) {
    int capacity = stack->capacity;
    stack = realloc(stack,capacity*2*sizeof *stack);
    if ( stack == NULL )
    {
        exit(EXIT_FAILURE);
    }
    return stack;
}

// Stack is full when top is equal to the last index
int isFull(struct Stack* stack)
{   return stack->top == stack->capacity - 1; }

int size(struct Stack* stack) {
return stack->top + 1;
}

// Stack is empty when top is equal to -1
int isEmpty(struct Stack* stack)
{   return stack->top == -1;  }

// Function to add an item to stack.  It increases top by 1
void push(struct Stack* stack, int item)
{
    if (isFull(stack))
        stack = stack_resize(stack);
    stack->array[++stack->top] = item;
}

// Function to remove an item from stack.  It decreases top by 1
int pop(struct Stack* stack)
{
    if (isEmpty(stack))
        return INT_MIN;
    return stack->array[stack->top--];
}
struct Stack* init() {
    FILE *fp;
    int count = 0;  // Line counter (result)
    char filename[] = "./bookstore.dat";
    char c;  // To store a character read from file
    fp = fopen(filename, "r");
    // Check if file exists
    if (fp == NULL)
    {
        printf("Could not open file %s", filename);
        return 0;
    }

    // Extract characters from file and store in character c
    for (c = getc(fp); c != EOF; c = getc(fp))
        if (c == '\n') // Increment count if this character is newline
            count = count + 1;

    struct Stack* stack = createStack(count+1);
    fseek(fp, 0, SEEK_SET);
    int t;
    while(fscanf(fp, "%d", &t) != EOF)
            push(stack,t);
    // Close the file
    fclose(fp);
    return stack;
}
void saveChanges(struct Stack* stack) {
    FILE *fp;
    int count = 0;  // Line counter (result)
    char filename[] = "./bookstore.dat";
    char c;  // To store a character read from file
    fp = fopen(filename, "w");
    // Check if file exists
    if (fp == NULL)
    {
        printf("Could not open file %s", filename);
        return 0;
    }
    int t;
    while(!isEmpty(stack)) {
        t = pop(stack);
        fprintf(fp, "%d\n", t);
    }
    fclose(fp);
}

// Driver program to test above functions
int main()
{
    struct Stack* stack = init();
    int c,b_no;
    printf("\nWelcome to book stack !\n");
while(1) {
int flag = 0,b_id;
printf("\nEnter 1 if you want to return the book\n");
printf("enter 2 if you want to know total number of books available\n");
printf("enter 3 if you want retreive/remove a book\n");
printf("enter 4 to exit\n");
printf("Enter your choice: ");
scanf("%d",&c);
switch(c) {
case 1 :
printf("Enter the book's serial Number: ");
scanf("%d",&b_no);
push(stack,b_no);
printf("%d book has been returned\n", b_no);
break;
case 2 :
printf("Total no of books: %d\n", size(stack));
break;
case 3 :
                    b_id = pop(stack);
                    if(b_id == INT_MIN) {
                        printf("There is no books to remove\n");
                    }
                    else {
                        printf("%d book has been removed\n", b_id);
                    }
break;
case 4 :
                    saveChanges(stack);
                    free(stack);
flag = 1;
}
if(flag) break;
}

    return 0;
}

Real Time implementation of stack and file

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.