#include
#define MAX 1000 // Do not edit this macro.
typedef struct // Do not edit this struct.
{
unsigned long restaurant_id;
char restaurant_name[15];
char description[127];
double rate;
char cuisine[31];
unsigned short opening_year;
unsigned long capacity;
char city[31];
char address[63];
char owner[31];
} RECORD_t, *RECORD;
/* DECLARE YOUR FUNCTION PROTOTYPES HERE. */
void find_by_opening_year(unsigned short opening_year, RECORD *restaurant_array, unsigned long size, unsigned long start, unsigned long end, unsigned long *p_found_num, RECORD **p_found_restaurants);
RECORD find_by_restaurant_name(char *restaurant_name, RECORD *restaurant_array, unsigned long size, unsigned long start, unsigned long end);
unsigned long insert(RECORD restaurant, RECORD *restaurant_array, unsigned long *p_size);
RECORD delete (unsigned long restaurant_id, RECORD *restaurant_array, unsigned long *p_size);
void sort(RECORD restaurant_array, unsigned long size, double rate, char *restaurant_name);
int main()
{
/* --- DO NOT EDIT THIS PART. --- */
unsigned long num_records = MAX;
unsigned long i;
RECORD tmp;
RECORD *rec = (RECORD *)malloc(sizeof(RECORD) * num_records);
FILE *file = fopen("restdata.txt", "rb");
if (file == NULL)
{
printf("Cannot open the file.\n");
exit(0);
}
for (i = 0; i < num_records;="">
{
tmp = (RECORD)malloc(sizeof(RECORD_t));
fread(tmp, sizeof(RECORD_t), 1, file);
rec[i] = tmp;
}
fclose(file);
/* --- DO NOT EDIT THIS PART. --- */
/**************************/
/* Your code starts here. */
/**************************/
/*
Call the functions that you implemented,
You can create a menu to demonstrate your functions.
*/
/**************************/
/* Your code ends here. */
/**************************/
/* --- DO NOT EDIT THIS PART. --- */
file = fopen("data_out.txt", "wb");
for (i = 0; i < num_records;="">
{
tmp = rec[i];
fwrite(tmp, sizeof(RECORD_t), 1, file);
free(tmp);
}
fclose(file);
free(rec);
return 1;
/* --- DO NOT EDIT THIS PART. --- */
}
/* IMPLEMENT YOUR FUNCTIONS HERE */
void find_by_opening_year(unsigned short opening_year, RECORD *restaurant_array, unsigned long size, unsigned long start, unsigned long end, unsigned long *p_found_num, RECORD **p_found_restaurants)
{
/*
num <- find="" the="" number="" of="" records="" with="" given="" release_year="" between="" start="" and="" end="" in="">->
restaurants <->->
if num != 0 then
restaurants <- allocate="" an="" array="" of="" records="" of="" size="">->
put the RECORDs with given opening_year between start and end in restaurant_array into restaurants
endif
*p_found_num <- num="" return="" the="" number="" of="" records="" via="" pointer="">->
*p_found_restaurants <- restaurants="" return="" the="" array="" of="" found="" records="" via="" pointer="">->
*/
}
RECORD find_by_restaurant_name(char *restaurant_name, RECORD *restaurant_array, unsigned long size, unsigned long start, unsigned long end)
{
/*
p_found <->->
p_found <- find="" the="" record="" with="" given="" restaurant_name="" in="" restaurant_array="" between="" start="" and="" end="" (p_found="" does="" not="" change="" if="" no="" such="" record="" is="">->
return p_found
*/
}
unsigned long insert(RECORD restaurant, RECORD *restaurant_array, unsigned long *p_size)
{
/*
index <- find="" the="" first="" location="" which="" contains="" null="" in="">->
if index is valid then
*p_restaurant_array[index] <->->
else
new_restaurants_array <- extends="" *p_restaurant_array="" by="" 1="" to="" insert="" to="" the="">->
if extension is successful then
index <->->
new_restaurants_array[index] <->->
*p_restaurant_array <- new_restaurants_array="" return="" the="" new="" array="" via="" pointer="">->
*p_size <- *p_size="" +="" 1="" return="" the="" new="" size="" via="" pointer="">->
else
index <->->
endif
endif
return index
*/
}
RECORD delete (unsigned long restaurant_id, RECORD *restaurant_array, unsigned long *p_size)
{
/*
index <- find="" the="" index="" of="" the="" record="" with="" given="" restaurant_id="" in="">->
if index is valid then
rec <->->
else
rec <->->
endif
return rec
*/
}
void sort(RECORD restaurant_array, unsigned long size, double rate, char *restaurant_name)
{
// This sort function must sort by rate. If rates are the same, the function must
// sort by restaurant_name alphabetically.
/*
x <- end="" +="">->
while x != start do
high <->->
for i <- start,="" i="">-><= x="" -="" 2,="" i="">=><- i="" +="" 1="">->
if compare(restaurant_array[i], restaurant_array[i + 1]) > 0 then
swap(&restaurant_array[i], &restaurant_array[i + 1])
high <- i="" +="">->
endif
endfor
x <->->
endwhile
*/
}