One C program for implementing structure, file manipulation, switch case and functions
Write a C program for the following:
1.
Define a structure to store the details about a book, having members
like
bood_id,
title of book, subject of the book, publisher, edition and price.
2.
Input a number which works as the size of the array. Create a dynamic
array
of the book structure.
3.
Define the following functions:
1.
getData(&bookVariable);
//
Function which takes input of all the members of the book
structure
variable passed a parameter
2.
showData(&bookVariable);
//
Function which show the details stored in the member variables of
book structure variable passed as parameter
3.
totalCost = calculateTotalCost ( booksArray, arraySize ) ;
//
Function which takes array of books and the size of the book
array
as parameter and returns the total of price of all books
4.
storeToFile(filename, bookArray, arraySize);
//
Function which stores the books from the book array to the file
specified
with the file name parameter to the function
5.
Write the main function to test above mentioned functions using a
menu. */
#include"stdio.h"
#include"stdlib.h"
struct
book
{
int
book_id, edit;
int
price;
char
title[100], subj[100], pub[100];
};
int
showData(struct book *);
int
getData(struct book *);
int
calculateTotalCost(struct book *, int);
int
storeData(FILE *, struct book *, int);
int
main()
{
int
num, x, *temp, opt;
//int
temp[500];
FILE
*fp;
printf("\nEnter
the size of array: ");
scanf("%d",
&num);
struct
book b[num];
//struct
book b;
do
{
printf("\n\n1.
Get Data\n");
printf("2.
Show Data \n");
printf("3.
Calculate Total Cost \n");
printf("4.
Store Data \n");
printf("5.
Exit");
printf("\n\n
Please enter your choice:");
scanf("%d",
&opt);
switch(opt)
{
case
1:
for(x=0;
x<num; x++)
{
getData(&b[x]);
}
break;
case
2:
for(x=0;
x<num; x++)
{
showData(&b[x]);
}
break;
case
3:
calculateTotalCost(&b,
num);
break;
case
4:
storeData(&fp,
&b, num);
break;
case
5:
exit(0);
break;
default:
printf("Please
anter any choice: ");
}
}
while(opt
!= 5);
return
0;
}
int
getData(struct book *p)
{
printf("Enter
book_id: ");
scanf("%d",
&p->book_id);
printf("Enter
book edition: ");
scanf("%d",
&p->edit);
printf("Enter
book price: ");
scanf("%d",
&p->price);
printf("Enter
book title: ");
scanf("%s",
p->title);
printf("Enter
book subject: ");
scanf("%s",
p->subj);
printf("Enter
publisher name: ");
scanf("%s",
p->pub);
return
0;
}
int
showData(struct book *z)
{
printf("\nBook
id : %d", z->book_id);
printf("\nBook
Edition: %d", z->edit);
printf("\nBook
Price: %d", z->price);
printf("\nBook
Title: %s", z->title);
printf("\nSubject:
%s", z->subj);
printf("\nPublisher
name: %s", z->pub);
return
0;
}
int
calculateTotalCost(struct book *c, int num)
{
int
total = 0, x;
for(x=0;
x<num; x++)
{
total
= total + c[x].price;
}
printf("Total
cost is: %d\n", total);
return
0;
}
int
storeData(FILE *fp, struct book *a, int num)
{
int
x;
fp
= fopen("program1.txt", "a");
for(x=0;
x<num; x++)
{
printf("\n");
fprintf(fp,
"Book id: %d \n Book Edition: %d \n Book price: %d \n Book
title: %s \n Subject: %s \n Publisher name: %s\n", a[x].book_id,
a[x].edit, a[x].price, a[x].title, a[x].subj, a[x].pub);
}
fclose(fp);
printf("\nData
stored successfully.\n");
return
0;
}
Output:
Comments
Post a Comment