---
# System prepended metadata

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

---

Quiz 04
===
# [A]
[Quiz 03 [C] SubNetwork](https://hackmd.io/@nnyjan02426/cp2_quiz03#C-Subnetwork)

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

FILE *rPtr, *wPtr;

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

struct pricing{
    char name[100];
    int price;
};

struct cat{
    char *input_type[5];
    int price;
};

int has_num(char *str);
int has_comma(char *str);
int str_to_int(char *str);
int in_string(char *str, char *find);

int main(){
    char input[150];
    enum input_type input_type = BREED;
    struct pricing pricing[5][50];
    int pricing_idx[5] = {0};

    rPtr = fopen("test_input_Pets.txt", "r");
    while(input_type <= COAT){
        fgets(input, 150, rPtr);
        input[strlen(input)-1] = '\0';
        if(!has_num(input)){
            input_type++;
        }
        else{
            strcpy(pricing[input_type][pricing_idx[input_type]].name, strtok(input, ","));
            pricing[input_type][pricing_idx[input_type]].price = str_to_int(strtok(NULL, ","));
            //printf("%d ", pricing[input_type][pricing_idx[input_type]].price);
            pricing_idx[input_type]++;
        }
    }

    wPtr = fopen("my_output_Pets.txt", "w+");
    fprintf(wPtr, "====================================================================================\n");
    fprintf(wPtr, "Breed                    Origin                Type    Body        Coat        Price\n");
    fprintf(wPtr, "------------------------------------------------------------------------------------\n");

    struct cat cat;
    char tmp_type[20];
    while(!feof(rPtr)){
        fgets(input, 150, rPtr);

        if(has_comma(input)){
            cat.price = 0;
            cat.input_type[BREED] = strtok(input, ",");
            for(enum input_type input_type=ORIGIN; input_type<=COAT; input_type++){
                cat.input_type[input_type] = strtok(NULL, ",");
                for(int i=0; i<pricing_idx[input_type]; i++){
                    if(in_string(cat.input_type[input_type], pricing[input_type][i].name)){
                        //fprintf(wPtr, "%d ", pricing[input_type][i].price);
                        cat.price += pricing[input_type][i].price;
                        break;
                    }
                }
            }
            cat.input_type[COAT][strlen(cat.input_type[COAT])-1] = '\0';
            fprintf(wPtr, "%-25s%-22s  %-6c%-12s%-12s%5d\n", cat.input_type[BREED], cat.input_type[ORIGIN], cat.input_type[TYPE][0], cat.input_type[BODY], cat.input_type[COAT], cat.price);
        }
    }
    fprintf(wPtr, "====================================================================================\n");
    fclose(rPtr);
    fclose(wPtr);
}

int has_num(char *str){
    for(int i=0; i<strlen(str); i++){
        if((str[i] == ',') && ((str[i+1] >= '0') && (str[i+1] <= '9'))){
            return 1;
        }
    }
    return 0;
}

int str_to_int(char *str){
    int result = 0;
    for(int i=0; i<strlen(str); i++){
        if((str[i] > '9') || (str[i] < '0')){
            return result;
        }
        result *= 10;
        result += (int)str[i] - (int)'0';
    }
    return result;
}

int has_comma(char *str){
    for(int i=0; i<strlen(str); i++){
        if(str[i] == ','){
            return 1;
        }
    }
    return 0;
}

int in_string(char *str, char *find){
    int found;
    for(int i=0; i<strlen(str); i++){
        if(str[i] == find[0]){
            found = 1;
            for(int j=1; j<strlen(find); j++){
                if(str[i+j] != find[j]){
                    found = 0;
                    break;
                }
            }
            if(found){
                return 1;
            }
        }
    }

    return 0;
}

```

# [C] PetsTransaction
## 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;

void sort(pet pets[]);
int compare(pet *pet1, pet *pet2);

int main(){
    FILE *rPtr = fopen("test_input_PetsTransaction.txt", "rb");
    pet pets[500];
    fread(pets, sizeof(pet), 500, rPtr);
    fclose(rPtr);

    sort(pets);
    FILE *wPtr = fopen("my_output_PetsTransaction.txt", "wb+");
    fwrite(pets, sizeof(pet), 500, wPtr);
    fclose(wPtr);
}

void sort(pet pets[]){
    int largest_idx;
    pet tmp;
    for(int i=0; i<499; i++){
        largest_idx = i;
        for(int j=i+1; j<500; j++){
            if(compare(&pets[largest_idx], &pets[j])){
                largest_idx = j;
            }
        }
        if(largest_idx != i){
            tmp = pets[largest_idx];
            pets[largest_idx] = pets[i];
            pets[i] = tmp;
        }
    }
}

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];
        }
    }
}
```
###### tags: `程設二quiz`