assignment problem 2
This commit is contained in:
5
assignment1/problem2/Makefile
Normal file
5
assignment1/problem2/Makefile
Normal file
@@ -0,0 +1,5 @@
|
||||
EXEC = validbrackets
|
||||
SRC = $(wildcard *.c)
|
||||
|
||||
$(EXEC): $(SRC)
|
||||
gcc -o $@ $^ -Wall
|
||||
108
assignment1/problem2/stack.c
Normal file
108
assignment1/problem2/stack.c
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Author: Walter
|
||||
* Student ID: 1930006025
|
||||
* Assignment_1_problem_2
|
||||
* stack lib, allow to create empty stack
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "stack.h"
|
||||
|
||||
bool CreateStack(Stack *stack, int size) {
|
||||
/* check, allow create empty stack */
|
||||
assert(size >= 0);
|
||||
|
||||
/* memory allocate */
|
||||
stack->data = (char *)malloc(sizeof(char) * size);
|
||||
if (stack->data == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* init attribute */
|
||||
stack->top = -1;
|
||||
stack->size = size;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IsEmpty(Stack *stack) {
|
||||
/* if empty, stack->top = -1 */
|
||||
return stack->top < 0;
|
||||
}
|
||||
|
||||
bool IsFull(Stack *stack) {
|
||||
/* if full, stack->top = stack->size */
|
||||
return stack->top >= stack->size;
|
||||
}
|
||||
|
||||
bool Top(Stack *stack, char *x) {
|
||||
/* check empty */
|
||||
if (IsEmpty(stack)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* get top */
|
||||
*x = stack->data[stack->top];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Push(Stack *stack, char x) {
|
||||
/* check */
|
||||
if (IsFull(stack)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* push */
|
||||
stack->data[++stack->top] = x;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Pop(Stack *stack, char *x) {
|
||||
/* check */
|
||||
if (IsEmpty(stack)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* get x, and top - 1 */
|
||||
*x = stack->data[stack->top--];
|
||||
return true;
|
||||
}
|
||||
|
||||
void DisplayStack(Stack *stack) {
|
||||
char *top_p = stack->data + stack->top;
|
||||
|
||||
/* print the top pointer hint */
|
||||
printf("top --> ");
|
||||
|
||||
/* loop for stack data */
|
||||
for (; top_p >= stack->data; top_p--) {
|
||||
|
||||
/* print top --> hint */
|
||||
if (top_p != stack->data + stack->top) {
|
||||
printf(" ");
|
||||
}
|
||||
|
||||
printf("| %c |\n", *top_p);
|
||||
}
|
||||
|
||||
/* print top --> hint */
|
||||
if (top_p != stack->data + stack->top) {
|
||||
printf(" ");
|
||||
}
|
||||
|
||||
printf("+--------------+\n");
|
||||
}
|
||||
|
||||
void DestroyStack(Stack *stack) {
|
||||
free(stack->data);
|
||||
stack->data = NULL;
|
||||
stack->size = 0;
|
||||
stack->top = -1;
|
||||
}
|
||||
47
assignment1/problem2/stack.h
Normal file
47
assignment1/problem2/stack.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Author: Walter
|
||||
* Student ID: 1930006025
|
||||
* Assignment_1_problem_2
|
||||
* allow to create empty stack
|
||||
*/
|
||||
|
||||
#ifndef MY_STACK
|
||||
#define MY_STACK
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
/* struct for stack */
|
||||
typedef struct Stack_str {
|
||||
/* the size of stack */
|
||||
int size;
|
||||
/* top index, empty is -1 */
|
||||
int top;
|
||||
/* data */
|
||||
char *data;
|
||||
} Stack;
|
||||
|
||||
/* create an stack with size */
|
||||
bool CreateStack(Stack *stack, int size);
|
||||
|
||||
/* check a stack whether it contains data */
|
||||
bool IsEmpty(Stack *stack);
|
||||
|
||||
/* check full of stack */
|
||||
bool IsFull(Stack *stack);
|
||||
|
||||
/* get the top value of a stack */
|
||||
bool Top(Stack *stack, char *x);
|
||||
|
||||
/* add a value to stack */
|
||||
bool Push(Stack *stack, char x);
|
||||
|
||||
/* delete a value from stack */
|
||||
bool Pop(Stack *stack, char *x);
|
||||
|
||||
/* print the stack */
|
||||
void DisplayStack(Stack *stack);
|
||||
|
||||
/* delete a stack */
|
||||
void DestroyStack(Stack *stack);
|
||||
|
||||
#endif
|
||||
40
assignment1/problem2/test_main.c
Normal file
40
assignment1/problem2/test_main.c
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Author: Walter
|
||||
* Student ID: 1930006025
|
||||
* Assignment_1_problem_2
|
||||
* Test function
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "validbrackets.h"
|
||||
|
||||
/* test unit */
|
||||
typedef struct Unit_str {
|
||||
char *text;
|
||||
bool except;
|
||||
} Unit;
|
||||
|
||||
int main() {
|
||||
|
||||
/* use for converting bool to string */
|
||||
static char *BOOL_TEXT[] = {"false", "true"};
|
||||
|
||||
Unit *test;
|
||||
static Unit case1 = {"{()<()>}[]", true};
|
||||
static Unit case2 = {"(<)>", false};
|
||||
static Unit case3 = {"{()}[", false};
|
||||
static Unit case4 = {"", true};
|
||||
static Unit case5 = {NULL, false};
|
||||
|
||||
static char *HINT = "Test '%s', except: %s, result: %s\n";
|
||||
|
||||
test = &case1; printf(HINT, test->text, BOOL_TEXT[test->except], BOOL_TEXT[ValidBrackets(test->text)]);
|
||||
test = &case2; printf(HINT, test->text, BOOL_TEXT[test->except], BOOL_TEXT[ValidBrackets(test->text)]);
|
||||
test = &case3; printf(HINT, test->text, BOOL_TEXT[test->except], BOOL_TEXT[ValidBrackets(test->text)]);
|
||||
test = &case4; printf(HINT, test->text, BOOL_TEXT[test->except], BOOL_TEXT[ValidBrackets(test->text)]);
|
||||
test = &case5; printf(HINT, test->text, BOOL_TEXT[test->except], BOOL_TEXT[ValidBrackets(test->text)]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
106
assignment1/problem2/validbrackets.c
Normal file
106
assignment1/problem2/validbrackets.c
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Author: Walter
|
||||
* Student ID: 1930006025
|
||||
* Assignment_1_problem_2
|
||||
* validbrackets function
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "stack.h"
|
||||
#include "validbrackets.h"
|
||||
|
||||
/* all the left brackets */
|
||||
static char LEFTBRACKETS[] = "({[<";
|
||||
|
||||
/* all the right brackets */
|
||||
static char RIGHTBRACKETS[] = ")}]>";
|
||||
|
||||
/* number of brackets pairs */
|
||||
static int BRACKETS_LEN = 4;
|
||||
|
||||
bool ValidBrackets(char *str) {
|
||||
|
||||
/* if string pass all the test, will return default value 'true' */
|
||||
bool result = true;
|
||||
|
||||
/* null pointer will return false */
|
||||
if (str == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* tmp value to store the left brackets */
|
||||
char left;
|
||||
|
||||
/* length of string */
|
||||
size_t str_len = strlen(str);
|
||||
|
||||
Stack stack;
|
||||
/* only half length of the string is enougth,
|
||||
* empty stack is allowed */
|
||||
CreateStack(&stack, str_len/2);
|
||||
|
||||
/* loop for all char in string */
|
||||
for (size_t i=0; i<str_len; i++) {
|
||||
|
||||
/* full stack means the string is not vailed */
|
||||
if (IsFull(&stack)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
|
||||
/* if it is not left brackets will return -1 */
|
||||
if (IndexLeftBracket(str[i]) != -1) {
|
||||
/* store the left brackets in stack */
|
||||
Push(&stack, str[i]);
|
||||
} else {
|
||||
/* check empty stack before pop from stack */
|
||||
if (IsEmpty(&stack)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
/* get the pair left brackets from stack */
|
||||
Pop(&stack, &left);
|
||||
|
||||
/* check if the left and right brackets matched */
|
||||
if (!MatchBracket(left, str[i])) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* if finally the stack is not empty, return falsle */
|
||||
if (!(IsEmpty(&stack))) {
|
||||
result = false;
|
||||
}
|
||||
/* no memory leak */
|
||||
DestroyStack(&stack);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int IndexLeftBracket(char c) {
|
||||
for (int i=0; i<BRACKETS_LEN; i++) {
|
||||
if (c == LEFTBRACKETS[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
/* if c is not left brackets, return -1 */
|
||||
return -1;
|
||||
}
|
||||
|
||||
int IndexRightBracket(char c) {
|
||||
for (int i=0; i<BRACKETS_LEN; i++) {
|
||||
if (c == RIGHTBRACKETS[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
/* if c is not right brackets, return -1 */
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool MatchBracket(char left, char right) {
|
||||
/* check whether left and right brackets are matched */
|
||||
return IndexLeftBracket(left) == IndexRightBracket(right);
|
||||
}
|
||||
31
assignment1/problem2/validbrackets.h
Normal file
31
assignment1/problem2/validbrackets.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Author: Walter
|
||||
* Student ID: 1930006025
|
||||
* Assignment_1_problem_2
|
||||
* validbrackets header files
|
||||
*/
|
||||
|
||||
#ifndef VAILDBRACKETS
|
||||
#define VAILDBRACKETS
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "stack.h"
|
||||
|
||||
/* cvalid a string brackets,
|
||||
* return true if empty string,
|
||||
* return false if NULL string pointer */
|
||||
bool ValidBrackets(char *str);
|
||||
|
||||
/* get the index number of left bracket,
|
||||
* return -1 if c is not a left bracket */
|
||||
int IndexLeftBracket(char c);
|
||||
|
||||
/* get the index number of right breacket,
|
||||
* return -1 if c is not a left bracket */
|
||||
int IndexRightBracket(char c);
|
||||
|
||||
/* compare two brackets if they match */
|
||||
bool MatchBracket(char left, char right);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user