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 */
@@ -48,7 +48,7 @@ char ASTScan(ASTScanner *scanner, ASTNode **ret_node) {
* after the loop, i point after '(' or ')' */ * after the loop, i point after '(' or ')' */
i = scanner->current_index; i = scanner->current_index;
while (i < scanner->length) { while (i < scanner->length) {
if (scanner->string[i]=='(' || scanner->string[i] == ')') { if (scanner->string[i] == '(' || scanner->string[i] == ')') {
i++; i++;
break; break;
} }
@@ -56,21 +56,23 @@ char ASTScan(ASTScanner *scanner, ASTNode **ret_node) {
} }
/* if read ( */ /* if read ( */
if (scanner->string[i-1] == '(') { if (scanner->string[i - 1] == '(') {
node->type = 2; node->type = 2;
/* check is there is any number before ( */ /* check is there is any number before ( */
if (i-1 == 0) { if (i - 1 == 0) {
fprintf(stderr, "Syntax error: invailed value of repeat at begining\n"); fprintf(stderr, "Syntax error: invailed value of repeat at begining\n");
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 */
@@ -80,17 +82,17 @@ char ASTScan(ASTScanner *scanner, ASTNode **ret_node) {
} }
/* if read ) */ /* if read ) */
} else if (scanner->string[i-1] == ')') { } else if (scanner->string[i - 1] == ')') {
node->type = 3; node->type = 3;
/* if there are string before ) */ /* if there are string before ) */
if (i-1 != scanner->current_index) { if (i - 1 != scanner->current_index) {
/* change type to 1 */ /* change type to 1 */
node->type = 1; node->type = 1;
node->string.string = scanner->string; node->string.string = scanner->string;
node->string.start = scanner->current_index; node->string.start = scanner->current_index;
node->string.end = i-1; node->string.end = i - 1;
i = i - 1; i = i - 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,13 +134,11 @@ 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;
for (int i=1; i<y; i++) { for (int i = 1; i < y; i++) {
result = result * x; result = result * x;
} }
return result; return result;
@@ -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 */
@@ -226,14 +222,14 @@ void ASTCompileNode(ASTNode *root, FILE *file) {
if (currNode->type == 2) { if (currNode->type == 2) {
/* repeat to compile child node */ /* repeat to compile child node */
for (int i=0; i<currNode->repeat; i++) { for (int i = 0; i < currNode->repeat; i++) {
/* recursion */ /* recursion */
ASTCompileNode(currNode->child, file); ASTCompileNode(currNode->child, file);
} }
} else if (currNode->type == 1) { } else if (currNode->type == 1) {
/* print string */ /* print string */
for (size_t i=currNode->string.start; i<currNode->string.end; i++) { for (size_t i = currNode->string.start; i < currNode->string.end; i++) {
fputc(currNode->string.string[i], file); fputc(currNode->string.string[i], file);
} }
} }
@@ -259,7 +255,7 @@ char ASTScanAll(ASTTree *tree, ASTScanner *scanner) {
} }
/* scan until end */ /* scan until end */
while(err = ASTScan(scanner, &node), node) { while (err = ASTScan(scanner, &node), node) {
/* raise error */ /* raise error */
if (err != 0) { if (err != 0) {
@@ -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);