assignment 1 problem 3 clang-format

This commit is contained in:
2021-03-30 11:04:35 +08:00
parent c312dd6400
commit 52c2445f05
4 changed files with 39 additions and 46 deletions

View File

@@ -5,8 +5,8 @@
* Stack, with functioin automaticlly increase the size * Stack, with functioin automaticlly increase the size
*/ */
#include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -14,7 +14,6 @@
#include "stack.h" #include "stack.h"
bool CreateStack(Stack *stack, int size) { bool CreateStack(Stack *stack, int size) {
/* check */ /* check */
assert(size > 0); assert(size > 0);

View File

@@ -8,9 +8,8 @@
#ifndef MY_STRUCT_H #ifndef MY_STRUCT_H
#define MY_STRUCT_H #define MY_STRUCT_H
#include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h>
/* Used for AST Node's string */ /* Used for AST Node's string */
typedef struct ASTString_str { typedef struct ASTString_str {

View File

@@ -1,11 +1,11 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
#include "uncompress.h"
#include "stack.h" #include "stack.h"
#include "uncompress.h"
ASTScanner *CreateASTScanner(char *string) { ASTScanner *CreateASTScanner(char *string) {
/* Special case define in pdf */ /* Special case define in pdf */
@@ -65,12 +65,14 @@ char ASTScan(ASTScanner *scanner, ASTNode **ret_node) {
return 1; return 1;
} }
if (!IsNumber(scanner->string[i - 2])) { if (!IsNumber(scanner->string[i - 2])) {
fprintf(stderr, "Syntax error: invailed value of repeat at index %ld\n", i-1); fprintf(stderr, "Syntax error: invailed value of repeat at index %ld\n",
i - 1);
return 1; return 1;
} }
/* get number and the string before the number */ /* get number and the string before the number */
node->repeat = GetNumber(scanner->string, scanner->current_index, i-1, &node->string); node->repeat = GetNumber(scanner->string, scanner->current_index, i - 1,
&node->string);
/* if the string exists */ /* if the string exists */
if (node->string.string) { if (node->string.string) {
/* change type to 1 */ /* change type to 1 */
@@ -111,7 +113,8 @@ char ASTScan(ASTScanner *scanner, ASTNode **ret_node) {
return 0; return 0;
} }
unsigned int GetNumber(char *string, size_t start, size_t end, ASTString *other_string) { unsigned int GetNumber(char *string, size_t start, size_t end,
ASTString *other_string) {
unsigned int result = 0; unsigned int result = 0;
size_t index = end - 1; size_t index = end - 1;
@@ -131,9 +134,7 @@ unsigned int GetNumber(char *string, size_t start, size_t end, ASTString *other_
return result; return result;
} }
bool IsNumber(const char c) { bool IsNumber(const char c) { return c >= '0' && c <= '9'; }
return c >= '0' && c <= '9';
}
unsigned int Power(int x, int y) { unsigned int Power(int x, int y) {
unsigned int result = 1; unsigned int result = 1;
@@ -147,13 +148,8 @@ void DEBUG_NODE(ASTNode *root) {
ASTNode *currNode = root; ASTNode *currNode = root;
while (currNode) { while (currNode) {
printf("[%p] type: %d, next: %p, child: %p, repeat: %d, string: %ld-%ld\n", printf("[%p] type: %d, next: %p, child: %p, repeat: %d, string: %ld-%ld\n",
currNode, currNode, currNode->type, currNode->next, currNode->child,
currNode->type, currNode->repeat, currNode->string.start, currNode->string.end);
currNode->next,
currNode->child,
currNode->repeat,
currNode->string.start,
currNode->string.end);
if (currNode->type == 2) { if (currNode->type == 2) {
/* recursion to child */ /* recursion to child */
@@ -324,6 +320,4 @@ void Uncompress(char *string, FILE *file) {
DestroyASTScanner(scanner); DestroyASTScanner(scanner);
} }
void DestroyASTScanner(ASTScanner *scanner) { void DestroyASTScanner(ASTScanner *scanner) { free(scanner); }
free(scanner);
}

View File

@@ -1,8 +1,8 @@
#ifndef UNCOMPRESS #ifndef UNCOMPRESS
#define UNCOMPRESS #define UNCOMPRESS
#include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include "struct.h" #include "struct.h"
@@ -20,7 +20,8 @@ char ASTScan(ASTScanner *scanner, ASTNode **ret_node);
char ASTScanAll(ASTTree *tree, ASTScanner *scanner); char ASTScanAll(ASTTree *tree, ASTScanner *scanner);
/* return the number before a string with index */ /* return the number before a string with index */
unsigned int GetNumber(char *string, size_t start, size_t end, ASTString *other_string); unsigned int GetNumber(char *string, size_t start, size_t end,
ASTString *other_string);
/* Useful function to check whether a char is a number */ /* Useful function to check whether a char is a number */
bool IsNumber(const char c); bool IsNumber(const char c);