---
# System prepended metadata

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

---

Quiz 03
===
# [A] Tablelize
[Quiz02 [C] Tablelize](https://hackmd.io/@nnyjan02426/cp2_quiz02#C-Tablelize)

# [B] Pets
## Notes
## Code
```c=1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 25

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

struct price{
    char name[50];
    int price;
};

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

int str_to_int(char *str);
int in_str(char *str, char *to_find);
int has_num(char *str);
int has_comma(char *str);

int main(){
    struct price Price[5][SIZE];
    int idx[5] = {0};
    char input[150];
    enum datatype datatype;
    struct cat Cat;
    int printed = 0;

    while(gets(input)){
        if(input[0] == 0){
            break;
        }
        else if(has_num(input)){
            strcpy(Price[datatype][idx[datatype]].name, strtok(input, ","));
            Price[datatype][idx[datatype]].price = str_to_int(strtok(NULL, ","));
            idx[datatype]++;
        }
        else{
            if(in_str(input, "Origin")){
                datatype = ORIGIN;
            }
            else if(in_str(input, "Type")){
                datatype = TYPE;
            }
            else if(in_str(input, "Body")){
                datatype = BODY;
            }
            else if(in_str(input, "Coat")){
                datatype = COAT;
            }
        }
    }

    printf("====================================================================================\n");
    printf("Breed                    Origin                Type    Body        Coat        Price\n");
    printf("------------------------------------------------------------------------------------\n");

    while(gets(input)){
        if(!has_comma(input)){
            if(printed){
                printf("====================================================================================\n\n");
                break;
            }
        }
        else{
            printed = 1;
            Cat.price = 0;
            Cat.datatype[BREED] = strtok(input, ",");
            for(enum datatype t=ORIGIN; t<=COAT; t++){
                Cat.datatype[t] = strtok(NULL, ",");
                for(size_t i=0; i<idx[t]; i++){
                    if(in_str(Price[t][i].name, Cat.datatype[t])){
                        Cat.price += Price[t][i].price;
                        break;
                    }
                }
            }

            printf("%-25s%-22s  %-6c%-12s%-12s%5d\n", Cat.datatype[BREED], Cat.datatype[ORIGIN], Cat.datatype[TYPE][0], Cat.datatype[BODY], Cat.datatype[COAT], Cat.price);
        }
    }
    printf("====================================================================================\n");
}

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

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

    return 0;
}

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

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

# [C] Subnetwork
## Notes
## Code
```c=
#include <stdio.h>
#include <stdlib.h>

unsigned int arr_to_int(int arr[]);
unsigned int create_mask(int n);

int main(){
    int n, host[4], prefix, ip[4];
    unsigned int host_int, ip_int, mask;
    int printed = 0;

    while(scanf("%d", &n)){
        if(n == 0){
            break;
        }
        scanf("%d.%d.%d.%d/%d", &host[0], &host[1], &host[2], &host[3], &prefix);
        if(printed){
            puts("");
        }
        else{
            printed = 1;
        }
        printf("--------------------------------------------------------------------------------\n");
        printf("The following addresses are within the subnetwork %d.%d.%d.%d/%d:\n", host[0], host[1], host[2], host[3], prefix);
        host_int = arr_to_int(host);
        mask = create_mask(prefix);

        for(int i=0; i<n; i++){
            scanf("%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]);
            ip_int = arr_to_int(ip);

            if((host_int & mask) == (ip_int & mask)){
                printf("%d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
            }
        }

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

unsigned int arr_to_int(int arr[]){
    unsigned int result = 0;
    for(size_t i=0; i<4; i++){
        result <<= 8;
        result |= arr[i];
    }
    return result;
}

unsigned int create_mask(int n){
    unsigned int mask = 0;
    for(int i=0; i<n; i++){
        mask |= 1;
        mask <<= 1;
    }
    mask <<= (32 - n - 1);
    return mask;
}
```

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