diff --git a/semester3/pset0/7-1-Balancing-Symbols.cpp b/semester3/pset0/7-1-Balancing-Symbols.cpp new file mode 100644 index 0000000..4d71455 --- /dev/null +++ b/semester3/pset0/7-1-Balancing-Symbols.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include + +char getParen(const char &ch) { + if (ch == ']') { + return '['; + } else if (ch == ')') { + return '('; + } else { + return '{'; + } +} + +int main(void) { + std::string input; + std::cin >> input; + std::stack st; + bool ans[4] = {false, false, false, false}; + + for (char ch : input) { + // scan for errors + // () [] {} + if (ch == '(' || ch == '[' || ch == '{') { + st.push(ch); + } else if (ch == ')' || ch == ']' || ch == '}') { + // check + if (!st.empty() && st.top() == getParen(ch)) { + // pop, nothing wrong + st.pop(); + } else { + if (ch == ')') { + ans[1] = true; + } else if (ch == ']') { + ans[2] = true; + } else { + ans[3] = true; + } + ans[0] = true; + } + } + } + + while (!st.empty()) { + if (st.top() == '(') { + ans[1] = true; + } else if (st.top() == '[') { + ans[2] = true; + } else { + ans[3] = true; + } + + ans[0] = true; + st.pop(); + } + + if (ans[0] == false) { + printf("0"); + } else { + for (int i = 1; i < 4; i++) { + if (ans[i] == true) { + printf("%d,", i); + } + } + } + + return 0; +} diff --git a/semester3/pset0/7-2-Infix-to-Postfix-Conversion.cpp b/semester3/pset0/7-2-Infix-to-Postfix-Conversion.cpp new file mode 100644 index 0000000..23a5dad --- /dev/null +++ b/semester3/pset0/7-2-Infix-to-Postfix-Conversion.cpp @@ -0,0 +1,92 @@ +// Convert the infix expression to postfix expression. +#include +#include +#include +#include +#include +using namespace std; + +int prec(char ch) { + if (ch == '+' || ch == '-') { + return 0; + } else if (ch == '*' || ch == '/') { + return 1; + } else { + return -1; + } +} + +void printEval(string &postfix) { + stack st; + double l, r; + double ans; + for (char ch : postfix) { + if (isdigit(ch)) { + st.push(ch - '0'); + } else { + r = st.top(); + st.pop(); + l = st.top(); + st.pop(); + if (ch == '+') { + ans = l + r; + } else if (ch == '-') { + ans = l - r; + } else if (ch == '*') { + ans = l * r; + } else if (ch == '/') { + ans = l / r; + } + st.push(ans); + } + } + printf("%.2lf\n", st.top()); +} + +void printFix(string &postfix) { + for (char ch : postfix) { + printf("%c ", ch); + } +} + +int main(void) { + string input; + cin >> input; + + stack st; + string postfix; + + for (char ch : input) { + if (isdigit(ch)) { + postfix.push_back(ch); + } else { + // if is not a digit + if (ch == '(') { + st.push(ch); + } else if (ch == ')') { + while (st.top() != '(') { + postfix.push_back(st.top()); + st.pop(); + } + st.pop(); + } else { + // a operand + while (!st.empty() && prec(ch) <= prec(st.top())) { + postfix.push_back(st.top()); + st.pop(); + } + st.push(ch); + } + } + } + + while (!st.empty()) { + postfix.push_back(st.top()); + st.pop(); + } + + printEval(postfix); + printFix(postfix); + + return 0; +} diff --git a/semester3/pset0/7-3-central-list.c b/semester3/pset0/7-3-central-list.c new file mode 100644 index 0000000..a06776f --- /dev/null +++ b/semester3/pset0/7-3-central-list.c @@ -0,0 +1,63 @@ +#include +#include +#include + +typedef struct stack { + int val; + struct stack *next; +} stack; + +stack *push(stack *st, int val) { + stack *new = malloc(sizeof(stack)); + new->next = st; + new->val = val; + return new; +} + +int isEmpty(stack *st) { return st == NULL; } + +int peek(stack *st) { return st->val; } + +stack *pop(stack *st) { + stack *tmp = st->next; + free(st); + return tmp; +} + +int main(void) { + stack *st = NULL; + + int size = 0; + int in; + int last; + int lastUsed = 0; + int centFound = 0; + + while (scanf("%d,", &in) != EOF) { + size++; + if (!isEmpty(st) && peek(st) == in) { + st = pop(st); + } else if (lastUsed == 1 && last == in) { + st = pop(st); + st = pop(st); + centFound = 1; + } else { + if (!isEmpty(st)) { + last = peek(st); + lastUsed = 1; + } + // printf("PUSHING: %d\n", in); + st = push(st, in); + } + } + + if (size > 0 && isEmpty(st) && centFound == 1) { + printf("Yes"); + } else if (size == 1) { + printf("Yes"); + } else { + printf("No"); + } + + return 0; +} diff --git a/semester3/pset0/7-4-who-is-the-last.c b/semester3/pset0/7-4-who-is-the-last.c new file mode 100644 index 0000000..abfa7ec --- /dev/null +++ b/semester3/pset0/7-4-who-is-the-last.c @@ -0,0 +1,84 @@ +#include +#include + +typedef struct listNode { + int id; + struct listNode *next; + int out; +} listNode; + +listNode *appendNode(int id, listNode *head) { + listNode *new = malloc(sizeof(listNode)); + new->id = id; + new->next = NULL; + new->out = 0; + + head->next = new; + return new; +} + +listNode *makecircle(const int N) { + listNode *head = malloc(sizeof(listNode)); + head->id = 1; + head->next = NULL; + head->out = 0; + + listNode *ptr = head; + for (int i = 2; i <= N; i++) { + ptr = appendNode(i, ptr); + // printf("appending: %d\n", i); + } + ptr->next = head; + + return head; +} + +void printCircle(listNode *head) { + if (head->out == 0) { + printf("%d->", head->id); + } + head = head->next; + while (head->id != 1) { + if (head->out == 0) { + printf("%d->", head->id); + } + head = head->next; + } + printf("|"); +} + +int main(void) { + int N, M; + scanf("%d,%d", &N, &M); + + listNode *head = makecircle(N); + // printCircle(head); + + int count = N; + int step = 0; + listNode *ptr = head; + while (count != 0) { + while (step != M) { + while (ptr->out == 1) { + ptr = ptr->next; + } + ptr = ptr->next; + step++; + } + step = 0; + while (ptr->out == 1) { + ptr = ptr->next; + } + + ptr->out = 1; + count--; + + if (count != 0) { + printf("%d,", ptr->id); + } + } + + printf("%d", ptr->id); + + return 0; +}