From cd77744a06232f2344c766d587f0f062b7cda83c Mon Sep 17 00:00:00 2001 From: juan Date: Sun, 20 Nov 2022 21:22:03 +0800 Subject: [PATCH] Add lazy versions of pset1 --- .../pset1/7-2-Infix-to-Postfix-Conversion.c | 120 ++++++++++++++++-- semester3/pset1/7-3-central-list-ARRAY.c | 22 ++++ semester3/pset1/7-4-who-is-the-last-ARRAY.c | 42 ++++++ 3 files changed, 172 insertions(+), 12 deletions(-) create mode 100644 semester3/pset1/7-3-central-list-ARRAY.c create mode 100644 semester3/pset1/7-4-who-is-the-last-ARRAY.c diff --git a/semester3/pset1/7-2-Infix-to-Postfix-Conversion.c b/semester3/pset1/7-2-Infix-to-Postfix-Conversion.c index 925e484..f6e5c02 100644 --- a/semester3/pset1/7-2-Infix-to-Postfix-Conversion.c +++ b/semester3/pset1/7-2-Infix-to-Postfix-Conversion.c @@ -1,26 +1,122 @@ +// 20:05 - 20:50 #include -#define SIZE 100 +#include +#define MAX 256 + +int getPriority(char ch) { + if (ch == '+' || ch == '-') { + return 0; + } else if (ch == '*' || ch == '/') { + return 1; + } else { + return -1; + } +} typedef struct stack { + double *arr; int top; - int capacity; - int* array; } stack; -void push(stack* st, int val) { - st->array[++st->top] = val; +stack *newStack() { + stack *st = malloc(sizeof(stack)); + st->top = 0; + st->arr = malloc(sizeof(double) * MAX); + + return st; } -void pop(stack* st) { - st->top--; +void push(stack *st, double val) { + // printf("Pushing: %.2lf\n", val); + st->arr[st->top++] = val; } -int peek(stack* st) { - return st->array[st->top]; +double top(stack *st) { return st->arr[st->top - 1]; } + +void pop(stack *st) { st->top--; } + +int isEmpty(stack *st) { return (st->top == 0); } + +void toPostfix(char *buf, char *out) { + int count = 0; + stack *st = newStack(); + for (int i = 0; buf[i] != '\0'; i++) { + if (buf[i] >= '0' && buf[i] <= '9') { + // is digit, push to out array + out[count++] = buf[i]; + } else { + if (buf[i] == '(') { + // left paren, don't pop elements in stack. + push(st, buf[i]); + } else if (buf[i] == ')') { + // push everything after the ( + while (top(st) != '(') { + out[count++] = top(st); + pop(st); + } + pop(st); + } else { + // the equal sign is important + while (!isEmpty(st) && getPriority(top(st)) >= getPriority(buf[i])) { + out[count++] = top(st); + pop(st); + } + push(st, buf[i]); + } + } + } + + while (!isEmpty(st)) { + out[count++] = top(st); + pop(st); + } + + out[count] = '\0'; +} + +void calculate(char *postfix) { + double l, r; + stack *st = newStack(); + for (int i = 0; postfix[i] != '\0'; i++) { + if (postfix[i] >= '0' && postfix[i] <= '9') { + // remember convert from char to int + push(st, postfix[i] - '0'); + } else { + r = top(st); + pop(st); + l = top(st); + pop(st); + if (postfix[i] == '+') { + push(st, l + r); + } else if (postfix[i] == '-') { + push(st, l - r); + } else if (postfix[i] == '*') { + push(st, l * r); + } else if (postfix[i] == '/') { + push(st, l / r); + } else { + printf("ERROR: %c is not recognized\n", postfix[i]); + } + } + } + + printf("%.2lf\n", top(st)); +} + +void printPostfix(char *postfix) { + for (int i = 0; postfix[i] != '\0'; i++) { + printf("%c ", postfix[i]); + } } int main(void) { - char input[SIZE]; - - fscanf(input, SIZE, stdin); + char buf[MAX]; + char out[MAX]; + + scanf("%s", buf); + + toPostfix(buf, out); + calculate(out); + printPostfix(out); + return 0; } diff --git a/semester3/pset1/7-3-central-list-ARRAY.c b/semester3/pset1/7-3-central-list-ARRAY.c new file mode 100644 index 0000000..ce43df3 --- /dev/null +++ b/semester3/pset1/7-3-central-list-ARRAY.c @@ -0,0 +1,22 @@ +// 20:50 - 21:00 +#include +#define MAX 100 + +int main(void) { + int arr[MAX]; + int counter = 0; + while (scanf("%d,", &arr[counter]) != EOF) { + counter++; + } + + for (int i = 0, end = counter / 2; i < end; i++) { + if (arr[i] != arr[counter - i - 1]) { + printf("No"); + return 0; + } + } + + printf("Yes"); + + return 0; +} diff --git a/semester3/pset1/7-4-who-is-the-last-ARRAY.c b/semester3/pset1/7-4-who-is-the-last-ARRAY.c new file mode 100644 index 0000000..19726c8 --- /dev/null +++ b/semester3/pset1/7-4-who-is-the-last-ARRAY.c @@ -0,0 +1,42 @@ +// 21:01 - 21:21 +#include + +int step(int cur, int *dead, int N, int M) { + for (int i = 0; i < M; i++) { + cur = (cur + 1) % N; + while (dead[cur] == 1) { + cur = (cur + 1) % N; + } + } + return cur; +} + +int main(void) { + int N, M; + scanf("%d,%d", &N, &M); + + int deadCount = 0; + int dead[N]; + for (int i = 0; i < N; i++) { + dead[i] = 0; + } + + int cur = 0; + + while (1) { + cur = step(cur, dead, N, M); + + dead[cur] = 1; + deadCount++; + if (deadCount == N) { + printf("%d", cur + 1); + break; + } else { + printf("%d,", cur + 1); + } + + cur = step(cur, dead, N, 1); + } + + return 0; +}