---
# System prepended metadata

title: Quiz 05
tags: [程設二quiz]

---

Quiz 05
===
# [A] PetsTransaction
[Quiz 04 [C] PetsTransaction](https://hackmd.io/@nnyjan02426/cp2_quiz04#C-PetsTransaction)

# [B] PetStack
## Code
```c=
#include <stdio.h>
#include <stdlib.h>

enum data_type{
    BREED, ORIGIN, TYPE, BODY, COAT
};

typedef struct{
    char data_type[5][256];
    unsigned int price;
}pet;

typedef struct stack{
    pet *pet_ptr;
    struct stack *below;
}stack;

stack* push(stack *top, pet *pet_ptr);
stack* pop(stack *top);

int main(){
    FILE *rPtr = fopen("test_input_PetStack.txt", "rb");
    pet Pets[500];
    stack *top = NULL;

    for(int i=0; i<500; i++){
        fread(&Pets[i], sizeof(pet), 1, rPtr);
        top = push(top, &Pets[i]);
    }
    fclose(rPtr);

    FILE *wPtr = fopen("my_output_PetStack.txt", "w+");
    fprintf(wPtr, "====================================================================================\n");
    fprintf(wPtr, "Breed                    Origin                Type    Body        Coat        Price\n");
    fprintf(wPtr, "------------------------------------------------------------------------------------\n");
    for(int i=0; i<500; i++){
        fprintf(wPtr, "%-25s%-22s  %-6c%-12s%-12s%5u\n", top->pet_ptr->data_type[BREED], top->pet_ptr->data_type[ORIGIN], top->pet_ptr->data_type[TYPE][0], top->pet_ptr->data_type[BODY], top->pet_ptr->data_type[COAT], top->pet_ptr->price);
        //printf("%-25s%-22s  %-6c%-12s%-12s%5u\n", top->pet_ptr->data_type[BREED], top->pet_ptr->data_type[ORIGIN], top->pet_ptr->data_type[TYPE][0], top->pet_ptr->data_type[BODY], top->pet_ptr->data_type[COAT], top->pet_ptr->price);
        top = pop(top);
    }
    fprintf(wPtr, "====================================================================================\n");
}

stack* push(stack *top, pet *pet_ptr){
    stack *new_stack = malloc(sizeof(stack));
    new_stack->pet_ptr = pet_ptr;

    new_stack->below = top;
    return new_stack;
}
stack* pop(stack *top){
    stack *new_top = top->below;
    free(top);
    return new_top;
}
```

# [C] PetList
>Note: 
>此題目要求要寫出給Linked List 的函式delete，此題不會用到，就不另外寫出來了。
>老實說，我也不是很確定traverse是什麼，所以就隨便寫一寫了，反正if it works, it works!
## Code
```c=
#include <stdio.h>
#include <stdlib.h>

enum data_type{
    BREED, ORIGIN, TYPE, BODY, COAT
};

typedef struct{
    char data_type[5][256];
    unsigned int price;
}pet;

typedef struct node{
    pet *pet_ptr;
    struct node* next_node;
}Node;

Node* add(Node* start, pet *pet_ptr);
int compare(pet *pet1, pet *pet2);

int main(){
    FILE *rPtr = fopen("test_input_PetList.txt", "rb");
    pet pets[500];
    Node* start = NULL;
    for(int i=0; i<500; i++){
        fread(&pets[i], sizeof(pet), 1, rPtr);
        start = add(start, &pets[i]);
    }
    fclose(rPtr);

    FILE *wPtr = fopen("my_output_PetList.txt", "wb+");
    fprintf(wPtr, "====================================================================================\n");
    fprintf(wPtr, "Breed                    Origin                Type    Body        Coat        Price\n");
    fprintf(wPtr, "------------------------------------------------------------------------------------\n");    
    for(int i=0; i<500; i++){
        fprintf(wPtr, "%-25s%-22s  %-6c%-12s%-12s%5u\n", start->pet_ptr->data_type[BREED], start->pet_ptr->data_type[ORIGIN], start->pet_ptr->data_type[TYPE][0], start->pet_ptr->data_type[BODY], start->pet_ptr->data_type[COAT], start->pet_ptr->price);
        start = start->next_node;
    }
    fprintf(wPtr, "====================================================================================\n");
    fclose(wPtr);
}

int compare(pet *pet1, pet *pet2){
    for(enum data_type data_type=ORIGIN; data_type<=COAT; data_type++){
        for(int i=0; i<256; i++){
            if(pet1->data_type[data_type][i] != pet2->data_type[data_type][i]){
                return pet1->data_type[data_type][i] > pet2->data_type[data_type][i];
            }
        }
    }

    for(int i=0; i<256; i++){
        if(pet1->data_type[BREED][i] != pet2->data_type[BREED][i]){
            return pet1->data_type[BREED][i] > pet2->data_type[BREED][i];
        }
    }
}

Node* add(Node* start, pet *pet_ptr){
    Node *new_node = malloc(sizeof(Node));
    new_node->pet_ptr = pet_ptr;

    Node *prev = NULL;
    Node *curr = start;
    while((curr != NULL) && (compare(pet_ptr, curr->pet_ptr))){
        prev = curr;
        curr = curr->next_node;
    }

    if(prev == NULL){
        new_node->next_node = curr;
        return new_node;
    }
    prev->next_node = new_node;
    new_node->next_node = curr;
    return start;
}
```

###### tags: `程設二quiz`