0% found this document useful (0 votes)
29 views9 pages

Test

Uploaded by

tongvythanh05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views9 pages

Test

Uploaded by

tongvythanh05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

/* Includes ------------------------------------------------------------------*/

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Define --------------------------------------------------------------------*/
#define MAX_ROW 2
#define MAX_COLUMN 15
#define MAX_ENTERPRISE 30

/* Enum ----------------------------------------------------------------------*/
typedef enum Status { Empty, Registered } Status;

typedef enum CommandType {


REGISTER,
ALTER,
SHOW,
DELETE,
QUIT,
INVALID
} CommandType;
char output[10000];
int len = 0;
/* ---------------------------------------------------------------------------*/
// Requirement 1: Generate abbreviation from the full name of an enterprise
void getAbbreviation(char *name, char *abbre) {
// TODO: Implement this function
int index = 0;
bool newWord = true;

for (int i = 0; name[i] != '\0'; i++) {


if (name[i] == ' ') {
newWord = true;
} else if (newWord) {
abbre[index++] = name[i];
newWord = false;
}
}
abbre[index] = '\0';

// Requirement 2: Determine the type of command


CommandType getCommandType(char *command) {
// TODO: Implement this function
char firstWord[50];
sscanf(command, "%s", firstWord);

if (strcmp(firstWord, "Register") == 0) {
return REGISTER;
} else if (strcmp(firstWord, "Alter") == 0) {
return ALTER;
} else if (strcmp(firstWord, "Show") == 0) {
return SHOW;
} else if (strcmp(firstWord, "Delete") == 0) {
return DELETE;
} else if (strcmp(firstWord, "Quit") == 0) {
return QUIT;
}
return INVALID;
}

// Requirement 3: Define the structure to store enterprise details


typedef struct
{
// TODO: Implement this function
int booth_index;
char name[200];
char abbre[20];
int itemValue;
int itemWeight;
} Enterprise;

// Requirement 4: Print the details of a specific enterprise


void printEnterpriseDetails(Enterprise e)
{
// TODO: Implement this function
printf("||%d||%s||%s||%d||%d||\n", e.booth_index, e.name, e.abbre, e.itemValue,
e.itemWeight);
}

void createEnterprise(Enterprise *e, int booth_index, int itemValue, int


itemWeight, char *name, char *abbre)
{
// TODO: Implement this function
if (e == NULL) {
e = (Enterprise *)malloc(sizeof(Enterprise));
}
e->booth_index = booth_index;
e->itemValue = itemValue;
e->itemWeight = itemWeight;
strncpy(e->name, name, sizeof(e->name) - 1);
e->name[sizeof(e->name) - 1] = '\0';
strncpy(e->abbre, abbre, sizeof(e->abbre) - 1);
e->abbre[sizeof(e->abbre) - 1] = '\0';
}
void registerEnterprise(int map[MAX_ROW][MAX_COLUMN], Enterprise
enterpriseArray[MAX_ENTERPRISE],
char *name, int booth,int itemValue,int itemWeight ,int
*out_booth, char *out_abbre) {

getAbbreviation(name, out_abbre);
int num_abbre = strlen(out_abbre);

int booth_index;

if (booth == -1) {

booth_index = (num_abbre * 30) % 26;


int found = 0;
for (int i = booth_index; i < 30; i++) {
if(i<15) {
if (map[0][i]==0){
booth_index = i;
found = 1;
break;
}
} else {
if(map[1][i-15]==0) {
booth_index = i;
found = 1;
break;
}

}
}
if (!found) {
for (int i = booth_index; i >= 0; i--) {
if(i<15) {
if (map[0][i]==0){
booth_index = i;
found = 1;
break;
}
} else {
if(map[1][i-15]==0) {
booth_index = i;
found = 1;
break;
}

}
}
if (!found) {
*out_booth = booth_index + 100;
return;

}
}
} else {
booth_index = booth;
}

if (booth_index<15) {
if(map[0][booth_index]==0) {
map[0][booth_index]=1;

for (int i = 0; i < MAX_ENTERPRISE; i++) {


if (enterpriseArray[i].booth_index == -1) {
enterpriseArray[i].booth_index = booth_index;
strncpy(enterpriseArray[i].name, name,
sizeof(enterpriseArray[i].name) - 1);
enterpriseArray[i].name[sizeof(enterpriseArray[i].name) - 1] = '\
0';
strncpy(enterpriseArray[i].abbre, out_abbre,
sizeof(enterpriseArray[i].abbre) - 1);
enterpriseArray[i].abbre[sizeof(enterpriseArray[i].abbre) - 1] = '\
0';
enterpriseArray[i].itemValue=itemValue;
enterpriseArray[i].itemWeight=itemWeight;
break;
}
}
*out_booth = booth_index + 200;

else {
*out_booth = booth_index + 100;
}

} else if(booth_index>=15){

if(map[1][booth_index-15]==0) {
map[1][booth_index-15]=1;

for (int i = 0; i < MAX_ENTERPRISE; i++) {


if (enterpriseArray[i].booth_index == -1) {
enterpriseArray[i].booth_index = booth_index;
strncpy(enterpriseArray[i].name, name,
sizeof(enterpriseArray[i].name) - 1);
enterpriseArray[i].name[sizeof(enterpriseArray[i].name) - 1] = '\
0';
strncpy(enterpriseArray[i].abbre, out_abbre,
sizeof(enterpriseArray[i].abbre) - 1);
enterpriseArray[i].abbre[sizeof(enterpriseArray[i].abbre) - 1] = '\
0';
enterpriseArray[i].itemValue=itemValue;
enterpriseArray[i].itemWeight=itemWeight;
break;
}
}

*out_booth = booth_index + 200;

else {
*out_booth = booth_index + 100;
}
}

}
void showMap(int map[MAX_ROW][MAX_COLUMN], Enterprise
enterpriseArray[MAX_ENTERPRISE]) {
printf("|");
for (int j = 0; j < MAX_COLUMN; j++) {
printf("%d|", j);
}
printf("\n");

for (int j = 0; j < MAX_COLUMN; j++) {


printf("--");
}
printf("\n");

for (int i = 0; i < MAX_ROW; i++) {


printf("|");
for (int j = 0; j < MAX_COLUMN; j++) {
printf("%d|", map[i][j]);
}
printf("\n");
}
}
void showIndexOfStatus(int map[MAX_ROW][MAX_COLUMN], Status status)
{
printf("Index[");
int first = 1; // Dùng để kiểm tra xem có cần in dấu phẩy hay không
for (int i = 0; i < MAX_ROW; i++) {
for (int j = 0; j < MAX_COLUMN; j++) {
if (map[i][j] == status) {
if (!first) {
printf(", ");
}
printf("index_%d", i * MAX_COLUMN + j); // In định dạng index_x
first = 0; // Đã in một phần tử
}
}
}
printf("]\n");
}
void showTotalOfStatus(Enterprise enterpriseArray[MAX_ENTERPRISE], Status status)
{

int count = 0;
for (int i = 0; i < MAX_ENTERPRISE; i++) {
if (status == Empty && enterpriseArray[i].booth_index == -1) {
count++;

} else if (status == Registered && enterpriseArray[i].booth_index != -1) {

count++;
}
}

printf("Total: %d\n", count);


}
void showIndexBracket(int map[MAX_ROW][MAX_COLUMN], Enterprise
enterpriseArray[MAX_ENTERPRISE], int start, int end)
{
if (end == -1) {
end = start;
}
if (start==end) {

for (int i = 0; i < 30; i++) {

if (enterpriseArray[i].booth_index==start){
printEnterpriseDetails(enterpriseArray[i]);
return;
}

}
printf("NONE\n");
return;
}
for (int i=start;i<=end;i++) {
int row=(i<15)?0:1;
int col=(i<15)?i:(i-15);
if (map[row][col]==0) {
printf("NONE\n");

}
else{
for (int j=0;j<30;j++) {
if (enterpriseArray[j].booth_index==i) {

printEnterpriseDetails(enterpriseArray[j]);
}
}
}
}

int isNumber(char *c) {


for (int i = 0; c[i] != '\0'; i++) {
if (c[i] < '0' || c[i] > '9') {
return 0;
}
}
return 1;
}

void handleShowCommand(int map[MAX_ROW][MAX_COLUMN], Enterprise


enterpriseArray[MAX_ENTERPRISE], char *command) {
char *cmd = command + 4;

while (*cmd == ' ') {


cmd++;
}

if (strcmp(command, "Show &0") == 0) {


showIndexOfStatus(map, Empty);
} else if (strcmp(command, "Show &1") == 0) {
showIndexOfStatus(map, Registered);
} else if (strcmp(command, "Show map") == 0) {

showMap(map, enterpriseArray);
} else if (strcmp(command, "Show #0") == 0) {

showTotalOfStatus(enterpriseArray,Empty );
} else if (strcmp(command, "Show #1") == 0) {
showTotalOfStatus(enterpriseArray, Registered);
} else if (cmd[0] == '[' && cmd[strlen(cmd) - 1] == ']' &&(strlen(cmd)==3||
strlen(cmd)==4 )) {

char num_str[10];
strncpy(num_str, cmd + 1, strlen(cmd) - 2);
num_str[strlen(cmd) - 2] = '\0';

int num = atoi(num_str);

showIndexBracket(map, enterpriseArray, num, num);


}
else if (cmd[0] == '[' && strchr(cmd, ':') != NULL && cmd[strlen(cmd) - 1] ==
']') {

char *colon_pos = strchr(cmd, ':');


char from_str[12], to_str[12];

strncpy(from_str, cmd + 1, colon_pos - cmd - 1);


from_str[colon_pos - cmd - 1] = '\0';

strncpy(to_str, colon_pos + 1, strlen(colon_pos) - 2);


to_str[strlen(to_str)] = '\0';

int from_index = atoi(from_str);


int to_index = atoi(to_str);

showIndexBracket(map, enterpriseArray, from_index, to_index);


}else{

}
}
void initMap(int map[MAX_ROW][MAX_COLUMN])
{
for (int i = 0; i < MAX_ROW; i++)
{
for (int j = 0; j < MAX_COLUMN; j++)
{
map[i][j] = Empty;
}
}
}

// Initialize the enterprise array with default values


void initEnterpriseArray(Enterprise enterpriseArray[MAX_ENTERPRISE])
{
for (int i = 0; i < MAX_ENTERPRISE; i++)
{
enterpriseArray[i].booth_index = -1;
strcpy(enterpriseArray[i].name, "");
strcpy(enterpriseArray[i].abbre, "");
enterpriseArray[i].itemValue = 0;
enterpriseArray[i].itemWeight = 0;
}
}
int main(){

// Initialize map and enterpriseArray


int map[MAX_ROW][MAX_COLUMN]; // All booths are empty initially
initMap(map);
Enterprise enterpriseArray[MAX_ENTERPRISE] ;
initEnterpriseArray(enterpriseArray);
// Add sample enterprises
int out_booth;
char out[10];
registerEnterprise(map,enterpriseArray,"D",2,5,5,&out_booth,out );
registerEnterprise(map,enterpriseArray,"Hihi haha",4,5,5,&out_booth,out );
// registerEnterprise(map,enterpriseArray,"Hihi haha a",6,5,5,&out_booth,out
);

// Mark booths as registered

// Testcase 1: Show index of empty booths


printf("Testcase 1: Show &0\n");
handleShowCommand(map, enterpriseArray, "Show &0");

// Testcase 2: Show index of registered booths


printf("\nTestcase 2: Show &1\n");
handleShowCommand(map, enterpriseArray, "Show &1");

// Testcase 3: Show map


printf("\nTestcase 3: Show map\n");
handleShowCommand(map, enterpriseArray, "Show map");

// Testcase 4: Show total empty booths


printf("\nTestcase 4: Show #0\n");
handleShowCommand(map, enterpriseArray, "Show #0");

// Testcase 5: Show total registered booths


printf("\nTestcase 5: Show #1\n");
handleShowCommand(map, enterpriseArray, "Show #1");

// Testcase 6: Show a single booth index


printf("\nTestcase 6: Show [5]\n");
handleShowCommand(map, enterpriseArray, "Show [5]");

// Testcase 7: Show booth range


printf("\nTestcase 7: Show [2:10]\n");
handleShowCommand(map, enterpriseArray, "Show [2:10]");

// Testcase 8: Show invalid booth range


printf("\nTestcase 8: Show [15:20]\n");
handleShowCommand(map, enterpriseArray, "Show [15:20]");
return 0;

You might also like