Monday, November 30, 2009

Chap 13[B](a) Reverse words in text file

Write a program to carry out the following:
(a) Read a text file provided at command prompt
(b) Print each word in reverse order
For example if the file contains
INDIA IS MY COUNTRY
Output should be
AIDNI SI YM YRTNUOC


How to use:
1) Compile the below program to obtain .exe file.
2) In turbo C press F9 to compile and go to C:\TC\BIN to find your .exe file
3) Rename your.exe file to any name for example reverse.exe.
4) In the folder where reverse.exe is, create a text file (e.g test.txt) and input any text in it.
5) For vista, in the same folder hold shift and right click anywhere in the folder and click open command prompt here.
6) Then in command prompt type :
reverse test.txt test2.txt
7)The reversed text is found in test2.txt.




#include "stdio.h"
void main(int argc,char *argv[])
{
int i,j,f=1;
FILE *fs,*ft;
char ch,word[25],word2[25];

if(argc!=3)
{
puts("Improper number of arguments");
exit();
}

fs=fopen(argv[1],"r");
if(fs==NULL)
{
puts("\nCannot open source file");
exit();
}

ft=fopen(argv[2],"w");

while(1)
{
word2[0]='\0';
word[0]='\0';
for(i=0;;i++)
{
ch=fgetc(fs);
if(ch==EOF)
{
f=0;
break;
}
if(ch==32)
{
f=2;
break;
}
if(ch==44)
{
f=3;
break;
}
if(ch==46)
{
f=4;
break;
}
if(ch=='\n')
{
f=5;
break;
}
word[i]=ch;

}
word[i]='\0';
for(j=0,i-=1;i>=0;i--,j++)
word2[j]=word[i];

word2[j]='\0';
fputs(word2,ft);
if(f==2)
fputc(32,ft);
if(f==3)
fputc(44,ft);
if(f==4)
fputc(46,ft);
if(f==5)
fputc('\n',ft);
if(f==0)
break;
}
fclose(fs);
fclose(ft);
}

Sunday, November 15, 2009

Chap12[C]j Sort data in file according to date

There are 100 records present in a file with the following structure:
struct date
{
int d, m, y ;
} ;
struct employee
{ int empcode[6] ;
char empname[20] ;
struct date join_date ;
float salary ;
} ;
Write a program to read these records, arrange them in ascending order of join_date and write them in to a target file.


#include "stdio.h"
struct date
{
int d,m,y;
};
struct employee
{
int empcode;
char empname[20];
struct date da;
float salary;
};

void main()
{
int choice;
char another,another2;
FILE *fs;
struct employee e;
fs=fopen("c:\\ctxt\\join.txt","a+");
while(1)
{
clrscr();
printf("1) Input new employee data");
printf("\n2) Sort employee data");
printf("\n3) Exit");
printf("\n\nKey in your choice(1,2 or 3)\n");
scanf("%d",&choice);

switch(choice)
{
case 1:
while(1)
{
clrscr();
printf("\nKey in employee's code\n");
scanf("%d",&e.empcode);
printf("\nKey in employee's name\n");
scanf("%s",e.empname);
printf("\nKey in year that employee joined\n");
scanf("%d",&e.da.y);
printf("\nKey in month that employee joined\n");
scanf("%d",&e.da.m);
printf("\nKey in day that employee joined\n");
scanf("%d",&e.da.d);
printf("\nKey in salary of employee\n");
scanf("%f",&e.salary);
fprintf(fs,"%d %s %d %d %d %f\n",e.empcode,e.empname,e.da.y,e.da.m,e.da.d,e.salary);
printf("\nDo you want to key in another data?(y/n)\n");
fflush(stdin);
another=getche();
if(another!='y')
break;
}
break;

case 2:
funcsort(fs);
break;

case 3:
fclose(fs);
exit();
}
}
}

funcsort(FILE *fs)
{
int i,j,flag=0,count=0;
struct employee temp1,temp2;
struct employee e1[1000];
char ch;
FILE *fp;
fp=fopen("c:\\ctxt\\join2.txt","w");
rewind(fs);
for(i=0;i<1000;i++)
{
if(flag==0)
{
ch=fscanf(fs,"%d%s%d%d%d%f",&e1[i].empcode,e1[i].empname,&e1[i].da.y,&e1[i].da.m,&e1[i].da.d,&e1[i].salary);
count++;
if(ch==EOF)
flag=1;
}
if(flag==1)
{
e1[i].empname[0]='\0';
}
}
for(i=0;e1[i].empname[0]!=0;i++)
{
for(j=i+1;j {
if(e1[i].da.y>e1[j].da.y)
{
temp1=e1[i];
temp2=e1[j];
e1[i]=temp2;
e1[j]=temp1;
}
else if((e1[i].da.y==e1[j].da.y)&&(e1[i].da.m==e1[j].da.m))
{
if(e1[i].da.d>e1[j].da.d)
{
temp1=e1[i];
temp2=e1[j];
e1[i]=temp2;
e1[j]=temp1;
}
}
else if(e1[i].da.y==e1[j].da.y)
{
if(e1[i].da.m>e1[j].da.m)
{
temp1=e1[i];
temp2=e1[j];
e1[i]=temp2;
e1[j]=temp1;
}
}

}
}

for(i=0;e1[i].empname[0]!=0;i++)
{
fprintf(fp,"%d %s %d %d %d %f\n",e1[i].empcode,e1[i].empname,e1[i].da.y,e1[i].da.m,e1[i].da.d,e1[i].salary);
}
fclose(fp);
fclose(fs);
remove("c:\\ctxt\\join.txt");
rename("c:\\ctxt\\join2.txt","c:\\ctxt\\join.txt");
fs=fopen("c:\\ctxt\\join.txt","a+");
}

Chap12[C]i Updating customer transactions with sorting feature

In the file ‘CUSTOMER.DAT’ there are 100 records with the following structure:
struct customer
{
int accno ;
char name[30] ;
float balance ;
} ;

In another file ‘TRANSACTIONS.DAT’ there are several records with the following structure:
struct trans
{
int accno ;
char trans_type ;
float amount ;
} ;
The parameter trans_type contains D/W indicating deposit or withdrawal of amount. Write a program to update ‘CUSTOMER.DAT’ file, i.e. if the trans_type is ‘D’ then update the balance of ‘CUSTOMER.DAT’ by adding amount to balance for the corresponding accno. Similarly, if trans_type is ‘W’ then subtract the amount from balance. However, while subtracting the amount make sure that the amount should not get overdrawn, i.e. at least 100 Rs. Should remain in the account.


Create a folder "ctxt" in C drive. In the "ctxt" folder create two text files with name cust.txt and trans.txt.
when option 4 is executed trans.txt get deleted while new transactions can be created with option 5 in program.

#include "stdio.h"
struct customer
{
int accno;
char name[30];
float balance;
};

struct trans
{
int accno;
char trans_type[1];
float amount;
};

void main()
{
char another,name1[30];
FILE *fs,*ft,*ftemp;
struct customer c;
struct trans t;
int choice,no,f2,no2,counter;
long pos1,pos2,pos3;
fs=fopen("c:\\ctxt\\cust.txt","r+");

if(fs==NULL)
{
puts("Cannot open file");
exit();
}

while(1)
{
clrscr();
printf("1)List Customer Data");
printf("\n2)Add Customer Data");
printf("\n3)Remove Customer Data");
printf("\n4)Update Customer Data from transactions");
printf("\n5)Perform transactions");
printf("\n6)Exit\n");
scanf("%d",&choice);
rewind(fs);

switch(choice)
{
case 1:

another='y';
clrscr();
while(fscanf(fs,"%d%s%f",&c.accno,c.name,&c.balance)!=EOF)
printf("\nAccount no: %3d Name: %-20s Balance: %.1f",c.accno,c.name,c.balance);
while(1)
{
printf("\n\nHit enter to escape");
fflush(stdin);
another=getche();
if(another!='y')
break;
}
break;

case 2:

clrscr();
another='y';
no=1;
funcsort(fs);
while(1)
{

f2=0;

for(no=1;fscanf(fs,"%d%s%f",&c.accno,c.name,&c.balance)!=EOF;no++)
{
if(f2==0)
{
if(c.accno!=no)
{
no2=no;
f2=1;
}

}

}
printf("\nKey in Customer name\n");
scanf("%s",c.name);
printf("\nKey in balance\n");
scanf("%f",&c.balance);
if(f2==1)
{
fprintf(fs,"%d %s %f\n",no2,c.name,c.balance);
}
else
{
fprintf(fs,"%d %s %f\n",no,c.name,c.balance);
no++;
}
funcsort(fs);
printf("\nAdd another customer Data(y/n)?\n");
fflush(stdin);
another=getche();
if(another!='y')
break;
}
break;

case 3:

another='y';
clrscr();

while(1)
{
ftemp=fopen("c:\\ctxt\\cust2.txt","w");
printf("\nKey in name to delete\n");
scanf("%s",name1);
while(fscanf(fs,"%d%s%f",&c.accno,c.name,&c.balance)!=EOF)
{
if(strcmp(c.name,name1)!=0)
fprintf(ftemp,"%d %s %f\n",c.accno,c.name,c.balance);
}
fclose(fs);
fclose(ftemp);
remove("c:\\ctxt\\cust.txt");
rename("c:\\ctxt\\cust2.txt","c:\\ctxt\\cust.txt");
printf("\nDelete another data?(y/n)\n");
fflush(stdin);
fs=fopen("c:\\ctxt\\cust.txt","r+");
another=getche();
if(another!='y')
break;
}
break;

case 4:

clrscr();
ft=fopen("c:\\ctxt\\trans.txt","r");
if(ft==NULL)
{
puts("No active transactions");
exit();
}
while(fscanf(ft,"%d%s%f",&t.accno,t.trans_type,&t.amount)!=EOF)
{
counter=0;
pos1=0;
rewind(fs);
while(fscanf(fs,"%d%s%f",&c.accno,c.name,&c.balance)!=EOF)
{
counter++;
pos2=pos1;
pos1=ftell(fs);
pos3=pos1-pos2;
if(t.accno==c.accno)
{
if(t.trans_type[0]=='w')
{
c.balance-=t.amount;
if(c.balance<100)
{
c.balance+=t.amount;
printf("\nTransaction for account no: %d not processed due to insufficient funds");
}
}
if(t.trans_type[0]=='d')
{
c.balance+=t.amount;

}
if(counter==1)
fseek(fs,-(pos3),SEEK_CUR);
if(counter>1)
fseek(fs,-(pos3-2),SEEK_CUR);
fprintf(fs,"%d %s %f\n",c.accno,c.name,c.balance);
counter=1;
}

}
}
fclose(ft);
remove("c:\\ctxt\\trans.txt");
break;

case 5:

clrscr();
another='y';
ft=fopen("c:\\ctxt\\trans.txt","a+");
if(ft==NULL)
{
puts("Cannot open file");
break;
}
while(1)
{
printf("\nKey in acc no.\n");
scanf("%d",&t.accno);
printf("\nWithdraw (w) or Deposit (d)\n");
scanf("%s",t.trans_type);
printf("\nKey in amount\n");
scanf("%f",&t.amount);
fprintf(ft,"%-3d %s %-6.2f\n",t.accno,t.trans_type,t.amount);
printf("\nDo you want to key another transaction(y/n)?\n");
fflush(stdin);
another=getche();
if(another!='y')
break;
}
fclose(ft);
break;

case 6:

fclose(fs);
exit();

}
}
fclose(fs);
}

funcsort(FILE *fp)
{
char ch;
int flag=0,i,j,count;
struct customer a[1000],temp1,temp2;
FILE *ftemp;
rewind(fp);
ftemp=fopen("c:\\ctxt\\cust2.txt","w");
for(i=0,count=0;i<1000;i++)
{
if(flag==0)
{
ch=fscanf(fp,"%d%s%f",&a[i].accno,a[i].name,&a[i].balance);
count++;
if(ch==EOF)
{
flag=1;
}
}
if(flag==1)
{
a[i].name[0]=0;
}
}

for(i=0;a[i].name[0]!=0;i++)
{
for(j=i+1;j<(count-1);j++)
{
if(a[i].accno>a[j].accno)
{
temp1=a[i];
temp2=a[j];
a[i]=temp2;
a[j]=temp1;
}
}
}

for(i=0;a[i].name[0]!=0;i++)
{
fprintf(ftemp,"%d %s %f\n",a[i].accno,a[i].name,a[i].balance);
}
fclose(fp);
fclose(ftemp);
remove("c:\\ctxt\\cust.txt");
rename("c:\\ctxt\\cust2.txt","c:\\ctxt\\cust.txt");
fp=fopen("c:\\ctxt\\cust.txt","r+");
}

Chap12[C]h Encrypting/Decrypting file

Write a program to encrypt/decrypt a file using:
An offset cipher: In an offset cipher each character from the source file is offset with a fixed value and then written to the target file. For example, if character read from the source file is ‘A’, then convert this into a new character by offsetting ‘A’ by a fixed value, say 128, and then writing the new character to the target file.


Program to encrypt file

#include "stdio.h"
void main()
{
FILE *fp,*ft;
char ch;

fp=fopen("c:\\ctxt\\cipher.txt","r");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
ft=fopen("c:\\ctxt\\cipher2.txt","w");
if(ft==NULL)
{
puts("Cannot open file");
exit();
}

while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
ch+=128;
fputc(ch,ft);
};
fclose(fp);
fclose(ft);
remove("c:\\ctxt\\cipher.txt");
rename("c:\\ctxt\\cipher2.txt","c:\\ctxt\\cipher.txt");
}

Program to decrypt the same file


#include "stdio.h"
void main()
{
FILE *fp,*ft;
char ch;
fp=fopen("c:\\ctxt\\cipher.txt","r");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
ft=fopen("c:\\ctxt\\cipher2.txt","w");
if(ft==NULL)
{
puts("Cannot open file");
exit();
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
ch-=128;
fputc(ch,ft);
};
fclose(fp);
fclose(ft);
remove("c:\\ctxt\\cipher.txt");
rename("c:\\ctxt\\cipher2.txt","c:\\ctxt\\cipher.txt");
}

Saturday, November 14, 2009

Chap12[C]d Sorting to alphabetical order in a file

Suppose a file contains student’s records with each record containing name and age of a student. Write a program to read these records and display them in sorted order by name.

Program to input students name
#include "stdio.h"

void main()
{
char j='y';
FILE *fp;

struct record
{
char name[20];
int age;
};
struct record a;

fp=fopen("c:\\ctxt\\record.txt","a");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
while(j=='y')
{
printf("\nKey in the name of the student\n");
scanf("%s",a.name);
printf("\nKey in the age of the student\n");
scanf("%d",&a.age);
fprintf(fp,"%s %d\n",a.name,a.age);
printf("\nAdd another record?(Y/N)");
fflush(stdin);
j=getche();
}
fclose(fp);
}

Program to sort students name


#include "stdio.h"
struct record
{
char name[10];
int age;
};
void main()
{
FILE *fp,*ft;

int i,f=1;

struct record a[5000];

clrscr();


fp=fopen("c:\\ctxt\\record.txt","r");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
for(i=0;i<5000;i++)
{
if(f==1)
{
if((fscanf(fp,"%s%d",a[i].name,&a[i].age)==EOF))
f=0;
}
if(f==0)
a[i].name[0]='\0';

}

funcsort(a,4);

for(i=0;a[i].name[0]!='\0';i++)
printf("\nName is %s. Age is %d",a[i].name,a[i].age);
printf("\nFile sorted according to alphabetical order");

ft=fopen("c:\\ctxt\\record2.txt","w");
for(i=0;a[i].name[0]!=0;i++)
fprintf(ft,"%s %d\n",a[i].name,a[i].age);
fclose(fp);
fclose(ft);
remove("c:\\ctxt\\record.txt");
rename("c:\\ctxt\\record2.txt","c:\\ctxt\\record.txt");

}
/* Function "funcsort()" is in picture below type it in for it to work */

Thursday, November 12, 2009

Chap12[C]g Reading text on console with pages

Write a program to display the contents of a text file on the screen. Make following provisions: Display the contents inside a box drawn with opposite corner co-ordinates being ( 0, 1 ) and ( 79, 23 ). Display the name of the file whose contents are being displayed, and the page numbers in the zeroth row. The moment one screenful of file has been displayed, flash a message ‘Press any key...’ in 24th row. When a key is hit, the next page’s contents should be displayed, and so on till the end of file.

Create a folder called "ctxt" in C drive and create a text file called page.txt in the ctxt folder.
Input lots of words into the page.txt file then run this program.

#include "stdio.h"
#include "dos.h"

void main()
{
void creategrid();
FILE *fp;
char ch,k;
int page=1,i,j,pgno,flag=0,count;


fp=fopen("c:\\ctxt\\page.txt","r");
while(1)
{
count=0;
clrscr();
creategrid();
for(i=3;i<=21&&flag!=1;i++)
{

for(j=4;j<=77&&flag!=1;j++)
{
ch=fgetc(fp);
if(ch==EOF)
{
flag=1;
break;
}
gotoxy(j,i);
printf("%c",ch);
count++;
}
}
gotoxy(38,24);
printf("Page %d",page);
if(page>1)
{
gotoxy(3,24);
printf("Key %c to go to page %d",17,page-1);
}
if(flag!=1)
{
gotoxy(58,24);
printf("Key %c to go to page %d",16,page+1);
}
if(flag==1)
{
gotoxy(66,24);
printf("Key %c to quit",16);
}
while(1)
{
pgno=getkey();
if(flag==1&&pgno==77)
break;
if(page==1&&pgno==75)
continue;

if(pgno==77&&flag!=1)
{
page+=1;
clrscr();
creategrid();
break;
}

if(pgno==75&&page!=1)
{
page-=1;
clrscr();
fseek(fp,-(1413+count),SEEK_CUR);
creategrid();
flag=0;
break;
}
}
if(flag==1&&pgno==77)
break;



}
fclose(fp);
}

getkey()
{
union REGS i,o;
while(!kbhit())
;
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}

void creategrid()
{
int i;
gotoxy(2,1);
printf("%c",218);
gotoxy(79,1);
printf("%c",191);
gotoxy(2,23);
printf("%c",192);
gotoxy(79,23);
printf("%c",217);

for(i=3;i<=78;i++)
{
gotoxy(i,1);
printf("%c",196);
gotoxy(i,23);
printf("%c",196);
}

for(i=2;i<=22;i++)
{
gotoxy(2,i);
printf("%c",179);
gotoxy(79,i);
printf("%c",179);
}
}

Wednesday, November 11, 2009

Chap12[C]f Merge alternate lines from two files

Write a program that merges lines alternately from two files and writes the results to new file. If one file has less number of lines than the other, the remaining lines from the larger file should be simply copied into the target file.

#include "stdio.h"
void main()
{
FILE *fp,*ft,*fs;
char ch;
int f1=1,f2=1,counter=1;

fp=fopen("c:\\ctxt\\first.txt","r");
ft=fopen("c:\\ctxt\\second.txt","r");
fs=fopen("c:\\ctxt\\combine.txt","w");

while(1)
{
if(counter==1||f2==0)
{
ch=fgetc(fp);
if(ch==EOF)
f1=0;
if(ch=='\n'||ch==46)
counter=2;
if(f1!=0)
fputc(ch,fs);
}

if(counter==2||f1==0)
{
ch=fgetc(ft);
if(ch==EOF)
f2=0;

if(ch=='\n'||ch==46)
counter=1;
if(f2!=0)
fputc(ch,fs);

if(f1==0&&f2==0)
break;
}
}

fclose(fp);
fclose(ft);
fclose(fs);
}

Chap12[C]e Lowercase to uppercase in file

Write a program to copy one file to another. While doing so replace all lowercase characters to their equivalent uppercase characters.

#include "stdio.h"

void main()
{
FILE *fp,*ft;
char ch;

fp=fopen("c:\\ctxt\\txt1.txt","r");
ft=fopen("c:\\ctxt\\txt2.txt","w");

if(fp==NULL)
{
puts("Cannot open file");
exit();
}

if(ft==NULL)
{
puts("Cannot open file");
exit();
}

while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
if(ch>=97&&ch<=122)
ch-=32;
fputc(ch,ft);
}

fclose(fp);
fclose(ft);
}

Chap12[C]c Add contents of file on another

Write a program to add the contents of one file at the end of another.

#include "stdio.h"

void main()
{
FILE *fp,*ft;
char ch,ch2;
int f=0;
fp=fopen("c:\\ctxt\\12cc1.txt","a");
ft=fopen("c:\\ctxt\\12cc2.txt","r");

fprintf(fp,"\n");
while(1)
{

ch=fgetc(ft);
if(ch==EOF)
break;

fputc(ch,fp);


}
fclose(fp);
fclose(ft);
}

Chap12[C]a Read file and display line numbers

Write a program to read a file and display contents with its line numbers.

#include "stdio.h"
void main()
{
FILE *fp;
char ch;
int c=1;
fp=fopen("c:\\ctxt\\12ca.txt","r");
if(fp==NULL)
{
puts("File cannot be opened");
exit();
}
clrscr();
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
if(ch=='\n')
c++;
printf("%c",ch);
}
printf("\nThe number of lines is %d",c);
}

Wednesday, November 4, 2009

Chap10D[h] Creating link list(inserting from front)

Linked list is a very common data structure often used to store similar data in memory. While the elements of an array occupy contiguous memory locations, those of a linked list are not constrained to be stored in adjacent location. The individual elements are stored “somewhere” in memory, rather like a family dispersed, but still bound together. The order of the elements is maintained by explicit links between them. Thus, a linked list is a collection of elements called nodes, each of which stores two item of information—an element of the list, and a link, i.e., a pointer or an address that indicates explicitly the location of the node containing the successor of this list element. Write a program to build a linked list by adding new nodes at the beginning, at the end or in the middle of the linked list. Also write a function display( ) which display all the nodes present in the linked list.

Reference: http://en.wikipedia.org/wiki/Malloc
http://cslibrary.stanford.edu/103/LinkedListBasics.pdf


#include alloc.h (include arrows)
struct list
{
int x;
struct list *next;
};

void main()
{
struct list *newlist();
struct list *a1=newlist();
struct list *current;
int i,k;
char j[1];


for(i=1,current=a1;current!=NULL;current=current->next,i++)
{
printf("\nThe No. %d in the list is %d",i,current->x);
}


while(1)
{
printf("\nEnter a value to key into the No. %d list",i);
scanf("%d",&k);
push(&a1,k);
printf("\nKey q to quit or any other key to continue keying");
scanf("%s",j);
if(j[0]==113)
break;
}

printf("\nThe new list is as below\n");


for(i=1,current=a1;current!=NULL;current=current->next,i++)
{
printf("\nThe No. %d in the list is %d",i,current->x);
}





}

struct list *newlist()
{
struct list *a1;
struct list *a2;
struct list *a3;

a1=malloc(sizeof(struct list));
a2=malloc(sizeof(struct list));
a3=malloc(sizeof(struct list));

a1->x=1;
a1->next=a2;

a2->x=2;
a2->next=a3;

a3->x=3;
a3->next=NULL;

return(a1);
}

push(struct list **head,int g)
{
struct list *nod=malloc(sizeof(struct list));
nod->x=g;
nod->next=*head;
*head=nod;

}

Manipulating Addresses

DIFFERENTIATING BETWEEN CHANGING ADDRESSES AND CHANGING VALUES
















1)For a pointer to an integer

void main()
{
int a=3;
int *b;
b=&a;
func(b);
printf("The output is %d",b);
}

func(int *b)
{
b=b+1;
}

For the above the answer is 4.

2)For this

void main()
{
int a=1,b=2;
int *aa,*bb;
aa=&a;
bb=&b;
printf("\nAddress at aa is %u, Address at bb is %u",aa,bb);

/*Lets say address at a is 001 and b is 002
We create a function to try and swap these addresses so aa has address b and bb has address a*/

fswap(&aa,&bb);
printf("\nValue at aa is %d, Address at aa is %u,\nValue at bb is %d, Address at bb is %u",*aa,aa,*bb,bb);
}

fswap(int **addressaa,int **addressbb)
{
int **tempcc;
*tempcc=*addressaa;
*addressaa=*addressbb;
*addressbb=*tempcc;
}

Sunday, November 1, 2009

Chap10[D]f Creating a library system

Write a menu driven program that depicts the working of a library. The menu options should be:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit Create a structure called library to hold accession number, title of the book, author name, price of the book, and flag indicating whether book is issued or not.


struct library
{
int num;
char title[20];
char author[20];
float price;
int flag;
};

void main()
{
void accbook(struct library *l,int count);
void addbook(struct library *l,int count);
int count,a,ac;
char key[20],j[1];
struct library l[100]={
{1,"Rabbit","MrR",5.6,1},
{3,"Hamster","MrH",10.1,1},
{2,"Rice","MrR",7.0,0},
{5,"Hamlet","MrH",20.7,0},
{4,"Mouse","MrM",15.3,1}
};
clrscr();

while(1)
{
a=menu();

if(a==1)
{
clrscr();
count=countbook(&l);
addbook(&l,count);
}
if(a==2)
{
clrscr();
displaybook(&l);
}
if(a==3)
{
clrscr();
printf("\nKey in the author's name\n");
scanf("%s",key);
authorbook(&l,key);
}
if(a==4)
{
clrscr();
printf("\nKey in the accession number of the book\n");
scanf("%d",&ac);
specbook2(&l,ac);
}
if(a==5)
{
clrscr();
count=countbook(&l);
printf("\nNumber of books in the library is %d",count);
}
if(a==6)
{
clrscr();
count=countbook(&l);
accbook(&l,count);
displaybook(&l);
}
if(a==7)
exit();
printf("\n\n\nHit q and enter to quit and any other value to go back to menu\n");
scanf("%s",j);
if(j[0]==113)
break;
clrscr();
}
}

menu()
{
int acc;


printf("\n1. Add book information");
printf("\n2. Display book information");
printf("\n3. List all books of given author");
printf("\n4. List the title of specified book (Accession Number)");
printf("\n5. List the count of books in the library");
printf("\n6. Sort the books in order of accession number and display them");
printf("\n7. Exit");
printf("\n\nTo access the menu, key in the corresponding number\n");
while(1)
{
scanf("%d",&acc);
if(acc<1||acc>7)
{
printf("\nYou keyed an invalid value, key again\n");
}
else
break;
}
return(acc);
}

countbook(struct library *l)
{
int i,count=0;

for(i=0;l[i].num!=0;i++)
count++;

return(count);
}

void addbook(struct library *l,int count)
{
char j[1];
int s=0,c=0,i;

while(1)
{
if(count>99)
{
printf("\nLibrary has no more space");
break;
}
s=0;
printf("\nKey in accession number\n");
scanf("%d",&l[count].num);
if(l[count].num==0)
s=1;
for(i=0;i {
if(l[i].num==l[count].num)
{
printf("\nSimilar accession number,Key again\n");

s=1;
}
}
if(s==1)
continue;
printf("\nKey in title (no spacings)\n");
scanf("%s",l[count].title);
printf("\nKey in author (no spacings)\n");
scanf("%s",l[count].author);
printf("\nKey in price\n");
scanf("%f",&l[count].price);

while(1)
{
printf("\nKey 0 if the book is borrowed otherwise key 1 if available\n");
scanf("%d",&l[count].flag);

if(l[count].flag==0||l[count].flag==1)
break;
}

c++;

printf("\nKey any value to continue adding books or key q to quit to menu");
scanf("%s",j);
if(j[0]==113)
break;

clrscr();
count++;
}

if(c==1)
printf("\nYou keyed in 1 new book");

else if (c>1)
printf("\nYou keyed in %d new books",c);
}

displaybook(struct library *l)
{
int i;

for(i=0;l[i].num!=0;i++)
{
printf("\nNo: %d, Title: %s, Author: %s, Price: %f",l[i].num,l[i].title,l[i].author,l[i].price);
if(l[i].flag==1)
printf(", Availability: Yes");

else
printf(", Availability: No");
}
}

specbook(struct library *l)
{
printf("\nNo: %d, Title: %s, Author: %s, Price: %f",l->num,l->title,l->author,l->price);
if(l->flag==1)
printf(", Availability: Yes");
else
printf(", Availability: No");
}

specbook2(struct library *l,int j)
{
int i,f=0;

for(i=0;l[i].num!=0;i++)
{
if(l[i].num==j)
{
specbook(&l[i]);
f=1;
break;
}
}
if(f==0)
printf("\nNo such book with this accession number");
}

authorbook(struct library *l,char *key)
{
int i,j,c,len,f=0;
len=strlen(key);
for(i=0;l[i].num!=0;i++)
{
for(j=0,c=0;l[i].author[j]!=0;j++)
{
if(*(key+j)==l[i].author[j])
c++;
}
if(c==len)
{
specbook(&l[i]);
f=1;
}
}
if(f==0)
printf("\nThere are no books with this author's name");
}

void accbook(struct library *l,int count)
{
int i,j;
struct library a1,a2;
for(i=0;l[i].num!=0;i++)
{
for(j=i+1;j {
if(l[i].num>l[j].num)
{
a1=l[i];
a2=l[j];
l[i]=a2;
l[j]=a1;
}
}
}
}

linkfloat()
{
float a,*b;
b=&a;
a=*b;
}