Share on ... Twitter LinkedIn Facebook Pinterest Telegram Kitap Stok Programı Kitapların adını ve numarasını girerek kitap aranabilen ve kitap eklenebilen bir program (C++ Binary Search Tree yapıları kullanılıyor). Programı İndir VeriTabanı Dosyasını İndir Proje Dosyaları: FilenameFilesizeLast modified LibraryStockProgram.cpp14.6 KiB2019/05/08 21:25 LibraryStockProgram.exe147.6 KiB2019/05/08 21:25 LibraryStockProgram.txt5.5 KiB2019/05/08 21:25 backup.txt65.8 KiB2019/05/08 21:25 backup.zip8.9 KiB2019/05/08 21:25 librarystockprogram.jpg20.7 KiB2019/05/08 21:25 librarystockprogram2.jpg29.9 KiB2019/05/08 21:25 Kaynak Kodu: Kitapların adını ve numarasını girerek kitap aranabilen ve kitap eklenebilen bir program (C++ Binary Search Tree yapıları kullanılıyor).#include<iostream.h> #include<string.h> #include<cstring.h> #include<stdlib.h> #include <fstream.h> #include<conio.h> class Book//a book record is stored in a Book class { public: int callno; //call number of the book string bookname; //name of the book string author; //author of the book Book(int c_no,string bname,string auth); void PrintBook(void); void WriteFile(void); }; Book::Book(int c_no,string bname,string auth) //constructor of the Book class { callno=c_no; author=auth; bookname=bname; } void Book::PrintBook(void) //used to print the variables of the book calss on the screen { cout.width(7); cout.setf(ios::left); cout<<callno; //adjusted to be printed in 7 char space,and left side cout.width(42); cout<<bookname; //adjusted to be printed in 42 char space,and left side cout<<author<<endl; } void Book::WriteFile(void)//used to store a Book object in to the file { ofstream fout("backup.txt",ios::app); // open backup.txt for writing fout.width(6); fout.setf(ios::left); fout<<callno; fout.width(45); fout<<bookname; fout<<author; fout<< "\n"; fout.close(); // close backup.txt } template< class T> class TreeNode //Nodes of the Binary search tree { private: TreeNode<T> *left; TreeNode<T> *right; public: T data; TreeNode(const T& item,TreeNode<T> *lptr=NULL,TreeNode<T> *rptr=NULL); TreeNode<T>* Left(void) const; TreeNode<T>* Right(void) const; friend class BSTree<T>; }; template<class T> //constructor of TreeNode class TreeNode<T>::TreeNode(const T& item,TreeNode<T> *lptr=NULL,TreeNode<T> *rptr=NULL):data(item),left(lptr),right(rptr){} template<class T> TreeNode<T>* TreeNode<T>::Left(void) const //access method { return left; } template<class T> TreeNode<T>* TreeNode<T>::Right(void) const //access method { return right; } template<class T> TreeNode<T> *CopyTree(TreeNode<T> *t) { TreeNode<T> *newlptr,*newrptr,*newnode; if(t==NULL) return NULL; if(t->Left() != NULL) newlptr=CopyTree(t->Left()); else newlptr=NULL; if(t->Right() != NULL) newrptr=CopyTree(t->Right()); else newrptr=NULL; newnode= GetTreeNode(t->data,newlptr,nrptr); return newnode; } template<class T> void DeleteTree(TreeNode<T> *t) { if(t != NULL) { DeleteTree(t->Left()); DeleteTree(t->Right()); FreeTreeNode(t); } } template<class T> void ClearTree(TreeNode<T> * &t) { DeleteTree(t); t=NULL; } template<class T> class BSTree //The Binary Search Tree class { private: TreeNode<T> *root; //private variables TreeNode<T> *current; int authorfind; int size; //Node Methods TreeNode<T> *GetTreeNode(const T& item,TreeNode<T> *lptr,TreeNode<T> *rptr); void FreeTreeNode(TreeNode<T> *p); TreeNode<T> *CopyTree(TreeNode<T> *t); void DeleteTree(TreeNode<T> *t); TreeNode<T> *FindNode(const T& item,TreeNode<T>* & parent) const; void FindAuthorItem(TreeNode<T> *t,T& item); void Traverse(TreeNode<T> *t); public: BSTree(void){current=NULL;root=NULL;}; ~BSTree(void){}; BSTree<T>& operator= (const BSTree<T>& rhs); //BSTree Methods void Update(void); void Find(T& item); void FindAuthor(T& item); void Insert(const T& item); void Delete(const T& item); void DeleteBSTree(void); void TraverseBST(void); TreeNode<T> *GetRoot(void) const; }; template<class T> void BSTree<T>::DeleteTree(TreeNode<T> *t) //deletes a Binary Seach Tree by Freeing the Memoy { if(t != NULL) { DeleteTree(t->Left()); DeleteTree(t->Right()); FreeTreeNode(t); } } template<class T> void BSTree<T>::FindAuthorItem(TreeNode<T> *t,T& item) //a recursive function to find author's books { if (t !=NULL) //Post order traversal { FindAuthorItem(t->Left(),item); FindAuthorItem(t->Right(),item); if(item.author==t->data.author) { t->data.PrintBook(); authorfind=1; } } } template<class T> void BSTree<T>::FindAuthor(T& item) //Finds the Authors using function FindAuthorItem { authorfind=0; FindAuthorItem(root,item); if(authorfind==0) cout<<"The Entry could not be found!"; } template<class T> void BSTree<T>::Traverse(TreeNode<T> *t) //used to store BSTree Nodes to the file { if (t !=NULL) //it recursively calls itself { Traverse(t->Left()); Traverse(t->Right()); t->data.WriteFile(); } } template<class T> void BSTree<T>::TraverseBST(void) //used to store BSTree Nodes to the file { Traverse(root); //it calls the function Traverse } template<class T> TreeNode<T> *BSTree<T>::FindNode(const T& item,TreeNode<T>* &parent) const//finds a node in a BSTree { //recursively called TreeNode<T> *t =root; parent=NULL; while(t!= NULL) { if (item.callno==t->data.callno) break; else { parent=t; if(item.callno < t->data.callno) t=t->left; else t=t->right; } } return t; //returns the address of the node } template<class T> void BSTree<T>::Find(T& item) //finds a node in a Binary Search Tree { //calls the function FindNode TreeNode<T> *parent; current = FindNode(item,parent); if(current != NULL) current->data.PrintBook(); //if the record is found print it to the screen else cout<<"The Entry could not be found"<<endl; } template<class T> void BSTree<T>::Update(void) //used to update an entry { int tmpcallno,buffer,menunumber; //temp items char buffer2[40]; Book tmpitem(2,"",""); //temprorary book item for user inputs cout<<"Enter the call number of the Book->"; //take the callno cin>>tmpcallno; tmpitem.callno=tmpcallno; //update is done using callnumber TreeNode<T> *parent; cout<<endl; current = FindNode(tmpitem,parent);//if it exists print it if(current != NULL) //if it exist continue { current->data.PrintBook(); cout<<"Select an option from the menu:"<<endl<<endl; cout<<"To Update Call Number press 1"<<endl; cout<<"To Update Book Name press 2"<<endl; cout<<"To Update Author press 3"<<endl; cin>>menunumber; cout<<endl; switch(menunumber) { //update callnumber case 1: cout<<"Enter the new Call Number->"; cin>>buffer; //take user input current->data.callno=buffer; //change record break; //update Book Name case 2: cout<<"Enter the new name of the Book->"; cin.ignore(5,'\n'); cin.getline(buffer2,40);//take user input current->data.bookname=buffer2; //change record break; //Update Author case 3: cout<<"Enter the new Author->"; cin.ignore(5,'\n'); cin.getline(buffer2,40); //take user input current->data.author=buffer2; //change record break; default: break; } cout<<endl<<"The Entry is Updated"<<endl; current->data.PrintBook(); //Print the updated record } else cout<<"The Entry could not be found"; //if the item with the call number,inform the user } template<class T> TreeNode<T>* BSTree<T>::GetTreeNode(const T& item,TreeNode<T> *lptr,TreeNode<T> *rptr)//allocate memory for a node { TreeNode<T> *p; p=new TreeNode<T>(item,lptr,rptr); if (p==NULL) cout<<"allocation error"; return p; } template<class T> void BSTree<T>::FreeTreeNode(TreeNode<T> *p) //delete allocated memory { delete p; } template<class T> void BSTree<T>::Insert(const T& item) //Used to insert a new item to the Binary Search Tree { TreeNode<T> *t =root,*parent = NULL,*newNode; while(t!=NULL) { parent =t; if(item.callno < t->data.callno) //Insertion is done according to the call number t=t->left; else t=t->right; } newNode = GetTreeNode(item,NULL,NULL); if( parent == NULL) root = newNode; else if((item.callno)< (parent->data.callno)) parent->left=newNode; else parent->right=newNode; current=newNode; size++; } template<class T> void BSTree<T>::Delete(const T& item) //used to delete a node from the BSTree { TreeNode<T> *DNodePtr,*PNodePtr,*RNodePtr; if((DNodePtr=FindNode(item,PNodePtr))==NULL) return; if (DNodePtr->right==NULL) RNodePtr=DNodePtr->left; else if(DNodePtr->left==NULL) RNodePtr =DNodePtr->right; else { TreeNode<T> *PofRNodePtr =DNodePtr; RNodePtr =DNodePtr->left; while (RNodePtr->right != NULL) { PofRNodePtr =RNodePtr; RNodePtr=RNodePtr->right; } if( PofRNodePtr==DNodePtr) RNodePtr->right=RNodePtr->left; else { PofRNodePtr->right=RNodePtr->left; RNodePtr->left = DNodePtr->left; RNodePtr->right = DNodePtr->right; } } if (PNodePtr==NULL) root =RNodePtr; else if (DNodePtr->data.callno<PNodePtr->data.callno) PNodePtr->left=RNodePtr; else PNodePtr->right =RNodePtr; FreeTreeNode(DNodePtr); size--; } template<class T> void BSTree<T>::DeleteBSTree(void)//Used to delete all Binary Search Tree Nodes(Used for freeing memory) { DeleteTree(root); } int main(void) { BSTree<Book> Library;//****The main binary search tree that is used for holding book entries**** char callnoChar[6]="",BnameBuff[40],AuthoBuff[10]; // for user input int callnobuf=1; char temp1,temp2; int end; ifstream fin("backup.txt"); // open file for reading Book buffer1(0,"",""); char m=' '; while(fin.good()&& m!='\n')//a dummy reading to skip first line { m=fin.get(); } char templine[80];//a temprorary variable will be used to store current line. while(!fin.eof()&& callnobuf) //take the file contents upto End of File { fin.getline(templine,80); //get the whole line for(int i=0;i<6;i++) //first 6 character is the call number callnoChar[i]=templine[i]; //take first 6 char to a buffer string callnoChar[6]=NULL; callnobuf=atoi(callnoChar); //convert string to integer if(callnobuf==0) break; for(int j=0;j<45;j++) //for a line characters between 6-51 are Book Name { BnameBuff[j]=templine[j+6]; //take this portion to another buffer string temp1=templine[j+6]; temp2=templine[j+7]; if(temp1==temp2&&temp2==' ') //if two space is found break the loop break; } BnameBuff[j]=NULL; //make the last character of the string Null for(int t=51;t<100;t++) //take the line characters after 51 { if(templine[t]==NULL) { end=t+1; //end shows the end of line break; //break if end of line is found } } for(int k=51;k<end;k++) //take the line characters from 51 to the "end" AuthoBuff[k-51]=templine[k]; buffer1.callno=callnobuf; //send these buffer number and names to a buffer Book(buffer1) buffer1.bookname=BnameBuff; buffer1.author=AuthoBuff; if(callnobuf!=0) Library.Insert(buffer1); //Insert The Buffer Book(buffer1) to the Library BSTree }//while fin.close();//close the file that is read int exit=0; while(!exit){ char menunoChar[5]; int menuno; clrscr(); cout<<" .: MyLibrary 1.0 :. "<<endl; //Main menu cout<<"-------------------------------------------"<<endl; cout<<"|Menu 1| Add a new Entry |"<<endl; cout<<"--------"<<endl; cout<<"|Menu 2| Search a Book with Call Number |"<<endl; cout<<"--------"<<endl; cout<<"|Menu 3| Search Books for an Author |"<<endl; cout<<"--------"<<endl; cout<<"|Menu 4| Modify an Entry |"<<endl; cout<<"--------"<<endl; cout<<"|Menu 5| Delete an Entry |"<<endl; cout<<"--------"<<endl; cout<<"|Menu 6| Exit the Program |"<<endl; cout<<"-------------------------------------------"<<endl; cout<<"Enter a Menu Number(Between 1 and 6)"; //waits from the user to enter a number then press enter cin>>menunoChar;//the entered value is taken to a string //then converted to integer menuno=atoi(menunoChar);//used to determine wrong menu entry(avoids forever loop) //temp variables for the user inputs char auname[25]; char name1[45] ,autname1[25]; int callno1,tmpcallno; int callsrch; Book temp(0,"","");//temp will be used to change,find records. char asksave;//te switch(menuno) { //1st Menu item: Add a new entry case 1: clrscr(); cout<<"Enter the call no-->"; cin>>callno1; cout<<endl<<"Enter the Book Name->"; cin.ignore(1,'\n'); cin.getline(name1,45); cout<<endl<<"Enter the Author Name->"; cin.getline(autname1,45); temp.callno=callno1; temp.bookname=name1; temp.author=autname1; Library.Insert(temp); cout<<"The Entry is successfully added"<<endl; temp.PrintBook(); cout<<endl<<"Press any key to continue"; getch(); break; //2nd Menu item: Search with callnumber case 2: clrscr(); cout<<"Enter a Call Number to search->"; cin>>callsrch; //takes search number to callsrch temp.callno=callsrch;//make the temp Books callno,callsrch cout<<endl; Library.Find(temp); //search for callnumber using temp's callno cout<<endl<<"Press any key to continue"; getch(); break; //3rd Menu item: Search with Author name case 3: clrscr(); cout<<"Enter an Author Name to search-->"; cin.ignore(1,'\n'); cin.getline(auname,25); //takes search item to auname temp.author=auname; //make the temp Books author,auname Library.FindAuthor(temp); //searches for the Author for the temp's author cout<<endl<<"Press any key to continue"; getch(); break; //4th Menu item: Update an Entry case 4: clrscr(); Library.Update(); //Update with BSTree method Update cout<<endl<<"Press any key to continue"; getch(); break; //5th Menu item:Delete an Entry case 5: clrscr(); cout<<"Enter the call number of the Book "; cin>>tmpcallno; temp.callno=tmpcallno;//The user input is assigned to the temproray items callno. Library.Delete(temp);//The item with the temp's call number is deleted. cout<<"Entry is deleted"; cout<<endl<<"Press any key to continue"; getch(); break; //6th Menu item:Exit the Program case 6: cout<<"Do you want to save changes?(y/n)"; cin>>asksave; if(asksave=='y') { ofstream fout1("backup.txt"); //if user enters y,the file is opened for writing fout1<<"ID No:Name Author-\n"; fout1.close(); Library.TraverseBST(); } exit=1; //exit=1 to break the while loop break; default: clrscr(); cout<<"!!!The menu number is not valid!!!"<<endl; //if the entered menu number is not-1,2,3,4,5 or 6 inform user cout<<endl<<"Press any key to continue"; getch(); break; }//switch }//while Library.DeleteBSTree();//Free Memory before exiting; return 0; } projelerim/programlama/cplusplus/library_stock_program.txt Son değiştirilme: 2019/05/08 21:25(Dışarıdan düzenle) Giriş yap