Giúp em code này với ạ, nhập tên cuốn sách có trong danh sách rồi in ra bằng cách sử dụng linear search or binary search ạ ( d) Print out to screen the book whose title is Y )
Em chỉ còn ngay chỗ cái findbooktitle thôi ạ
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
typedef struct Book{
char title[4];
float price;
int idsach;
} BOOK;
int n,x,d;
//char y;
void inputN (int &n);
void inputBook( BOOK &b);
void inputBookArr( BOOK A[], int n);
void outputBook( BOOK b);
void outputBookArr( BOOK A[], int n);
int findbookid( BOOK A[],int n,int x);
void bookmaxprice(BOOK A[], int n);
void insertionSort(BOOK A[], int d);
int findbooktitle( BOOK A[],int n,char &y);
int main(int argc, char *argv[])
{
int y;
BOOK A[100];
inputN(n);
inputBookArr(A,n);
printf("\nPrint the list of books");
outputBookArr(A,n);
printf("\n");
printf("Input id : ");
scanf("%d",&x);
findbookid(A,n,x);
int result = findbookid(A, n, x);
(result == -1) ? printf("Don't have book\n")
: printf("Location of book %d\n",
result+1);
printf("\n");
printf("Input title : ");
scanf("%d",&y);
//findbooktitle(A,n,y);
//int result11 = findbooktitle(A, n, x);
//(result11 == -1) ? printf("Don't have book\n")
//: printf("Name of book %d\n",result1+1);
bookmaxprice(A,n);
printf("\n");
insertionSort(A,d);
outputBookArr(A,n);
printf("\n");
return 0;
}
void inputN (int &n)
{
printf("Input the number book of list: ");
scanf("%d", &n);
}
void inputBook( BOOK &b)
{
printf("Name: ");
scanf("%s",b.title);
printf("Price: "); scanf("%f", &b.price);
printf("id: "); scanf("%d", &b.idsach);
}
void outputBook(BOOK b)
{
printf("Title : %s , id : %d , gia sach : %f", b.title, b.idsach, b.price);
}
void inputBookArr( BOOK A[], int n)
{
for(int i = 0; i < n; i++)
{
printf("\nBook %d\n", i+1);
inputBook(A[i]);
}
}
void outputBookArr( BOOK A[], int n)
{
for(int i = 0; i < n; i++)
{
printf("\nBook %d: ", i+1);
outputBook(A[i]);
}
}
int findbookid( BOOK A[],int n,int x)
{
int i;
for (i = 0; i < n; i++)
if (A[i].idsach == x)
return i;
return -1;
}
int findbooktitle( BOOK A[],int n,char &y)
{
int i;
for (i = 0; i < n; i++)
if (A[i].title == )
return i;
return -1;
}
void bookmaxprice(BOOK A[],int n)
{
float MAX=A[0].price;
for(int i=1;i<n;i++)
if(A[i].price>MAX)MAX=A[i].price;
printf ("Most expensive: ");
for(int i=0;i<n;i++)
if(A[i].price==MAX)
printf(" %s",A[i].title);
}
void insertionSort(BOOK A[], int d)
{
int i, key, j;
for (j = 1; j < d; j++) {
key = A[j].idsach;
i = j - 1;
while (i >= 0 && A[i].idsach > key) {
A[j + 1].idsach = A[i].idsach;
i = j - 1;
}
A[i + 1].idsach = key;
}
}