Featured image of post 109-02 第一次上機考

109-02 第一次上機考

Q1 字串取代

從命令列引數讀入引數,分別是要被取代的字串及要變成的字串。

/*
七題選六題  最後兩題20分
Q01
*/

#include<math.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define TRUE 1
#define FALSE 0

int main(int argc, char *argv[]) {
    char *f = argv[1];
    char *t = argv[2];

    char *str = malloc(10101 * sizeof(char));
    while(fgets(str, 10101, stdin)) {
        if(str[strlen(str) - 1] == '\n')str[strlen(str) - 1] = '\0';
        char *p = strstr(str, f);
        while(p != NULL) {
            while(str != p) {
                printf("%c\n", *str++);
            }
            printf("%s", t);
            str += strlen(f);
            p = strstr(str, f);
        }
        while(*str != '\0') {
            printf("%c", *str++);
        }
        printf("\n");
    }
}

Q2

輸入postfix運算式,請輸出計算結果。

範例輸入:547 661 - 830 + 616 - 891 81 / 660 / 309 * + 範例輸出:100

/*
七題選六題  最後兩題20分
Q02
*/

#include<math.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define TRUE 1
#define FALSE 0

int isnum(char *str);
int cou(int n1, char *c, int n2);

int main() {
    char *str = malloc(10101 * sizeof(char));
    while(fgets(str, 10101, stdin)) {
        int list[10101];
        int ind = 0;
        if(str[strlen(str) - 1] == '\n')str[strlen(str) - 1] = '\0';
        char *p = strtok(str, " ");
        while(p != NULL) {
            // printf("p: %s\n", p);
            if(isnum(p)) {
                // printf("Is number");
                list[ind++] = atoi(p);
            } else {
                // printf("Not is number");
                list[ind - 2] = cou(list[ind - 2], p, list[ind - 1]);
                ind--;
            }
            p = strtok(NULL, " ");
        }
        printf("%d\n", list[0]);
    }
}

int isnum(char *str) {
    while(*str != '\0') {
        if(!(*str >= '0' && *str <= '9')) {
            return 0;
        }
        str++;
    }
    return 1;
}

int cou(int n1, char *c, int n2) {
    switch(*c) {
        case '+':
            return n1 + n2;
        case '-':
            return n1 - n2;
        case '*':
            return n1 * n2;
        case '/':
            return n1 / n2;
    }
    return 0;
}

逐行讀入,並從頭輸出。

範例輸入:

W
S
N
N
E

範例輸出:

WSNNE
/*
七題選六題  最後兩題20分
Q03

Link list
- inser
- print
- clear
*/

#include<math.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define TRUE 1
#define FALSE 0

typedef struct n {
    char *str;
    struct n *next;
} node;

node *insert(node *list, char *str);
void print(node *list);
void clear(node *list);

int main() {
    char *str = malloc(10101 * sizeof(char));
    node *head = NULL;
    while(fgets(str, 10101, stdin)) {
        if(str[strlen(str) - 1] == '\n')str[strlen(str) - 1] = '\0';
        head = insert(head, str);
    }
    print(head);
    printf("\n");

    clear(head);
}

node *insert(node *list, char *str) {
    if(list == NULL) {
        node *n = malloc(sizeof(node));
        n->str = strdup(str);
        n->next = NULL;
        return n;
    }
    list->next = insert(list->next, str);
    return list;
}

void print(node *list) {
    if(list == NULL) return;
    printf("%s", list->str);
    print(list->next);
    return;
}

void clear(node *list) {
    if(list == NULL) return;
    clear(list->next);
    list->str = NULL;
    list->next = NULL;
    free(list);
    return;
}
/*
七題選六題  最後兩題20分
Q04

Link list
- inser
- print
- clear
*/

#include<math.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct n {
    char *str;
    struct n *next;
} node;

node *insert(node *list, char *str);
void print(node *list);
void clear(node *list);

int main() {
    char *str = malloc(10101 * sizeof(char));
    node *head = NULL;
    while(fgets(str, 10101, stdin)) {
        if(str[strlen(str) - 1] == '\n')str[strlen(str) - 1] = '\0';
        head = insert(head, str);
    }
    print(head);
    printf("\n");

    clear(head);
}

node *insert(node *list, char *str) {
    if(list == NULL) {
        node *n = malloc(sizeof(node));
        n->str = strdup(str);
        n->next = NULL;
        return n;
    } else {
        node *n = malloc(sizeof(node));
        n->str = strdup(str);
        n->next = list;
        return n;
    }
}

void print(node *list) {
    if(list == NULL) return;
    char p;
    switch(*(list->str)) {
        case 'N': p = 'S'; break;
        case 'S': p = 'N'; break;
        case 'E': p = 'W'; break;
        case 'W': p = 'E'; break;
    }
    printf("%c", p);
    print(list->next);
    return;
}

void clear(node *list) {
    if(list == NULL) return;
    clear(list->next);
    list->str = NULL;
    list->next = NULL;
    free(list);
    return;
}

Q5 移除多餘空白字元

/*
七題選六題  最後兩題20分
Q05

String
*/

#include<math.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main() {
    char *str = malloc(10101 * sizeof(char));
    while(fgets(str, 10101, stdin)) {
        if(str[strlen(str) - 1] == '\n')str[strlen(str) - 1] = '\0';
        // char *temp = str;
        int l = strlen(str) - 1;
        for(int i = l; i >= 0; i--) {
            if(str[i] == ' ') l--;
            else break;
        }
        while(*str == ' ') {
            str++;
            l--;
        }
        int i = 0;
        while(i <= l) {
            if(str[i] != ' ') printf("%c", str[i]);
            else {
                if(str[i - 1] != ' ') printf(" ");
            }
            i++;
        }
        printf("\n");
    }
}

Q6 BST

讀入資料,並輸出內容。

/*
七題選六題  最後兩題20分
Q06

BST
- search
- inser
- print
- clear
*/

#include<math.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct n {
    char *str;
    struct n *l;
    struct n *r;
} node;

int search(node *list, char *str);
node *insert(node *list, char *str);
void pre_print(node *list);
void post_print(node *list);
void in_print(node *list);
void clear(node *list);

int main(int argc, char *argv[]) {
    char c;
    if(argc == 2) {
        c = argv[1][1];
    }

    char *str = malloc(10101 * sizeof(char));
    node *head = NULL;
    while(fgets(str, 10101, stdin)) {
        if(str[strlen(str) - 1] == '\n')str[strlen(str) - 1] = '\0';
        if(*str == 'i') {
            str += 2;
            if(search(head, str)) {
                // 
            } else {
                head = insert(head, str);
            }
        }
        else {
            switch (c) {
                case 'p':
                    pre_print(head);
                    break;
                case 'P':
                    post_print(head);
                    break;
                case 'i':
                    in_print(head);
                    break;
            }
        }
    }
    clear(head);
}

int search(node *list, char *str) {
    if(list == NULL) return 0;
    int c = strcmp(list->str, str);
    if(c == 0 ) return 1;
    else if(c > 0) return search(list->l, str);
    else return search(list->r, str);
}

node *insert(node *list, char *str) {
    if(list == NULL) {
        node *news = malloc(sizeof(node));
        news->str = strdup(str);
        news->l = NULL;
        news->r = NULL;
        return news;
    }
    int c = strcmp(list->str, str);
    if(c > 0) list->l = insert(list->l, str);
    else list->r = insert(list->r, str);
    return list;
}

void pre_print(node *list) {
    if(list == NULL) return;
    printf("%s\n", list->str);
    pre_print(list->l);
    pre_print(list->r);
    return;
}

void post_print(node *list) {
    if(list == NULL) return;
    post_print(list->l);
    post_print(list->r);
    printf("%s\n", list->str);
    return;
}

void in_print(node *list) {
    if(list == NULL) return;
    in_print(list->l);
    printf("%s\n", list->str);
    in_print(list->r);
    return;
}

void clear(node *list) {
    if(list == NULL) return;
    clear(list->l);
    clear(list->r);
    list->str = NULL;
    free(list);
    return;
}

Q7 C qsort


/*
七題選六題  最後兩題20
Q07

qsort
*/

#include<math.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct n {
    char *str;
    struct n *next;
} node;

int cmp(const void *a, const void *b);
int num_cmp(const void *a, const void *b);

int usenum = 0, re = 0;

int main(int argc, char *argv[]) {
    char a;
    if(argc >= 2) {
        if(strlen(argv[1]) == 2) {
            a = argv[1][1];
            if(a == 'n') usenum = 1;
            else if(a == 'd') re = 1;
            else {
                printf("usage: mysort [-dn]\n");
                return 0;
            }
        } else {
            printf("usage: mysort [-dn]\n");
            return 0;
        }
    }
    if(argc == 3) {
        if(strlen(argv[2]) == 2) {
            a = argv[2][1];
            if(a == 'n') usenum = 1;
            else if(a == 'd') re = 1;
            else {
                printf("usage: mysort [-dn]\n");
                return 0;
            }
        } else {
            printf("usage: mysort [-dn]\n");
            return 0;
        }
    }
    char *str = malloc(10101 * sizeof(char));
    
    int i = 0;
    node arr[22222];
    while(fgets(str, 10101, stdin)) {
        if(str[strlen(str) - 1] == '\n')str[strlen(str) - 1] = '\0';
        node *news = malloc(sizeof(node));
        news->str = strdup(str);
        arr[i++] = *news;
    }
    
    if(usenum)
        qsort(arr, i, sizeof(node), num_cmp);
    else
        qsort(arr, i, sizeof(node), cmp);
    if(re) {
        for(int j = i - 1; j >= 0; j--) {
            printf("%s\n", arr[j].str);
        }
    } else {
        for(int j = 0; j < i; j++) {
            printf("%s\n", arr[j].str);
        }
    }

    for(int j = 0; j < i; j++) {
        free(arr[j].str);
        free(arr[j].next);
    }
}

int cmp(const void *a, const void *b) {
    node *aa = (node *)a;
    node *bb = (node *)b;
    return strcmp(aa->str, bb->str);
}

int num_cmp(const void *a, const void *b) {
    node *aa = (node *)a;
    node *bb = (node *)b;
    return atoi(aa->str) - atoi(bb->str);
}

 

Licensed under CC BY-NC-SA 4.0