Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- struct book{
- char title[100],author[100],subject[100];
- int id;
- };
- void input(struct book s[],int n){
- int i;
- for(i=0;i<n;i++){
- printf("Enter title:");
- gets(s[i].title);
- fflush(stdin);
- printf("Enter authors name:");
- gets(s[i].author);
- fflush(stdin);
- printf("Enter id:");
- scanf("%d",&s[i].id);
- fflush(stdin);
- printf("Enter subject:");
- gets(s[i].subject);
- fflush(stdin);
- printf("\n");
- }
- }
- void view(struct book s[],int n){
- int i;
- printf("Here is the list of books:\n");
- for(i=0;i<n;i++){
- printf("Title:%s\n",s[i].title);
- printf("Author:%s\n",s[i].author);
- printf("Id:%d\n",s[i].id);
- printf("Subject:%s\n",s[i].subject);
- printf("\n");
- }
- }
- void search(struct book s[],int n){
- char find[100];
- int i;
- printf("\nEnter the title of the book to find it:");
- gets(find);
- printf("\n");
- for(i=0;i<n;i++){
- if(strcmp(find,s[i].title)==0){
- printf("Title:%s\n",s[i].title);
- printf("Author:%s\n",s[i].author);
- printf("Id:%d\n",s[i].id);
- printf("Subject:%s\n",s[i].subject);
- }
- else{
- printf("Can't find it");
- }
- }
- }
- void edit(struct book s[],int n){
- char edit[100],ch;
- int i;
- printf("\nEnter the title of the book to edit it:");
- gets(edit);
- printf("\n");
- for(i=0;i<n;i++){
- if(strcmp(edit,s[i].title)==0){
- printf("Enter title:");
- gets(s[i].title);
- fflush(stdin);
- printf("Enter authors name:");
- gets(s[i].author);
- fflush(stdin);
- printf("Enter id:");
- scanf("%d",&s[i].id);
- fflush(stdin);
- printf("Enter subject:");
- gets(s[i].subject);
- fflush(stdin);
- printf("\n");
- }
- }
- printf("Press s/S to see the updated list\n");
- ch=getch();
- if(ch=='s' || ch=='S'){
- for(i=0;i<n;i++){
- printf("Title:%s\n",s[i].title);
- printf("Author:%s\n",s[i].author);
- printf("Id:%d\n",s[i].id);
- printf("Subject:%s\n",s[i].subject);
- printf("\n");
- }
- }
- }
- void delet(struct book s[],int n){
- int del,k,i;
- char ch;
- printf("Enter the Id of the book you want to delete:");
- scanf("%d",&del);
- for(i=0;i<n;i++){
- if(del==s[i].id){
- for(k=i;k<n;k++){
- s[k]=s[k+1];
- }
- n--;
- }
- }
- printf("Press s/S to see the updated list\n");
- ch=getch();
- if(ch=='s' || ch=='S'){
- for(i=0;i<n;i++){
- printf("Title:%s\n",s[i].title);
- printf("Author:%s\n",s[i].author);
- printf("Id:%d\n",s[i].id);
- printf("Subject:%s\n",s[i].subject);
- printf("\n");
- }
- }
- }
- int main(){
- int n;
- char ch;
- printf("How many book you want to add:");
- scanf("%d",&n);
- fflush(stdin);
- struct book s[n];
- input(s,n);
- view(s,n);
- printf("Press 'S\s' for search a book\n");
- printf("Press 'e/E' for edit a book\n");
- printf("Press 'd/D' to delete any book\n");
- ch=getch();
- if(ch=='s' || ch=='S'){
- search(s,n);
- }
- else if(ch=='e' || ch=='E'){
- edit(s,n);
- }
- else if(ch=='d' || ch=='D')
- {
- delet(s,n);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement