Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Курсовик
.cpp//Inclusion of library functions of C++
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
//definition of structure link used to form my book list
typedef struct link
{
char books[10];
char firstname[13];
char surname[13];
char date_pub[9];
int booknum;
int hall;
int num_books;
struct link *backward;
struct link *forward;
}DATA ; //link defined to DATA
//definition of the global variables used.
DATA *info,*first,/*fur,*rec,*/*crec;
int i,j,k,n,num,srch;
char halls[4][11]={"Science","Literature","History","Law"};
DATA *copyelsearch(DATA *scol);//Copy individual fields(subfunction of search_for)
void main(void)
{
first=NULL;
//description of the functions used
void markd_border(int x1,int y1,int x2,int y2);//marks border for output of data
void putd_text(int x,int y,char *word);//used to put strings to a particular position
void invertd_bar(int x1,int y1,int x2);//makes menu bar
void intowin (int x,int y,int d,int w,char *title,char *str,int len);//collects strings to window
int menu(int x1,int y1,char **title,char **item,int num,int titl,int def);//makes menu
int confirm();//menu to confirm deletion of existing list
int quit();//menu to confirm exit
int testnumber(int num,int lowl, int upl);//input number of lib. no. & hall no. & hall no. &rejects letters and numbers <=0
DATA *inp1(); //input of data to book's list
void outmain(DATA *rec);//outputs book's list ,main interface with user
int outsm(DATA *rec);//outputs data book's list and helps to simplify processing
void proc1(DATA *rec);// sorts data book's list with regards to identity number
void proc2(DATA *rec);//sorts data book's list with regards to book's title
void proc3(DATA *rec);//sorts data book's list with regards to names
void proc4(DATA *rec);//sorts data book's list with regards to hallnumber
void proc5(DATA *rec);//sorts data book's list with regards to number of books
void sort(DATA *rec); //main function for sorting of data book's list
DATA *inpadd(DATA *col);//create or adds new elements to data book's list
DATA *del(DATA *ncol);//deletes a specified element from data book's list
DATA *update(DATA *ncol);//edits list by updating data book's list
DATA *search_for(DATA *scol);//searches for element/s in data book's list
void savenew(DATA*tosav);//saves data book's list into a named file
DATA *loadfile();//opens file to read saved data book's list
char ch,mh,**pnts,**pnt;
int mn;
clrscr();
_setcursortype(_NOCURSOR);
textbackground(BLACK);
window(10,5,70,20); //welcome window
textbackground(RED);
textcolor(YELLOW);
gotoxy(1,1);
cprintf("ЙНННННННННННННННННННННННННННННННННННННННННННННННННННННННН»"); gotoxy(1,2);
cprintf("¶ St.Petersburg state Electrotechnical University ¶"); gotoxy(1,3);
cprintf("¶ Department of Computer Technology ¶"); gotoxy(1,4);
cprintf("¶ COURSEWORK ¶"); gotoxy(1,5);
cprintf("¶ ¶"); gotoxy(1,6);
cprintf("¶ written by: ¶"); gotoxy(1,7);
cprintf("¶ 12 ¶"); gotoxy(1,8);
cprintf("З Gr. ¶"); gotoxy(1,9);
cprintf("ИННННННННННННННННННННННННННННННННННННННННННННННННННННННННј"); gotoxy(1,10);
cprintf(" ..... PLEASE PRESS ANY KEY ..... ");
fflush(stdin);
getch();
clrscr();
window(1,1,80,25);
textbackground(BLACK);
clrscr();
pnts=(char **)malloc(sizeof(char*)*9);
for(i=0;i<9;i++) //creates memory space for menu parameters
pnts[i]=(char *)malloc(sizeof(char)*30);
pnts[0]="CREATE LIST";
pnts[1]="SORT";
pnts[2]="ADD NEW ELEMENTS";
pnts[3]="DELETE A SPECIFIC ELEMENT";
pnts[4]="SAVE IN FILE";
pnts[5]="OPEN FILE";
pnts[6]="UPDATE DATA";
pnts[7]="SEARCH";
pnts[8]="EXIT";
pnt=(char **)malloc(sizeof(char*)*2);
for(i=0;i<2;i++)
pnt[i]=(char *)malloc(sizeof(char)*25);
pnt[0]="MAIN MENU";
pnt[1]="PLEASE MAKE A CHOICE";
int l=2; //MAIN MENU
do
{
mn=menu(20,5,pnt,pnts,9,2,1);
switch(mn)
{
case 1:
{
clrscr();
int dl=1,p=0;
if(first)
dl=confirm();
fflush(stdin);
if(dl==1)
{
printf("\n Give Number of books to be considered...");
printf("\n Must be a Number greater than 0 ");
printf("\n NO:= ");
fflush(stdin);
if((scanf("%d",&n))&&n>0)
p=0;
else
if((n<=0)||(!scanf("%d",&n)))
{
do
{
printf("\n ERROR OF INPUT !!!!! \n PLEASE TRY AGAIN");
printf("\n\n Must be a NUMBER greater than 0 ");
printf("\n NO:= ");
fflush(stdin);
if((scanf("%d",&n))&&n>0)
p=0;
else
if((n<=0)||(!scanf("%d",&n)))
p=1;
} while(p==1);
}
fflush(stdin);
crec=inp1();
outmain(crec);
fflush(stdin);
getch();
fflush(stdin);
}
break;
}
case 2:
{
clrscr();
sort(crec);
outmain(crec);
getch();
break;
}
case 3:
{
clrscr();
crec=inpadd(crec);
outmain(crec);
getch();
break;
}
case 4:
{
clrscr();
crec=del(crec);
if(crec==NULL)
{
printf("\nYou have just deleted the final element in your list, but you");
printf("\n can still create a new list or open one from existing files");
getch();
clrscr();
}
else
{
outmain(crec);
}
getch();
break;
}
case 5:
{
clrscr();
savenew(crec);
getch();
break;
}
case 6:
{
clrscr();
crec=loadfile();
outmain(crec);
getch();
break;
}
case 7:
{
clrscr();
crec=update(crec);
outmain(crec);
getch();
break;
}
case 8:
{
clrscr();
DATA *first11,*crec11;
first11=first;
crec11=crec;
crec=search_for(crec);
if(crec)
{
outmain(crec);
printf("\nTHIS IS YOUR SEARCH \n");
printf("\nPRESS ANY KEY TO CONTINUE \n");
getch();
clrscr();
}
first=first11;
crec=crec11;
outmain(crec);
getch();
break;
}
case 9:
{
l=quit();
fflush(stdin);
break;
}
case 0:
{
l=quit();
fflush(stdin);
break;
}
}
}while(l==2);
free(first);
free(pnts);
free(pnt);
}
//function which verifies integer value of lib.no. & hall no. & book no.
int testnumber(int num,int lowl,int upl)
{
int p=0;
if(( scanf("%d",&num))&&num>lowl&&num<upl)
p=0;
else
if(num<=lowl||num>=upl||(!scanf("%d",&num)))//rejects non integers 0 and -ve numbers
{
do
{
printf("\n ERROR OF INPUT !!!!! PLEASE TRY AGAIN");
printf("\n Number must be btw %d and %d n:= ",lowl,upl);
fflush(stdin);
if(( scanf("%d",&num))&&num>lowl&&num<upl)
p=0;
else
if(num<=lowl||num>=upl||(!scanf("%d",&num)))
p=1;
} while(p==1);
}
return(num);
}
//function which creates operating list for books
DATA *inp1()
{
first=fur=info=NULL; //removes previous members of list
for(i=0;i<n;i++)
{
if(first==NULL)//creates head element of list
{
info=(DATA*)malloc(sizeof(DATA));
printf("\n Give title of no %d Book: ",i+1);
scanf("%s",info->books);
fflush(stdin);
printf("\n Give name of author: ");
scanf("%s",info->firstname);
fflush(stdin);
printf("\n Give surname of author: ");
scanf("%s",info->surname);
fflush(stdin);
printf("\n Date when book was published: ");
scanf("%s",info->date_pub);
fflush(stdin);
printf("\n Give book's identity number: ");
info->booknum=testnumber(num,0,999);
fflush(stdin);
printf("\n 1-Science , 2-Literature , 3-History , 4-Law");
printf("\n Give hall number to which the book belongs to: ");
info->hall=testnumber(num,0,5)-1;
fflush(stdin);
printf("\n Give number of copies of particular book: ");
info->num_books=testnumber(num,0,9999);
fflush(stdin);
info->backward=NULL;
info->forward=NULL;
first=info;
}
else
{
fur=info;//creates subsequent elements
info=(DATA*)malloc(sizeof(DATA));
printf("\n Give title of no %d Book: ",i+1);
scanf("%s",info->books);
fflush(stdin);
printf("\n Give name of author: ");
scanf("%s",info->firstname);
fflush(stdin);
printf("\n Give surname of author: ");
scanf("%s",info->surname);
fflush(stdin);
printf("\n Date when book was published: ");
scanf("%s",info->date_pub);
fflush(stdin);
printf("\n Give book's identity number: ");
info->booknum=testnumber(num,0,999);
fflush(stdin);
printf("\n 1-Science , 2-Literature , 3-History , 4-Law");
printf("\n Give hall number to which the book belongs to: ");
info->hall=testnumber(num,0,5)-1;
fflush(stdin);
printf("\n Give number of copies of particular book: ");
info->num_books=testnumber(num,0,9999);
fflush(stdin);
info->backward=fur;
info->forward=NULL;
fur->forward=info;
}
}
info=first;
return(info);
}
//function which draws border for the main output
void markd_border(int x1,int y1,int x2, int y2)
{ //by the given coordinates
gotoxy(x1,y1);
putch(218);
for(i=x1;i<x2-1;i++) putch('Д');
putch(191);
gotoxy(x1,y2);
putch(192);
for(i=x1;i<x2-1;i++) putch('Д');
putch(217);
for(i=y1+1;i<y2;i++)
{gotoxy(x1,i);putch(179);gotoxy(x2,i);putch(179);};
};
//function for main output of programme for books
void outmain(DATA *rec)
{
clrscr();
_setcursortype(_NOCURSOR);
window(1,1,80,4);
textbackground(RED);
gotoxy(15,2);
cprintf(" ESC ANY OTHER KEY ");
gotoxy(15,3);
cprintf(" TO EXIT TO RETURN TO MAIN MENU");
n=0;
if(!rec)
n=10;
else
{
do
{
n++;
rec=rec->forward;
} while(rec!=NULL);
rec=first;
n=n+9;
}
window(1,5,80,n);
textbackground(RED);
window(1,5,80,25);
markd_border(1,1,80,n-5);
gotoxy(13,1);putch(194);
gotoxy(26,1);putch(194);gotoxy(38,1);putch(194);gotoxy(49,1);putch(194);
gotoxy(57,1);putch(194);gotoxy(70,1);putch(194);
gotoxy(1,2);
cprintf("іBOOK іNAME іSURNAME іDATE іBOOK # іHALL # іNo. books");
gotoxy(1,3);
for(i=1;i<80;i++)putch(196);
gotoxy(1,3);putch(195);gotoxy(80,3);putch(180);
gotoxy(13,3);putch(197);gotoxy(26,3);putch(197);gotoxy(38,3);putch(197);
gotoxy(49,3);putch(197);gotoxy(57,3);putch(197);gotoxy(70,3);putch(197);
window(1,8,80,n);
if(rec)
{
gotoxy(1,1);
i=1;
do
{
gotoxy(1,i);
cprintf("і%-11s",rec->books);
cprintf("і%-12s",rec->firstname);
cprintf("і%-11s",rec->surname);
cprintf("і%-10s",rec->date_pub);
cprintf("і%-7d",rec->booknum);
cprintf("і%-12s",halls[rec->hall]);
cprintf("і%-9dі",rec->num_books);
rec=rec->forward;
i++;
} while(rec!=NULL);
rec=first;
}
else
{
gotoxy(15,1);
textcolor(YELLOW);
cprintf("EMPTY LIST PLEASE INPUT DATA FIRST!!!");
}
}
//frequently used function for output of selected strings
void putd_text(int x,int y,char *word)
{
gotoxy(x,y);
cputs(word);
}
//function for input of bar used in menu
void invertd_bar(int x1,int y1,int x2)
{
unsigned char i;
unsigned char *buffer;
buffer=(char *)malloc((x2-x1)*2+1);
gettext(x1,y1,x2,y1,buffer);
for(i=1;i<2*(x2-x1)+1;i+=2)
buffer[i]^=119;//changes color using logical operation
puttext(x1,y1,x2,y1,buffer);
free(buffer);
}
int menu(int x1,int y1,char **title,char **item,int num,int titl,int def)
{
int i,ch,j,y2,x2=0;
char *buf;
_setcursortype(_NOCURSOR);
window(1,1,80,25);
y2=num+(titl-1)+y1+3;//menu width
for(i=0;i<=num-1;i++)
if(strlen(item[i])>x2)
x2=strlen(item[i]);
for(i=0;i<=titl-1;i++)
if(strlen(title[i])>x2)
x2=strlen(title[i]);
x2=x2+x1+5;//menu length
buf=(char*)malloc((x2-x1+1)*(y2-y1+1)*2+1);
gettext(x1,y1,x2,y2,buf);
textbackground(BLUE);
for(i=y1;i<y2;i++) //draws menu border
for(j=x1;j<x2;j++)
putd_text(j,i," ");
textcolor(15);
putd_text(x1,y1,"Й ");
for(i=x1+1;i<x2;i++)
{
putd_text(i,y1,"Н");
putd_text(i,y2,"Н");
putd_text(i,y1+(titl-1)+2,"-");
}
for(i=y1+1;i<y2;i++)
{
putd_text(x1,i,"З");
putd_text(x2,i,"З");
}
putd_text(x2,y1+(titl-1)+2,"¶");
putd_text(x1,y1+(titl-1)+2,"З");
putd_text(x1,y2,"И");
putd_text(x2,y1,"»");
putd_text(x2,y2,"ј");
textcolor(7);
for(i=0;i<titl;i++)
putd_text(x1+3,y1+1+i,title[i]);
for(i=0;i<num;i++)
putd_text(x1+3,y1+(titl-1)+3+i,item[i]);
j=def;
invertd_bar(x1+3,(y1+(titl-1)+3)+j-1,x2-2);//places bar on first point
while(ch!=13)
{
ch=getch();
switch(ch) //moves bar by keyboard arrow
{
case 72:invertd_bar(x1+3,(y1+(titl-1)+3)+j-1,x2-2);
if(j==1){j=num;}
else j--;//up
invertd_bar(x1+3,(y1+(titl-1)+3)+j-1,x2-2);
break;
case 80: invertd_bar(x1+3,(y1+(titl-1)+3)+j-1,x2-2);
if(j==num){j=1;}
else j++; //down
invertd_bar(x1+3,(y1+(titl-1)+3)+j-1,x2-2);
break;
case 27: j=0;ch=13;
break;
}
}
puttext(x1,y1,x2,y2,buf);
free(buf);
textbackground(BLACK);
textcolor(WHITE);
_setcursortype(_NORMALCURSOR);
return j; //returns position of bar when enter was pressed
}
//function which outputs data for books into functions that process the data
int outsm(DATA *rec)
{
j=1;
if(!rec)
printf("\n NO ELEMENT IN YOUR LIST PLEASE .....\n");
else
do
{
printf("\n No %d book:",j);
printf(" %-12s",rec->books);
printf(" %-12s",rec->firstname);
printf(" %-12s",rec->surname);
printf(" %10s",rec->date_pub);
printf(" %-4d",rec->booknum);
printf(" %11s",halls[rec->hall]);
printf(" %-5d",rec->num_books);
j++;
rec=rec->forward;
} while(rec!=NULL);
rec=first;
return (j);
}
//function which sorts data in accordance to their increasing identity number
void proc1(DATA *rec)
{
DATA *rec1,*col;
col=(DATA*)malloc(sizeof(DATA));
while(rec!=NULL)
{
rec1=rec->forward;
while(rec1!=NULL)
{
if(rec->booknum > rec1->booknum)
{
strcpy(col->books,rec1->books);
strcpy(col->firstname,rec1->firstname);
strcpy(col->surname,rec1->surname);
strcpy(col->date_pub,rec1->date_pub);
col->booknum=rec1->booknum;
col->hall=rec1->hall;
col->num_books=rec1->num_books;
strcpy(rec1->books,rec->books);
strcpy(rec1->firstname,rec->firstname);
strcpy(rec1->surname,rec->surname);
strcpy(rec1->date_pub,rec->date_pub);
rec1->booknum=rec->booknum;
rec1->hall=rec->hall;
rec1->num_books=rec->num_books;
strcpy(rec->books,col->books);
strcpy(rec->firstname,col->firstname);
strcpy(rec->surname,col->surname);
strcpy(rec->date_pub,col->date_pub);
rec->booknum=col->booknum;
rec->hall=col->hall;
rec->num_books=col->num_books;
}
rec1=rec1->forward;
}
rec=rec->forward;
}
rec=first;
}
//function which sorts data in alphabetical order of the BOOK'S TITLE
void proc2(DATA *rec)
{
DATA *rec1,*col;
col=(DATA*)malloc(sizeof(DATA));
while(rec!=NULL)
{
rec1=rec->forward;
while(rec1!=NULL)
{
if(strcmp(rec->books,rec1->books)>0)
{
strcpy(col->books,rec1->books);
strcpy(col->firstname,rec1->firstname);
strcpy(col->surname,rec1->surname);
strcpy(col->date_pub,rec1->date_pub);
col->booknum=rec1->booknum;
col->hall=rec1->hall;
col->num_books=rec1->num_books;
strcpy(rec1->books,rec->books);
strcpy(rec1->firstname,rec->firstname);
strcpy(rec1->surname,rec->surname);
strcpy(rec1->date_pub,rec->date_pub);
rec1->booknum=rec->booknum;
rec1->hall=rec->hall;
rec1->num_books=rec->num_books;
strcpy(rec->books,col->books);
strcpy(rec->firstname,col->firstname);
strcpy(rec->surname,col->surname);
strcpy(rec->date_pub,col->date_pub);
rec->booknum=col->booknum;
rec->hall=col->hall;
rec->num_books=col->num_books;
}
rec1=rec1->forward;
}
rec=rec->forward;
}
rec=first;
}
//function which sorts data in alphabetical order of the author's names
void proc3(DATA *rec)
{
DATA *rec1,*col;
col=(DATA*)malloc(sizeof(DATA));
while(rec!=NULL)
{
rec1=rec->forward;
while(rec1!=NULL)
{
if(strcmp(rec->firstname,rec1->firstname)>0)
{
strcpy(col->books,rec1->books);
strcpy(col->firstname,rec1->firstname);
strcpy(col->surname,rec1->surname);
strcpy(col->date_pub,rec1->date_pub);
col->booknum=rec1->booknum;
col->hall=rec1->hall;
col->num_books=rec1->num_books;
strcpy(rec1->books,rec->books);
strcpy(rec1->firstname,rec->firstname);
strcpy(rec1->surname,rec->surname);
strcpy(rec1->date_pub,rec->date_pub);
rec1->booknum=rec->booknum;
rec1->hall=rec->hall;
rec1->num_books=rec->num_books;
strcpy(rec->books,col->books);
strcpy(rec->firstname,col->firstname);
strcpy(rec->surname,col->surname);
strcpy(rec->date_pub,col->date_pub);
rec->booknum=col->booknum;
rec->hall=col->hall;
rec->num_books=col->num_books;
}
else
if(strcmp(rec->firstname,rec1->firstname)==0)
if(strcmp(rec->surname,rec1->surname)>0)
{
strcpy(col->books,rec1->books);
strcpy(col->firstname,rec1->firstname);
strcpy(col->surname,rec1->surname);
strcpy(col->date_pub,rec1->date_pub);
col->booknum=rec1->booknum;
col->hall=rec1->hall;
col->num_books=rec1->num_books;
strcpy(rec1->books,rec->books);
strcpy(rec1->firstname,rec->firstname);
strcpy(rec1->surname,rec->surname);
strcpy(rec1->date_pub,rec->date_pub);
rec1->booknum=rec->booknum;
rec1->hall=rec->hall;
rec1->num_books=rec->num_books;
strcpy(rec->books,col->books);
strcpy(rec->firstname,col->firstname);
strcpy(rec->surname,col->surname);
strcpy(rec->date_pub,col->date_pub);
rec->booknum=col->booknum;
rec->hall=col->hall;
rec->num_books=col->num_books;
}
rec1=rec1->forward;
}
rec=rec->forward;
}
rec=first;
}
//function which sorts data in increasing order of their hall number
void proc4(DATA *rec)
{
DATA *rec1,*col;
col=(DATA*)malloc(sizeof(DATA));
while(rec!=NULL)
{
rec1=rec->forward;
while(rec1!=NULL)
{
if(rec->hall>rec1->hall)
{
strcpy(col->books,rec1->books);
strcpy(col->firstname,rec1->firstname);
strcpy(col->surname,rec1->surname);
strcpy(col->date_pub,rec1->date_pub);
col->booknum=rec1->booknum;
col->hall=rec1->hall;
col->num_books=rec1->num_books;
strcpy(rec1->books,rec->books);
strcpy(rec1->firstname,rec->firstname);
strcpy(rec1->surname,rec->surname);
strcpy(rec1->date_pub,rec->date_pub);
rec1->booknum=rec->booknum;
rec1->hall=rec->hall;
rec1->num_books=rec->num_books;
strcpy(rec->books,col->books);
strcpy(rec->firstname,col->firstname);
strcpy(rec->surname,col->surname);
strcpy(rec->date_pub,col->date_pub);
rec->booknum=col->booknum;
rec->hall=col->hall;
rec->num_books=col->num_books;
}
rec1=rec1->forward;
}
rec=rec->forward;
}
rec=first;
}
//function which sorts data in increasing order of the number of books available
void proc5(DATA *rec)
{
DATA *rec1,*col;
col=(DATA*)malloc(sizeof(DATA));
while(rec!=NULL)
{
rec1=rec->forward;
while(rec1!=NULL)
{
if(rec->num_books>rec1->num_books)
{
strcpy(col->books,rec1->books);
strcpy(col->firstname,rec1->firstname);
strcpy(col->surname,rec1->surname);
strcpy(col->date_pub,rec1->date_pub);
col->booknum=rec1->booknum;
col->hall=rec1->hall;
col->num_books=rec1->num_books;
strcpy(rec1->books,rec->books);
strcpy(rec1->firstname,rec->firstname);
strcpy(rec1->surname,rec->surname);
strcpy(rec1->date_pub,rec->date_pub);
rec1->booknum=rec->booknum;
rec1->hall=rec->hall;
rec1->num_books=rec->num_books;
strcpy(rec->books,col->books);
strcpy(rec->firstname,col->firstname);
strcpy(rec->surname,col->surname);
strcpy(rec->date_pub,col->date_pub);
rec->booknum=col->booknum;
rec->hall=col->hall;
rec->num_books=col->num_books;
}
rec1=rec1->forward;
}
rec=rec->forward;
}
rec=first;
}
//function with the sub menu for the general sorting of data for books
void sort(DATA *rec)
{
clrscr();
window(1,1,80,25);
int san;
char **sconf,**scon;
scon=(char**)malloc(sizeof(char*)*1);
scon[0]=(char*)malloc(sizeof(char)*10);
scon[0]="SORT BY ";
sconf=(char **)malloc(sizeof(char*)*5);
for(i=0;i<5;i++)
sconf[i]=(char *)malloc(sizeof(char)*12);
sconf[0]="IDENTITY NUMBER";
sconf[1]="BOOK'S TITLE";
sconf[2]="NAMES";
sconf[3]="HALL NUMBER";
sconf[4]="NUMBER of BOOKS";
san=menu(20,5,scon,sconf,5,1,1);
switch(san)
{
case 1:
{
clrscr();
proc1(rec);
break;
};
case 2:
{
clrscr();
proc2(rec);
break;
};
case 3:
{
clrscr();
proc3(rec);
break;
};
case 4:
{
clrscr();
proc4(rec);
break;
};
case 5:
{
clrscr();
proc5(rec);
break;
};
case 0:
{
clrscr();
break;
};
}
free(scon);
free(sconf);
rec=first;
}
//function which adds new elements to data book's list after the first elemant
DATA *inpadd(DATA *ncol)
{
int m,p=0;
if(ncol==NULL)
{
printf("\n List is EMPTY you'll be creating one by adding new elements\n ");
printf("\n Give number of books to be considered...");
printf("\n Must be a number greater than 0 ");
printf("\n NO:= ");
fflush(stdin);
if((scanf("%d",&n))&&n>0)
p=0;
else
if((n<=0)||(!scanf("%d",&n)))
{
do
{
printf("\n ERROR OF INPUT !!!!! PLEASE TRY AGAIN");
printf("\n Must be a NUMBER greater than 0 ");
printf("\n NO:= ");
fflush(stdin);
if((scanf("%d",&n))&&n>0)
p=0;
else
if((n<=0)||(!scanf("%d",&n)))
p=1;
} while(p==1);
}
fflush(stdin);
ncol=inp1();
}
else
{
clrscr();
p=0;
printf("\n Here is the list you have chosen to add to:\n ");
outsm(ncol);
fflush(stdin);
printf("\n Give number of new books to be added ....");
printf("\n Must be a NUMBER greater than 0 ");
printf("\n NO:= ");
fflush(stdin);
if((scanf("%d",&m))&&(m>0))
p=0;
else
if((m<=0)||(!scanf("%d",&m)))
{
do
{
printf("\n ERROR OF INPUT !!!!! PLEASE TRY AGAIN");
printf("\n Must be a NUMBER greater than 0 n:= ");
printf("\n NO:= ");
fflush(stdin);
if((scanf("%d",&m))&&(m>0))
p=0;
else
if((m<=0)||(!scanf("%d",&m)))
p=1;
} while(p==1);
}
fflush(stdin);
ncol=first;
for(i=0;i<m;i++)
{
info=(DATA*)malloc(sizeof(DATA));
printf("\n Give title of no %d Book: ",i+1);
scanf("%s",info->books);
fflush(stdin);
printf("\n Give name of author: ");
scanf("%s",info->firstname);
fflush(stdin);
printf("\n Give surname of author: ");
scanf("%s",info->surname);
fflush(stdin);
printf("\n Date when book was published: ");
scanf("%s",info->date_pub);
fflush(stdin);
printf("\n Give book's identity number: ");
info->booknum=testnumber(num,0,999);
fflush(stdin);
printf("\n 1-Science , 2-Literature , 3-History , 4-Law");
printf("\n Give hall number to which the book belongs to: ");
info->hall=testnumber(num,0,5)-1;
fflush(stdin);
printf("\n Give number of copies of particular book: ");
info->num_books=testnumber(num,0,9999);
fflush(stdin);
info->forward=ncol->forward;
ncol->forward->backward=info;
info->backward=ncol;
ncol->forward=info;
}
}
return(ncol);
}
//function which deletes a specified element from data book's list
DATA *del(DATA *ncol)
{
int x,y,p=0;
clrscr();
if(!ncol)
return(NULL);
else
{
ncol=first;
printf("\n Here is the list you want to delete from :\n ");
//function outsm gives user the opportunity to choose
//the number of the book he wants deleted
//however choice must be only from those on screen!!!
y=outsm(ncol);
printf("\n Please give the BOOK'S NUMBER you want deleted");
printf("\n must be a NUMBER from those listed above ");
printf("\n No:= ");
fflush(stdin);
if((scanf("%d",&x))&&(x>0)&&(x<=y-1))
p=0;
else
if((x<=0)||(x>y-1)||(!scanf("%d",&x)))
{
do
{
printf("\n ERROR OF INPUT !!!!! PLEASE TRY AGAIN");
printf("\n Must be a NUMBER from those listed above ");
printf("\n No:= ");
fflush(stdin);
if((scanf("%d",&x))&&(x>0)&&(x<=y-1))
p=0;
else
if((x<=0)||(x>y-1)||(!scanf("%d",&x)))
p=1;
} while(p==1);
}
fflush(stdin);
for(j=0;j<x-1;j++)
ncol=ncol->forward;
if(first==ncol)
first=ncol->forward;
ncol->backward->forward=ncol->forward;
ncol->forward->backward=ncol->backward;
free(ncol);
ncol=first;
return(ncol);
};
}
//function which confirm users decision to create a new list
//deleting previously existing list.user may change his mind and return
//to main menu
int confirm()
{
int an;
char **pconf,**pcon;
pcon=(char**)malloc(sizeof(char*)*1);
pcon[0]=(char*)malloc(sizeof(char)*40);
pcon[0]="PREVIOUSLY EXITING LIST WILL BE DELETED";
pconf=(char**)malloc(sizeof(char*)*2);
for(i=0;i<2;i++)
pconf[i]=(char *)malloc(sizeof(char)*20);
pconf[0]="YES";
pconf[1]="CANCEL";
an=menu(20,5,pcon,pconf,2,1,1);
free(pcon);
free(pconf);
return an;
}
//function used to write strings into window
void intowin(int x,int y,int d,int w,char *title,char *str,int len)
{
void *destin;
int i;
char *inp;
int xtmp,ytmp;
_setcursortype(_NOCURSOR);
inp=(char *)malloc(sizeof(char)*30);
destin=(void *)malloc(2*d*w);
xtmp=wherex();
ytmp=wherey();
gettext(x,y,x+d-1,y+w-1,destin);
textbackground(BLUE);
textcolor(WHITE);
window(x,y,x+d-1,y+w-1);
clrscr();
window(1,1,80,25);
//-------------
//Draws border:
gotoxy(x,y);
cprintf("Ъ");
for(i=0;i<d-2;i++)
cprintf("Д");
cprintf("ї");
for(i=0;i<w-2;i++)
{
gotoxy(x+d-1,y+i+1);
cprintf("і");
}
gotoxy(x+d-1,y+w-1);
cprintf("Щ");
gotoxy(x+1,y+w-1);
for(i=0;i<d-2;i++)
cprintf("Д");
for(i=0;i<w-2;i++)
{
gotoxy(x,y+i+1);
cprintf("і");
}
gotoxy(x,y+w-1);
cprintf("А");
//-------------
//fixes title at topcenter of window
gotoxy(x+(d-strlen(title))/2,y);
cprintf("%s",title);
window(x+1,y+1,x+d-2,y+w-2);
_setcursortype(_SOLIDCURSOR);
inp[0]=len+1;
strcpy(str,cgets(inp));
textbackground(BLACK);
textcolor(WHITE);
puttext(x,y,x+d-1,y+w-1,destin);
free(destin);
free(inp);
gotoxy(xtmp,ytmp);
_setcursortype(_NORMALCURSOR);
}
//function for saving selected data book's list to file
void savenew(DATA*tosav)
{
FILE *fp;
int cre=0;
char str[30];
intowin(10,10,40,3,"SAVE AS",str,30);
fflush(stdin);
if((fp=fopen(str,"rb"))==NULL)
cre=1;
else
{
window(1,1,80,25);
int an;
char **conf,**con;
con=(char **)malloc(sizeof(char*)*1);
con[0]=(char *)malloc(sizeof(char)*35);
con[0]="FILE ALREADY EXISTS OVERWRITE IT";
conf=(char **)malloc(sizeof(char*)*2);
for(i=0;i<2;i++)
conf[i]=(char *)malloc(sizeof(char)*10);
conf[0]="ENTER";
conf[1]="CANCEL";
an=menu(20,5,con,conf,2,1,1);
free(con);
free(conf);
if(an==2)
an=0;
else
cre=1;
}
if(cre==1)
{
if((fp=fopen(str,"wb"))==NULL)
{
_setcursortype(_NOCURSOR);
window(1,1,80,25);
gotoxy(10,15);
textcolor(RED);
cprintf("SYSTEM ERROR:CAN'T OPEN FILE - %s",str);
textcolor(YELLOW);
char *erob="PRESS ANY KEY TO RETURN TO MENU";
putd_text(10,16,erob);
getch();
clrscr();
textbackground(BLACK);
fflush(stdin);
}
else
{
n=0;
do
{
_setcursortype(_NOCURSOR);
n++;
tosav=tosav->forward;
} while(tosav!=NULL);
fwrite(&n,sizeof(int),1,fp);
tosav=first;
for(i=0;i<n;i++)
{
fwrite(tosav,sizeof(DATA)-2*sizeof(struct link*),1,fp);
tosav=tosav->forward;
}
fclose(fp);
window(1,1,80,25);
gotoxy(10,15);
textcolor(RED);
cprintf("FILE { %s } SAVED.",str);
gotoxy(10,16);
textcolor(YELLOW);
cprintf("PLEASE PRESS ANY KEY TWICE TO RETURN TO MENU");
getch();
clrscr();
textbackground(BLACK);
}
}
}
//function which reads data book's list from file
DATA *loadfile()
{
FILE *fp;
char rstr[30];
clrscr();
intowin(10,10,40,3,"GIVE FILE NAME",rstr,30);
fflush(stdin);
if((fp=fopen(rstr,"rb"))==NULL)
{
_setcursortype(_NOCURSOR);
window(1,1,80,25);
gotoxy(10,15);
textcolor(RED);
cprintf("SYSTEM ERROR:CAN'T OPEN FILE -{ %s }",rstr);
gotoxy(10,16);
textcolor(YELLOW);
cprintf("PLEASE PRESS ANY KEY TO CONTINUE");
getch();
clrscr();
textbackground(BLACK);
return(NULL);
}
//creates memory space and new list from the read data
else
{
fread(&n,sizeof(int),1,fp);
first=fur=info=NULL;
for(i=0;i<n;i++)
{
if(first==NULL)
{
info=(DATA*)malloc(sizeof(DATA));
fread(info,sizeof(DATA)-2*sizeof(struct link*),1,fp);
info->backward=NULL;
info->forward=NULL;
first=info;
}
else
{
fur=info;
info=(DATA*)malloc(sizeof(DATA));
fread(info,sizeof(DATA)-2*sizeof(struct link*),1,fp);
info->backward=fur;
info->forward=NULL;
fur->forward=info;
}
}
info=first;
fclose(fp);
return(info);//returns a new list from read data
}
}
//function for updating data book's list
DATA *update(DATA *ncol)
{
int x,y,uan,p=0;
char **conf,**con;
clrscr();
if(!ncol)
{
return(NULL);
}
else
{
printf("\n Here is the list you want to update :\n ");
//function outsm gives user again the opportunity to choose
//the number of the book whose data he wants updated
//however choice must be only from those on screen!!!
y=outsm(ncol);
printf("\n Please give the NO of the book whose data you want updated");
printf("\n must be a NO from those listed above ");
printf("\n NO:= ");
fflush(stdin);
if((scanf("%d",&x))&&(x>0)&&(x<=y-1))
p=0;
else
if((x<=0)||(x>y-1)||(!scanf("%d",&x)))
{
do
{
printf("\n ERROR OF INPUT !!!!! PLEASE TRY AGAIN");
printf("\n Must be a NO from those listed above ");
printf("\n NO:= ");
fflush(stdin);
if((scanf("%d",&x))&&(x>0)&&(x<=y-1))
p=0;
else
if((x<=0)||(x>y-1)||(!scanf("%d",&x)))
p=1;
} while(p==1);
}
fflush(stdin);
ncol=first;
for(i=1;i<x;i++)
ncol=ncol->forward;
clrscr();
//sub menu to providing for the
//choice of parameter to be updated
window(1,1,80,25);
con=(char**)malloc(sizeof(char*)*1);
*con=(char *)malloc(sizeof(char)*22);
con[0]="What do I update?";
conf=(char**)malloc(sizeof(char*)*8);
for(i=0;i<8;i++)
conf[i]=(char *)malloc(sizeof(char)*13);
conf[0]="ALL";
conf[1]="BOOK";
conf[2]="NAMES";
conf[3]="DATE PUBLISHED";
conf[4]="IDENTITY BOOK NO.";
conf[5]="HALL NO.";
conf[6]="NO. of BOOKS";
conf[7]="CANCEL";
uan=menu(20,5,con,conf,8,1,1);
switch(uan)
{
case 1:
{
//updates all info of the book's names
printf("\n Book's former title: %s",ncol->books);
printf("\n Please give new title= ");
scanf("%s",ncol->books);
fflush(stdin);
printf("\n Author's former name: %s",ncol->firstname);
printf("\n Please give new name= ");
scanf("%s",ncol->firstname);
fflush(stdin);
printf("\n Author's former surname: %s",ncol->surname);
printf("\n Please give new surname= ");
scanf("%s",ncol->surname);
fflush(stdin);
printf("\n Old date when book was published: %s",ncol->date_pub);
printf("\n New Date when book was published= ");
scanf("%s",&ncol->date_pub);
fflush(stdin);
printf("\n Book's former identity number: %d",ncol->booknum);
printf("\n Please give new book's identity number= ");
fflush(stdin);
ncol->booknum=testnumber(num,0,9999);
printf("\n Former hall to which book was located: %s",halls[ncol->hall]);
printf("\n 1-Science , 2-Literature , 3-History , 4-Law");
printf("\n Please give new hall number= ");
ncol->hall=testnumber(num,0,5)-1;
fflush(stdin);
printf("\n Former number of copies of book: %d",ncol->num_books);
printf("\n Please give new number of copies= ");
fflush(stdin);
ncol->num_books=testnumber(num,0,9999);
clrscr();
break;
};
case 2:
{ //update book's title
printf("\n Former title for book: %s",ncol->books);
printf("\n Please give new title of book= ");
scanf("%s",&ncol->books);
fflush(stdin);
clrscr();
break;
};
case 3:
{
//updates author's names
printf("\n Author's former name: %s",ncol->firstname);
printf("\n Please give new name= ");
scanf("%s",ncol->firstname);
fflush(stdin);
printf("\n Author's former surname: %s",ncol->surname);
printf("\n Please give new surname= ");
scanf("%s",ncol->surname);
fflush(stdin);
clrscr();
break;
};
case 4:
{ // update date when book was published
printf("\n Old date when book was published: %s",ncol->date_pub);
printf("\n New Date when book was published= ");
scanf("%s",&ncol->date_pub);
fflush(stdin);
clrscr();
break;
};
case 5:
{ // update book's identity number
printf("\n Book's former identity number: %d",ncol->booknum);
printf("\n Please give new book's identity number= ");
ncol->booknum=testnumber(num,0,9999);
fflush(stdin);
clrscr();
break;
}
case 6:
{ // update hall number
printf("\n Former hall to which reader is acquainted: %s",halls[ncol->hall]);
printf("\n 1-Science , 2-Literature , 3-History , 4-Law");
printf("\n Please give new hall number= ");
ncol->hall=testnumber(num,0,5)-1;
fflush(stdin);
clrscr();
break;
};
case 7:
{ // update copies of book
printf("\n Former number of copies of book: %d",ncol->num_books);
printf("\n Please give new number of copies= ");
fflush(stdin);
ncol->num_books=testnumber(num,0,9999);
clrscr();
break;
};
case 8:
{
printf("\n Data remains unchanged.");
break;
};
case 0:
{
printf("\n Data remains unchanged.");
break;
};
}
free(con);
free(conf);
ncol=first;
return(ncol);
}
}
//function which searches for book/s of given properties
DATA *search_for(DATA *scol)
{
DATA **searcher,*sfirst;
char **sconf,**scon;
char sname[17],ssurname[17],sbooks[17],sdate_pub[19];
int ronk,x,y,shall,snum_books,sbooknum;
clrscr();
if(!scol)
{
return(NULL);
}
else
{
scol=first;
n=0;
do
{
n++;
scol=scol->forward;
} while(scol!=NULL);
scol=first;
clrscr();
//sub menu for search
window(1,1,80,25);
int an;
scon=(char **)malloc(sizeof(char*)*1);
scon[0]=(char*)malloc(sizeof(char)*12);
scon[0]="SEARCH BY";
sconf=(char **)malloc(sizeof(char*)*8);
for(i=0;i<8;i++)
sconf[i]=(char*)malloc(sizeof(char)*13);
sconf[0]="ALL";
sconf[1]="BOOKS";
sconf[2]="NAMES";
sconf[3]="DATE PUBLISHED";
sconf[4]="IDENTITY NUMBER";
sconf[5]="HALL";
sconf[6]="NUMBER OF BOOKS";
sconf[7]="CANCEL";
ronk=menu(20,5,scon,sconf,8,1,1);
free(scon);
free(sconf);
clrscr();
//for(i=0;i<n;i++)
// *(searcher+i)=(DATA*)malloc(sizeof(DATA));
switch(ronk)
{
case 1:
{
//search for book by all parameters
intowin(10,10,30,3,"BOOKS",sbooks,20);
fflush(stdin);
intowin(10,10,30,3,"GIVE NAME",sname,20);
fflush(stdin);
intowin(10,10,30,3,"GIVE SURNAME",ssurname,20);
fflush(stdin);
intowin(10,10,30,3,"DATE PUBLISHED",sdate_pub,20);
fflush(stdin);
clrscr();
gotoxy(15,5);
printf(" IDENTITY NUMBER=");
fflush(stdin);
sbooknum=testnumber(num,0,9999);
fflush(stdin);
clrscr();
gotoxy(15,5);
printf(" HALL NUMBER=");
fflush(stdin);
shall=testnumber(num,0,5)-1;
clrscr();
gotoxy(15,5);
printf(" NUMBER OF BOOKS=");
fflush(stdin);
sbooknum=testnumber(num,0,9999);
clrscr();
srch=0;
first=fur=info=NULL;
for(i=0;i<n;i++)
{
if((strcmp(scol->books,sbooks)==0)&&(strcmp(scol->firstname,sname)==0)&&(strcmp(scol->surname,ssurname)==0)&&(strcmp(scol->date_pub,sdate_pub)==0)
&&(scol->booknum==sbooknum)&&(scol->hall==shall))
{
if(first==NULL)
{
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=NULL;
info->forward=NULL;
first=info;
}
else
{
fur=info;
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=fur;
info->forward=NULL;
fur->forward=info;
}
}
scol=scol->forward;
}
info=first;
scol=info;
break;
};
case 2:
{
//searching for those with a given title
intowin(10,10,30,3,"GIVE BOOK'S TITLE",sbooks,20);
fflush(stdin);
srch=0;
first=fur=info=NULL;
for(i=0;i<n;i++)
{
if(strcmp(scol->books,sbooks)==0)
{
if(first==NULL)
{
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=NULL;
info->forward=NULL;
first=info;
}
else
{
fur=info;
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=fur;
info->forward=NULL;
fur->forward=info;
}
}
scol=scol->forward;
}
info=first;
scol=info;
break;
};
case 3:
{
//search by names
intowin(10,10,30,3,"GIVE NAME",sname,20);
fflush(stdin);
intowin(10,10,30,3,"GIVE SURNAME",ssurname,20);
fflush(stdin);
srch=0;
first=fur=info=NULL;
for(i=0;i<n;i++)
{
if((strcmp(scol->firstname,sname)==0)&&(strcmp(scol->surname,ssurname)==0))
{
if(first==NULL)
{
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=NULL;
info->forward=NULL;
first=info;
}
else
{
fur=info;
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=fur;
info->forward=NULL;
fur->forward=info;
}
}
scol=scol->forward;
}
info=first;
scol=info;
break;
};
case 4:
{
//searching for those with the same date being published
intowin(10,10,30,3,"DATE PUBLISHED",sdate_pub,20);
fflush(stdin);
srch=0;
first=fur=info=NULL;
for(i=0;i<n;i++)
{
if(strcmp(scol->date_pub,sdate_pub)==0)
{
if(first==NULL)
{
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=NULL;
info->forward=NULL;
first=info;
}
else
{
fur=info;
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=fur;
info->forward=NULL;
fur->forward=info;
}
}
scol=scol->forward;
}
info=first;
scol=info;
break;
};
case 5:
{// search for those with same book's identity number
gotoxy(20,5);
printf(" PLEASE GIVE BOOK'S IDENTITY NUMBER ");
gotoxy(20,7);
printf(" BOOK'S IDENTITY NUMBER= ");
sbooknum=testnumber(num,0,9999);
fflush(stdin);
srch=0;
first=fur=info=NULL;
for(i=0;i<n;i++)
{
if((scol->booknum)==sbooknum)
{
if(first==NULL)
{
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=NULL;
info->forward=NULL;
first=info;
}
else
{
fur=info;
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=fur;
info->forward=NULL;
fur->forward=info;
}
}
scol=scol->forward;
}
info=first;
scol=info;
break;
};
case 6:
{ // search with those with same hallnumber
gotoxy(17,6);
printf(" PLEASE GIVE THE HALL NUMBER TO WHICH BOOK BELONGS ");
gotoxy(15,5);
printf("{ [1]-Science , [2]-Literature , [3]-History , [4]-Law } ");
gotoxy(20,8);
printf(" HALL NUMBER= ");
shall=testnumber(num,0,5)-1;
fflush(stdin);
srch=0;
first=fur=info=NULL;
for(i=0;i<n;i++)
{
if((scol->hall)==shall)
{
if(first==NULL)
{
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=NULL;
info->forward=NULL;
first=info;
}
else
{
fur=info;
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=fur;
info->forward=NULL;
fur->forward=info;
}
}
scol=scol->forward;
}
info=first;
scol=info;
break;
};
case 7:
{ // search with same copies of books
gotoxy(20,5);
printf(" GIVE NUMBER OF COPIES OF PARTICULAR BOOK ");
gotoxy(20,7);
printf(" NUMBER OF COPIES= ");
snum_books=testnumber(num,0,9999);
fflush(stdin);
srch=0;
first=fur=info=NULL;
for(i=0;i<n;i++)
{
if((scol->num_books)==snum_books)
{
if(first==NULL)
{
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=NULL;
info->forward=NULL;
first=info;
}
else
{
fur=info;
info=(DATA*)malloc(sizeof(DATA));
info=copyelsearch(scol);
info->backward=fur;
info->forward=NULL;
fur->forward=info;
}
}
scol=scol->forward;
}
info=first;
scol=info;
break;
};
case 8:
{
//cancels search
ronk=0;
break;
};
case 0:
{
// default case cancels search
ronk=0;
break;
};
}
return(scol);
}
}
//function just to copy elements
DATA *copyelsearch(DATA *scol)
{
strcpy(info->books,scol->books);
strcpy(info->firstname,scol->firstname);
strcpy(info->surname,scol->surname);
strcpy(info->date_pub,scol->date_pub);
info->booknum=scol->booknum;
info->hall=scol->hall;
info->num_books=scol->num_books;
return(info);
}
//function which confirms or reject exit from main menu
int quit()
{
int can;
char **usure,**sur;
sur=(char **)malloc(sizeof(char*)*2);
for(i=0;i<2;i++)
sur[i]=(char *)malloc(sizeof(char)*35);
sur[0]="YOU WILL LOSE ALL UNSAVED INFO";
sur[1]=" CONFIRM EXIT";
usure=(char **)malloc(sizeof(char*)*2);
for(i=0;i<2;i++)
usure[i]=(char*)malloc(sizeof(char)*10);
usure[0]="YES";
usure[1]="CANCEL";
can=menu(20,5,sur,usure,2,2,1);
free(usure);
free(sur);
return can;
}
Соседние файлы в предмете Программирование