Assignment 1 correct
This commit is contained in:
@@ -1,16 +1,79 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "uncompress.h"
|
#include "uncompress.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Uncompress(NULL, stdout);
|
int cnt = 0;
|
||||||
Uncompress("", stdout);
|
char* str = NULL;
|
||||||
Uncompress("abc", stdout);
|
char* res = NULL;
|
||||||
Uncompress("a1(b)c", stdout);
|
|
||||||
Uncompress("a11(c)d", stdout);
|
printf("input: %s\n", str);
|
||||||
Uncompress("3(a)2(bc)", stdout);
|
res = Uncompress(str);
|
||||||
Uncompress("3(a2(c))", stdout);
|
printf("output: %s\n", res);
|
||||||
Uncompress("2(abb3(cd))ef", stdout);
|
printf("judge: %s\n\n", res==NULL?"True":"False");
|
||||||
return 0;
|
if(res==NULL)++cnt;
|
||||||
|
|
||||||
|
str = "";
|
||||||
|
printf("input: %s\n", str);
|
||||||
|
res = Uncompress(str);
|
||||||
|
printf("output: %s\n", res);
|
||||||
|
printf("judge: %s\n\n", strcmp(res, "")==0?"True":"False");
|
||||||
|
if(strcmp(res, "")==0)++cnt;
|
||||||
|
|
||||||
|
str = "abc";
|
||||||
|
printf("input: %s\n", str);
|
||||||
|
res = Uncompress(str);
|
||||||
|
printf("output: %s\n", res);
|
||||||
|
printf("judge: %s\n\n", strcmp(res, "abc")==0?"True":"False");
|
||||||
|
if(strcmp(res, "abc")==0)++cnt;
|
||||||
|
|
||||||
|
str = "a1(b)c";
|
||||||
|
printf("input: %s\n", str);
|
||||||
|
res = Uncompress(str);
|
||||||
|
printf("output: %s\n", res);
|
||||||
|
printf("judge: %s\n\n", strcmp(res, "abc")==0?"True":"False");
|
||||||
|
if(strcmp(res, "abc")==0)++cnt;
|
||||||
|
|
||||||
|
str = "a11(c)d";
|
||||||
|
printf("input: %s\n", str);
|
||||||
|
res = Uncompress(str);
|
||||||
|
printf("output: %s\n", res);
|
||||||
|
printf("judge: %s\n\n", strcmp(res, "acccccccccccd")==0?"True":"False");
|
||||||
|
if(strcmp(res, "acccccccccccd")==0)++cnt;
|
||||||
|
|
||||||
|
str = "3(a)2(bc)";
|
||||||
|
printf("input: %s\n", str);
|
||||||
|
res = Uncompress(str);
|
||||||
|
printf("output: %s\n", res);
|
||||||
|
printf("judge: %s\n\n", strcmp(res, "aaabcbc")==0?"True":"False");
|
||||||
|
if(strcmp(res, "aaabcbc")==0)++cnt;
|
||||||
|
|
||||||
|
str = "3(a2(c))";
|
||||||
|
printf("input: %s\n", str);
|
||||||
|
res = Uncompress(str);
|
||||||
|
printf("output: %s\n", res);
|
||||||
|
printf("judge: %s\n\n", strcmp(res, "accaccacc")==0?"True":"False");
|
||||||
|
if(strcmp(res, "accaccacc")==0)++cnt;
|
||||||
|
|
||||||
|
str = "2(abb3(cd))ef";
|
||||||
|
printf("input: %s\n", str);
|
||||||
|
res = Uncompress(str);
|
||||||
|
printf("output: %s\n", res);
|
||||||
|
printf("judge: %s\n\n", strcmp(res, "abbcdcdcdabbcdcdcdef")==0?"True":"False");
|
||||||
|
if(strcmp(res, "abbcdcdcdabbcdcdcdef")==0)++cnt;
|
||||||
|
|
||||||
|
switch(cnt){
|
||||||
|
case 8:printf("A\n");break;
|
||||||
|
case 7:
|
||||||
|
case 6:printf("B\n");break;
|
||||||
|
case 5:printf("C\n");break;
|
||||||
|
case 4:
|
||||||
|
case 3:
|
||||||
|
case 2:
|
||||||
|
case 1:printf("D\n");break;
|
||||||
|
default:printf("F\n");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -218,7 +218,7 @@ void ASTCompileTree(ASTTree *tree, FILE *file) {
|
|||||||
ASTCompileNode(tree->root, file);
|
ASTCompileNode(tree->root, file);
|
||||||
|
|
||||||
/* new line at the end */
|
/* new line at the end */
|
||||||
fputc('\n', file);
|
//fputc('\n', file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ASTCompileNode(ASTNode *root, FILE *file) {
|
void ASTCompileNode(ASTNode *root, FILE *file) {
|
||||||
@@ -299,7 +299,10 @@ char ASTScanAll(ASTTree *tree, ASTScanner *scanner) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Uncompress(char *string, FILE *file) {
|
char* Uncompress(char *string) {
|
||||||
|
char *result = (char *)calloc(10000, sizeof(char));
|
||||||
|
char *ret = (char *)calloc(10000, sizeof(char));
|
||||||
|
FILE *file = fmemopen(result, 10000, "w");
|
||||||
char err;
|
char err;
|
||||||
|
|
||||||
/* create scanner and AST tree */
|
/* create scanner and AST tree */
|
||||||
@@ -325,6 +328,17 @@ void Uncompress(char *string, FILE *file) {
|
|||||||
/* free memory */
|
/* free memory */
|
||||||
DestroyASTTree(tree);
|
DestroyASTTree(tree);
|
||||||
DestroyASTScanner(scanner);
|
DestroyASTScanner(scanner);
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
file = fmemopen(result, strlen(result), "r");
|
||||||
|
char ch;
|
||||||
|
int i=0;
|
||||||
|
while((ch = fgetc(file)) != EOF) {
|
||||||
|
ret[i++] = ch;
|
||||||
|
}
|
||||||
|
ret[i] = 0;
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DestroyASTScanner(ASTScanner *scanner) { free(scanner); }
|
void DestroyASTScanner(ASTScanner *scanner) { free(scanner); }
|
||||||
|
|||||||
@@ -74,6 +74,6 @@ void ASTCompileTree(ASTTree *tree, FILE *file);
|
|||||||
void ASTCompileNode(ASTNode *root, FILE *file);
|
void ASTCompileNode(ASTNode *root, FILE *file);
|
||||||
|
|
||||||
/* A short cut function, directly uncompress the raw string to file stream */
|
/* A short cut function, directly uncompress the raw string to file stream */
|
||||||
void Uncompress(char *string, FILE *file);
|
char* Uncompress(char *string);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user