کد:
#include <iostream>
#include <conio>
#include <fstream>
#include <algorithm>
#include <stdio>
#include <stdlib>
# define esc 27
#define enter 13
#define up 72
#define down 80
#define tab 9
class library
{
public:
struct book
{
char name[20];
char writer[20];
char date[15];
char publisher[20];
char subject[20];
int id,price;
}book1,booktemp;
int menu();
char addmenu(int *,char *);
void add();
void searchmenu(int *,char *);
void search();
void show(int*,char*,char *);
void deletmenu(char *);
void delet();
}book;
//***********************main***************************************************
int main()
{
while(1)
{
window(1,1,80,25);
textbackground(0);
textcolor(15);
clrscr();
gotoxy(29,3);
cprintf("WELCOM TO MY LIBRARY");
gotoxy(55,23);
cprintf("*** SPRING OF 1387 **");
int select=book.menu();
switch(select)
{
case 1:book.add();break;
case 2:book.search();break;
case 3:book.delet();
case 4:
cout<<"\a";
window(10,5,70,20);
textbackground(1);
textcolor(15);
clrscr();
gotoxy(15,7);
cprintf("ARE YOU SURE (y/n)?");
char ex=getch();
if(ex==13||ex==121||ex==89)
return 0;
}
}
return 0;
}
//******************************functions***************************************
int library::menu()
{
char item[4][20]={
"1->Insert","2->Search","3->Delet","4->Exit"},key;
int i=0,j=0;
window(10,5,70,20);
textbackground(3);
clrscr();
window(1,1,25,80);
while(1)
{
for(i=0;i<4;i++)
{
textbackground(3);
textcolor(14);
if(i==j)
{
textbackground(1);
textcolor(15);
}
gotoxy(2,2+(2*i));
cprintf("%s",item[i]);
}
gotoxy(2,2+(2*j));
key=getch();
switch(key)
{
case 49:j=0;break;
case 50:j=1;break;
case 51:j=2;break;
case 52:j=3;break;
case up:if(j==0)j=3;else j--;break;
case down:if(j==3)j=0;else j++;break;
case tab:if(j==3)j=0;else j++;break;
case enter:break;
case esc:return 4;
}
if(key==13)break;
}
return j+1;
}
//*****************************************
char library::addmenu(int *j,char *key)
{
char add[7][20]={
"Name :","Writer :","Date publisher :","Price :","publisher :","Subject :","Code:"};
int i,flag=0;
window(21,5,50,20);
textbackground(1);
clrscr();
while(1)
{
for(i=0;i<7;i++)
{
textbackground(1);
textcolor(14);
if(i==*j)
{
textbackground(3);
textcolor(15);
}
gotoxy(2,2+(2*i));
cprintf("%s",add[i]);
}
gotoxy(2,2+(2*(*j)));
*key=getch();
switch(*key)
{
case up:if(*j==0)*j=6;else (*j)--;break;
case down:if(*j==6)*j=0;else (*j)++;break;
case tab:if(*j==6)*j=0;else (*j)++;break;
case enter:break;
case esc:break;
}
if(*key==13||*key==27)break;
}
}
//****************************************************
void library::add()
{
char add[7][20]={
"Name :","Writer :","Date publisher :","Price :","publisher :","Subject :","Code:"},key;
int j=0;
char ch=addmenu(&j,&key);
ofstream file1("C:\\library.dat",ios::app);
if(!file1) cout<<"\ncan not open file";
if(ch==13)
{
gotoxy(2,2+(2*j));textbackground(1);textcolor(14);cprintf("%s",add[j]);
gotoxy(2,2);textbackground(3);textcolor(15);cprintf("%s",add[0]);
gotoxy(8,2);cin.get(book1.name,20);cin.get();
gotoxy(10,4);cin.get(book1.writer,20);cin.get();
gotoxy(18,6);cin.get(book1.date,20);cin.get();
gotoxy(9,8);cin>>book1.price;cin.get();
gotoxy(13,10);cin.get(book1.publisher,20);cin.get();
gotoxy(11,12);cin.get(book1.subject,20);cin.get();
gotoxy(7,14);cin>>book1.id;cin.get();
file1.write((char*)&book1,sizeof(struct book));
file1.close();
}
}
//********************************************************
void library::searchmenu(int*j,char*key)
{
char add[3][20]={"Name :","subject :","Code:"};
int i,flag=0;
window(21,7,50,15);
textbackground(1);
clrscr();
while(1)
{
for(i=0;i<3;i++)
{
textbackground(1);
textcolor(14);
if(i==*j)
{
textbackground(3);
textcolor(15);
}
gotoxy(2,2+(2*i));
cprintf("%s",add[i]);
}
gotoxy(2,2+(2*(*j)));
*key=getch();
switch(*key)
{
case up:if(*j==0)*j=2;else (*j)--;break;
case down:if(*j==2)*j=0;else (*j)++;break;
case tab:if(*j==2)*j=0;else (*j)++;break;
case enter:break;
case esc:break;
}
if(*key==13||*key==27)break;
}
}
//*************************************************************
void library::search ()
{
char add[3][20]={"Name :","subject :","Code:"},ch,n[20];
int i,j,cod;
searchmenu(&j,&ch);
if(ch!=27)
{
switch(j)
{
case 0:gotoxy(8,2);cin.get(n,20);cin.get();i=1;break;
case 1:gotoxy(11,4);cin.get(n,20);cin.get();i=2;break;
case 2:gotoxy(7,6);cin.get(n,20);cin.get();i=3;break;
}
}
int x=atol(n);
show(&x,n,&ch);
}
//*************************************************************
void library::show(int *x,char*n,char *ch)
{
char item[7][20]={
"Name :",
"Writer :",
"Date publisher :",
"Price :",
"publisher :",
"Subject :",
"Code:"
};
if(*ch!=27)
{
ifstream file1("C:\\library.dat",ios::out);
if(!file1) cout<<"\ncan not open file";
int j=0;
if((*x)>0) while(file1.read((char*)&book1,sizeof(struct book)))
if(*x==book1.id)
{
j=1;
booktemp=book1;
break;
}
if(*x==0) while(file1.read((char*)&book1,sizeof(struct book)))
if(strcmp(book1.name,n)==0||strcmp(book1.subject,n)==0)
{
j=1;
booktemp=book1;
break;
}
file1.close();
window(10,5,70,20);
textbackground(6);
clrscr();
if(j==0)
{
textcolor(1);
gotoxy(6,7);
cprintf("!!! NOT FOUND THIS BOOK IN THE LIBRARY !!!");
gotoxy(15,10);
cprintf("PRESS ANY KEY TO COMTINUE :");
}
else
{
for(int i=0;i<7;i++)
{
textcolor(1);
gotoxy(2,(i+1)*2);
cprintf("%s",item[i]);
}
gotoxy(8,2);cprintf("%s",book1.name);
gotoxy(10,4);cprintf("%s",book1.writer);
gotoxy(18,6);cprintf("%s",book1.date);
gotoxy(9,8);cout<<book1.price;
gotoxy(13,10);cprintf("%s",book1.publisher);
gotoxy(11,12);cprintf("%s",book1.subject);
gotoxy(7,14);cout<<book1.id;
}
getch();
}
}
//**********************************************
void library::deletmenu(char *key)
{
window(20,9,35,11);
textbackground(1);
textcolor(14);
clrscr();
textcolor(15);
textbackground(3);
gotoxy(2,2);cprintf("Book code :");
gotoxy(2,2);
*key=getch();
}
//********************************
void library::delet()
{
char add[7][20]={
"Name :",
"Writer :",
"Date publisher :",
"Price :","publisher :",
"Subject :",
"Code:"};
char ch;
int code,i,f;
ofstream file2("C:\\temp.dat",ios::in);
if(!file2) cout<<"\ncan not open file";
ifstream file1("C:\\library.dat",ios::out);
if(!file1) cout<<"\ncan not open file";
deletmenu(&ch);
if(ch!=27)
{
gotoxy(13,2);
cin>>code;
}
while(file1.read((char*)&book1,sizeof(struct book)))
{
if(book1.id!=code)
file2.write((char*)&book1,sizeof(struct book));
if(book1.id==code)
{
window(10,5,70,22);
textbackground(1);
textcolor(15);
clrscr();
gotoxy(10,2);cprintf("PLEASE ENTER NEW INFORMATION OF BOOK");cin.get();
gotoxy(2,4);cprintf("%s",add[0]);cin.get(booktemp.name,20);cin.get();
gotoxy(2,6);cprintf("%s",add[1]);cin.get(booktemp.writer,20);cin.get();
gotoxy(2,8);cprintf("%s",add[2]);cin.get(booktemp.date,20);cin.get();
gotoxy(2,10);cprintf("%s",add[3]);cin>>booktemp.price;cin.get();
gotoxy(2,12);cprintf("%s",add[4]);cin.get(booktemp.publisher,20);cin.get();
gotoxy(2,14);cprintf("%s",add[5]);cin.get(booktemp.subject,20);cin.get();
gotoxy(2,16);cprintf("%s",add[6]);cin>>booktemp.id;cin.get();
file2.write((char*)&booktemp,sizeof(struct book));
}
}//end of while
file1.close();
file2.close();
remove("C:\\library.dat");
rename("C:\\temp.dat","C:\\library.dat");
}