Вы находитесь на странице: 1из 5

#include<stdio.

h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
int search;
FILE *tr, *temp;
void add();
void edition();
void deletion();
void transact();
void view();
struct bookrec {
int book_no, no_copy;
char title[20], author[20];
} rec;
int ans,m;
int main()
{
int cho;
int reps;
do {
system("cls");
printf("\n What would you like to do ?");
printf("\n1.) Add record\n2.) Edit record\n3.) Delete record\n4.
) Make Transaction\n5.) View record\n6.) Exit\n");
scanf("%d", &cho);
switch(cho) {
case 1: {
add();
break;
}
case 2: {
edition();
break;
}
case 3: {
deletion();
break;
}
case 4: {
transact();
break;
}
case 5: {
view();
break;
}
case 6: {
return 0;
break;
}

default: {
printf("Error!");
}
}
printf("\nBack to Menu? [1 - YES 0 - NO]\n");
scanf("%d",&reps);
}
while(reps==1);
getch();
return 0;
}
void add() {
tr=fopen("book.txt","a");
do {
system("cls");
printf("Book number : ");
scanf("%d", &rec.book_no);
printf("Book title : ");
scanf("%s", &rec.title);
printf("Book author: ");
scanf("%s", &rec.author);
printf("Number of book copies: ");
scanf("%d", &rec.no_copy);
fprintf(tr, "\n%d\t%s\t%s\t%d\n", rec.book_no, rec.title, rec.
author, rec.no_copy);
printf("\nMake another record? [1/0] ");
scanf("%d",&ans);
}
while(ans==1);
fclose(tr);
}
void edition() {
int d=0,m;
do {system("cls");
tr=fopen("book.txt", "r");
temp=fopen("book1.txt", "w");
printf("Book number: ");
scanf("%d", &search);
while(fscanf(tr,"%d%s%s%d",&rec.book_no,&rec.title,&rec.author,&
rec.no_copy)!=EOF)
{ if(rec.book_no==search)
{ printf("Enter new record: ");
printf("\nBook number: ");
scanf("%d",&rec.book_no);
printf("Book title : ");
scanf("%s", &rec.title);
printf("Book author: ");
scanf("%s", &rec.author);
printf("Number of book copies: ");
scanf("%d", &rec.no_copy);
m=1;
}
fprintf(temp,"\n%d\t%s\t%s\t%d\n", rec.book_no,rec.title,rec.author
,rec.no_copy);}
if(m==0)
printf("Record not found!");

printf("\nWould you like to edit another record ? \n\t1 - Yes 0 - No\


n");
scanf("%d",&d);
} while(d!=0);
getch();
fclose(tr);
remove("book.txt");
fclose(temp);
rename("book1.txt","book.txt");
tr = fopen("book.txt","r");
printf("Book No\tBook Title\tAuthor\tNo. Of Copies\n");
while(fscanf(tr,"%d%s%s%d", &rec.book_no, &rec.title, &rec.author, &rec.no_
copy)!=EOF )
{ printf("%d\t%s\t%s\t%d\n",rec.book_no, rec.title, rec.author, rec.no_copy
); }
fclose(tr);
}
void deletion()
{
int i=0, d;
do
{
system("cls");
tr=fopen("book.txt","r");
temp=fopen("book1.txt","w");
m=0;
printf("Book no. that you want to delete: ");
scanf("%d",&search);
while(fscanf(tr,"%d%s%s%d", &rec.book_no, &rec.title, &rec.author, &rec.
no_copy)!=EOF)
{ if(rec.book_no==search)
{
m=1;}
else
{fprintf(temp,"%d\t%s\t%s\t%d\n", rec.book_no, rec.title, rec.author, rec.
no_copy);}}
if(m==0)
printf("Record not found!");
printf("\nWould you like to delete another record ? \n\t1 - Yes 0 - No\n
");
scanf("%d",&d);
fclose(tr);
remove("book.txt");
fclose(temp);
rename("book1.txt","book.txt");
} while(d!=0);
getch();
tr = fopen("book.txt","r");
printf("Book No\tBook Title\tAuthor\tNo. Of Copies\n\n\n");
while(fscanf(tr,"%d%s%s%d", &rec.book_no, &rec.title, &rec.author, &rec.no_copy
)!=EOF )
{ printf("%d\t%s\t%s\t%d\n",rec.book_no, rec.title, rec.author, rec.no_copy); }
fclose(tr);
getch();

}
void transact()
{
int r,b,ret,bor;
int trans,ans;
system("cls");
tr=fopen("book.txt","r");
printf("Transaction: Borrow or Return? [1/2] ");
scanf("%s",&trans);
printf("\nBook number : ");
scanf("%d", &rec.book_no);
printf("\nBook title : ");
scanf("%s", &rec.title);
printf("\nBook author: ");
scanf("%s", &rec.author);
do
{
if(toupper(trans)==1)
{
system("cls");
tr=fopen("book.txt","r");
printf("How many books you want to return? ");
scanf("%d",&r);
fscanf(tr,"%d%s%s%d", &rec.book_no, &rec.title, &rec.author, &rec.no_cop
y);
ret=r+rec.no_copy;
rec.no_copy=ret;
fprintf(tr,"%d%s%s%d", rec.book_no, rec.title, rec.author, rec.no_copy);
fclose(tr);
}
else if(toupper(trans)!=2)
{
system("cls");
tr=fopen("book.txt","r");
printf("How many books you want to borrow? ");
scanf("%d",&b);
fscanf(tr,"%d%s%s%d", &rec.book_no, &rec.title, &rec.author, &rec.no_copy);
bor=b+rec.no_copy;
rec.no_copy=bor;
fprintf(tr,"%d%s%s%d", rec.book_no, rec.title, rec.author, rec.no_copy);
fclose(tr);
}
printf("\nTransaction done!");
printf("\n\tAnother transaction? [1/0] :");
}while(toupper(ans)==1);
getch();
fclose(tr);
}
void view()
{
{system("cls");
tr=fopen("book.txt","r");
printf("Book No\tBook Title\tAuthor\tNo. Of Copies\n");
while(fscanf(tr, "%d%s%s%d", &rec.book_no, &rec.title, &rec.author, &rec.no_cop
y)!=EOF )
{ printf("%d\t%s\t%s\t%d\n",rec.book_no, rec.title, rec.author, rec.no_copy);

}
fclose(tr);
}
}

Вам также может понравиться