Demo entry 6257003

SciLab

   

Submitted by anonymous on Oct 15, 2016 at 06:33
Language: Matlab. Code size: 6.1 kB.

clc  
function [due] = dues(et)  
  if(et <= 604800) //1 week in seconds  
    due = 0  
  elseif((et > 604800)& (et <= 1209600)) //Less than 2 weeks  
    due  = 15  
  else  
    et = et  - 1209600 //More than 2 weeks  
    et = et/86400  //Charge for every day extra  
    due = 25 + et  
  end  
endfunction  
  
function [] = search_for_book()  
  search_type = 1  
  f = mopen("books.txt","r")  
  search_type = input("[1] To search by book name[2] To search by author name[3] To search by ISBN number")  
  if(search_type == 1) then  
    name_search = input("Enter the name of the book","string")  
    while (~meof(f))  
      [n,book_name,author_name,ISBN,no_of_titles,no_of_titles_borrowed] = mfscanf(f,"%s %s %d %d %d\n")  
      if(book_name == name_search) then  
        mprintf("%s %s %d %d %d\n",book_name,author_name,ISBN,no_of_titles,no_of_titles_borrowed)  
      end  
    end  
  elseif(search_type == 2) then  
    author_name_search = input("Enter the name of the book","string")  
    while(~meof(f))  
      [n,book_name,author_name,ISBN,no_of_titles,no_of_titles_borrowed] = mfscanf(f,"%s %s %d %d %d\n")  
      if(author_name == author_name_search) then  
        mprintf("%s %s %d %d %d\n",book_name,author_name,ISBN,no_of_titles,no_of_titles_borrowed)  
      end  
    end  
  elseif(search_type == 3) then  
    ISBN_search = input("Enter the number of the book")  
    while(~meof(f))  
      [n,book_name,author_name,ISBN,no_of_titles,no_of_titles_borrowed] = mfscanf(f,"%s %s %d %d %d\n")  
      if(ISBN_search==ISBN) then  
        mprintf("%s %s %d %d %d\n",book_name,author_name,ISBN,no_of_titles,no_of_titles_borrowed)  
      end  
    end  
  else  
    disp("Wrong input")  
  end  
  mclose(f)  
endfunction  
  
function [] = lend_book(member_no,book_no)  
  f1 = mopen("books.txt","r")  
  f2 = mopen("members.txt","r")  
  f1_t = mopen("tmp_book.txt","w")  
  f2_t = mopen("tmp_members.txt","w")  
  mseek(0,f1,'set')  
  mseek(0,f2,'set')  
  while(~(meof(f1)))  
    [n,book_name,author_name,ISBN,no_of_titles,no_of_titles_borrowed] = mfscanf(f1,"%s %s %d %d %d\n")  
    if(book_no == ISBN)  
      if(no_of_titles_borrowed = no_of_titles)  
        disp("All books are reserved right now try again later")  
      else  
        no_of_titles_borrowed = no_of_titles_borrowed + 1  
      end  
    end  
    mfprintf(f1_t,"%s %s %d %d %d\n",book_name,author_name,ISBN,no_of_titles,no_of_titles_borrowed)  
  end  
  while(~(meof(f2)))  
    [n,member_name,member_id,book_isbn,issue_date,return_date,due] = mfscanf(f2,"%s %d %d %d %d %d\n")  
    if(member_no == member_id)  
      book_isbn = book_no  
      issue_date = getdate("s")  
    end  
      mfprintf(f2_t,"%s %d %d %d %d %d\n",member_name,member_id,book_isbn,issue_date,return_date,due)  
  end  
  movefile("tmp_book.txt","books.txt")  
  movefile("tmp_members.txt","members.txt")  
  mclose(f1)  
  mclose(f2)  
  mclose(f1_t)  
  mclose(f2_t)  
endfunction  
  
function [] = return_book(member_no)  
  f3 = mopen("books.txt","r+")  
  f4 = mopen("members.txt","r+")  
  f3_t = mopen("tmp_book.txt","w")  
  f4_t = mopen("tmp_members.txt","w")  
  /*mseek(0,f3,'set')*/  
  mseek(0,f4,'set')  
  book_no = 0  
  while(~(meof(f4)))  
    [n,member_name,member_id,book_isbn,issue_date,return_date,due] = mfscanf(f4,"%s %d %d %d %d %d\n")  
    if(member_no == member_id)  
      book_no = book_isbn  
      book_isbn = 0  
      return_date = getdate("s")  
      et = return_date - issue_date  
      due = due + dues(et)  
      issue_date = 0  
      return_date = 0  
      mprintf("%s %d %s","Your due amount is",due,"Rupees")  
    end  
      mfprintf(f4_t,"%s %d %d %d %d %d\n",member_name,member_id,book_isbn,issue_date,return_date,due)  
  end  
  while(~(meof(f3)))  
    [n,book_name,author_name,ISBN,no_of_titles,no_of_titles_borrowed] = mfscanf(f3,"%s %s %d %d %d\n")  
    if(book_no == ISBN)  
      no_of_titles_borrowed = no_of_titles_borrowed - 1  
    end  
    mfprintf(f3_t,"%s %s %d %d %d\n",book_name,author_name,ISBN,no_of_titles,no_of_titles_borrowed)  
  end  
  movefile("tmp_book.txt","books.txt")  
  movefile("tmp_members.txt","members.txt")  
  mclose(f3)  
  mclose(f4)  
  mclose(f3_t)  
  mclose(f4_t)  
endfunction  
  
  
  
  
// Main function  
books = mopen("books.txt",'a+')  
member = mopen("members.txt","a+")  
flag = 1  
flag1 = 1  
choice = 1  
while(flag1)  
  disp("[1] Search for book")  
  disp("[2] Check for member dues")  
  disp("[3] Lend new book")  
  disp("[4] Return book")  
  disp("[5] Exit")  
  choice  = input("Enter your choice?")  
  if(choice == 1) then  
    search_for_book()  
    c = input("Do you want to conduct more operations ?[Y/N] ","string")  
    if(c ~= 'Y') then  
      flag1  = 0  
    end          //Search : have to implement regex based fuzzy  
  elseif(choice == 2) then  
    k = 0  
    member_no = input("Enter the member id ")  
    fl = mopen("members.txt","r")  
    while(~meof(fl))  
      mfscanf(member,"%s %d %d %d %d %d\n",member_name,member_id,book_isbn,issue_date,return_date,due)  
      if(member_no == member_id) then  
        k = due  
        mprintf("%s %d %s","Your due amount is",k,"Rupees")  
      end  
    end  
    c = input("Do you want to conduct more operations ?[Y/N] ","string")  
    if(c ~= 'Y') then  
      flag1  = 0  
    end  
  elseif(choice  == 3) then  
    member_no = input("Enter member id" )  
    book_no = input("Enter book ISBN number" )  
    lend_book(member_no,book_no)  
    c = input("Do you want to conduct more operations ?[Y/N]" ,"string")  
    if(c ~= 'Y') then  
      flag1  = 0  
    end  
  elseif(choice == 4) then  
    member_no = input("Enter member number" )  
    return_book(member_no)  
    c = input("Do you want to conduct more operations ?[Y/N]" ,"string")  
    if(c ~= 'Y') then  
      flag1  = 0  
    end  
  else  
    exit()  
  end  
end  

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).