diff --git a/Lab/lab00/first_set.txt b/Lab/lab00/first_set.txt index 6c6d52e..4de5b05 100644 --- a/Lab/lab00/first_set.txt +++ b/Lab/lab00/first_set.txt @@ -1,4 +1,28 @@ +first_set: Which command walks a file hierarchy in search of a keyword? +find -r [file] +grep -r [keyword] [directory] + Which command displays information about processes running on your machine? +jobs只显示当前shell的作业 +ps\top\htop显示进程 + Which command terminates a process? +kill [pid]/pkill [processname] + Which command can help you find the difference between two files? +diff [file1] [file2] + +second set: +Move files from one directory to the next +mv [addr1] [addr1] +Change a file’s permissions +chmod +x [file] +Show the directory you’re currently in +pwd +Check if a host is online +ping? + +third set: +Show file permissions for all files in a directory +ls -la \ No newline at end of file diff --git a/Lab/lab01/eccentri2c.out b/Lab/lab01/eccentri2c.out new file mode 100644 index 0000000..44d8b15 Binary files /dev/null and b/Lab/lab01/eccentri2c.out differ diff --git a/Lab/lab01/eccentric.c b/Lab/lab01/eccentric.c index 0564266..3d2996e 100644 --- a/Lab/lab01/eccentric.c +++ b/Lab/lab01/eccentric.c @@ -1,10 +1,10 @@ #include /* Only change any of these 4 values */ -#define V0 0 -#define V1 -1 -#define V2 0 -#define V3 0 +#define V0 3 +#define V1 3 +#define V2 1 +#define V3 3 int main(void) { int a; @@ -55,4 +55,4 @@ int main(void) { } return 0; -} \ No newline at end of file +} diff --git a/Lab/lab01/eccentric.out b/Lab/lab01/eccentric.out new file mode 100644 index 0000000..b44a312 Binary files /dev/null and b/Lab/lab01/eccentric.out differ diff --git a/Lab/lab01/gdb.txt b/Lab/lab01/gdb.txt index ce9d903..ccd68f3 100644 --- a/Lab/lab01/gdb.txt +++ b/Lab/lab01/gdb.txt @@ -1,9 +1,30 @@ -1. -2. -3. -4. -5. -6. -7. -8. -9. \ No newline at end of file +1.While you’re in a gdb session, how do you set the arguments that will be passed to the program when it’s run? +before run the program, input "set args arg1 arg2 ..." + +2.How do you create a breakpoint? +break [funName/line_number] + +3.How do you execute the next line of C code in the program after stopping at a breakpoint? +next + +4.If the next line of code is a function call, you’ll execute the whole function +call at once if you use your answer to # (If not, consider a different command for #3!) +How do you tell GDB that you want to debug the code inside the function +(i.e. step into the function) instead? (If you changed your answer to #3, +then that answer is most likely now applicable here.) +step + +5.How do you continue the program after stopping at a breakpoint? +continue/next + +6.How can you print the value of a variable (or even an expression like 1+2) in gdb? +display 会在每次step显示变量,而print只是打印一次 + +7.How do you configure gdb so it displays the value of a variable after every step? +display + +8.How do you show a list of all variables and their values in the current function? +info locals + +9.How do you quit out of gdb? +q diff --git a/Lab/lab01/hello.c b/Lab/lab01/hello.c index 0cf4fb5..cf48d36 100644 --- a/Lab/lab01/hello.c +++ b/Lab/lab01/hello.c @@ -11,4 +11,5 @@ int main(int argc, char *argv[]) { printf("Thanks for waddling through this program. Have a nice day."); return 0; -} \ No newline at end of file +} + diff --git a/Lab/lab01/input.txt b/Lab/lab01/input.txt new file mode 100644 index 0000000..7747fa0 --- /dev/null +++ b/Lab/lab01/input.txt @@ -0,0 +1 @@ +Gingko \ No newline at end of file diff --git a/Lab/lab01/interactive_hello.c b/Lab/lab01/interactive_hello.c index ab01357..95a103a 100644 --- a/Lab/lab01/interactive_hello.c +++ b/Lab/lab01/interactive_hello.c @@ -10,4 +10,4 @@ int main(int argc, char *argv[]) { printf("Hey, %sI just really wanted to say hello to you.\nI hope you have a wonderful day.", a_word); return 0; -} \ No newline at end of file +} diff --git a/Lab/lab01/ll_cycle.c b/Lab/lab01/ll_cycle.c index 8e27c0d..30fbc55 100644 --- a/Lab/lab01/ll_cycle.c +++ b/Lab/lab01/ll_cycle.c @@ -3,5 +3,22 @@ int ll_has_cycle(node *head) { /* your code here */ - return 0; -} \ No newline at end of file + node* tortoise=head,*hare=head; + int is_cyclic=0; + while(hare!=NULL) + { + hare=hare->next; + if(NULL==hare) + break; + + hare=hare->next; + tortoise=tortoise->next; + + if(hare==tortoise) + { + is_cyclic=1; + break; + } + } + return is_cyclic; +} diff --git a/Project/proj1/hidden_pc.ppm b/Project/proj1/hidden_pc.ppm new file mode 100644 index 0000000..e69de29 diff --git a/Project/proj1/imageloader.c b/Project/proj1/imageloader.c index 9b508c7..837c5e3 100644 --- a/Project/proj1/imageloader.c +++ b/Project/proj1/imageloader.c @@ -19,23 +19,83 @@ #include #include #include "imageloader.h" +#define DEBUG //Opens a .ppm P3 image file, and constructs an Image object. //You may find the function fscanf useful. //Make sure that you close the file with fclose before returning. + +char buf[100]; Image *readData(char *filename) { - //YOUR CODE HERE + Image* img=(Image*)malloc(sizeof(Image)); + int scale; + + FILE *fp = fopen(filename,"r"); + if(!fp){ + perror("fopen failed"); + free(img); + return NULL; + } + + fscanf(fp,"%s",buf); + fscanf(fp,"%" SCNu32 " %" SCNu32 ,&img->cols,&img->rows); + fscanf(fp,"%" SCNu8 ,&scale); + + img->image=(Color**)malloc(sizeof(Color*)*(img->rows)); + +#ifdef DEBUG + printf("width=%d,height=%d,scale=%d\n",img->cols,img->rows,scale); +#endif + + // start reading pixels + for(int i=0;irows;i++) + { + img->image[i]=(Color*)malloc(sizeof(Color)*(img->cols)); + for(int j=0;jcols;j++) + { + fscanf(fp,"%" SCNu8 " %" SCNu8 " %" SCNu8 , + &img->image[i][j].R, \ + &img->image[i][j].G, \ + &img->image[i][j].B); + } + } + + fclose(fp); + return img; } //Given an image, prints to stdout (e.g. with printf) a .ppm P3 file with the image's data. void writeData(Image *image) { - //YOUR CODE HERE + printf("P3\n"); + printf("%"PRIu32" %"PRIu32"\n",image->cols,image->rows); + printf("255\n"); + for(int i=0;irows;i++) + { + for(int j=0;jcols;j++) + { + printf("%3" PRIu8" %3" PRIu8 " %3" PRIu8 , + image->image[i][j].R, \ + image->image[i][j].G, \ + image->image[i][j].B); + fflush(stdout); + if(j!=image->cols-1) + printf(" "); + } + printf("\n"); + } + + return; } //Frees an image void freeImage(Image *image) { - //YOUR CODE HERE -} \ No newline at end of file + for(int i=0;irows;i++) + free(image->image[i]); + + free(image->image); + free(image); + return ; +} diff --git a/Project/proj1/imageloadertester.c b/Project/proj1/imageloadertester.c index 755eee7..a8dd1a7 100644 --- a/Project/proj1/imageloadertester.c +++ b/Project/proj1/imageloadertester.c @@ -35,4 +35,4 @@ int main(int argc, char **argv) image = readData(filename); writeData(image); freeImage(image); -} \ No newline at end of file +} diff --git a/Project/proj1/output.ppm b/Project/proj1/output.ppm new file mode 100644 index 0000000..e69de29 diff --git a/Project/proj1/output.txt b/Project/proj1/output.txt new file mode 100644 index 0000000..e69de29 diff --git a/Project/proj1/steganography.c b/Project/proj1/steganography.c index 07b7d57..878a6e3 100644 --- a/Project/proj1/steganography.c +++ b/Project/proj1/steganography.c @@ -21,13 +21,60 @@ //Determines what color the cell at the given row/col should be. This should not affect Image, and should allocate space for a new Color. Color *evaluateOnePixel(Image *image, int row, int col) { - //YOUR CODE HERE + Color *newColor = (Color *)malloc(sizeof(Color)); + if (newColor == NULL) { + return NULL; + } + uint8_t bp = (image->image[row][col].B & 1) ? 0xFF : 0x00; + newColor->R=bp; + newColor->G=bp; + newColor->B=bp; + return newColor; } //Given an image, creates a new image extracting the LSB of the B channel. Image *steganography(Image *image) { - //YOUR CODE HERE + Image* img=malloc(sizeof(Image)); + if(NULL==img){ + return NULL; + } + + int cols=image->cols,rows=image->rows; + img->image=malloc(rows*sizeof(void*)); + if(NULL==img->image){ + free(img); + return NULL; + } + img->cols=cols,img->rows=rows; + for(int i=0;iimage[i]=malloc(sizeof(Color)*cols); + if(NULL==img->image[i]){ + for (int k = 0; k <= i; k++) { + free(img->image[k]); + } + free(img->image); + free(img); + return NULL; + } + for(int j=0;jimage[k]); + free(img->image); + free(img); + return NULL; + } + img->image[i][j].R=nC->R; + img->image[i][j].G=nC->G; + img->image[i][j].B=nC->B; + free(nC); + } + } + return img; } /* @@ -45,5 +92,19 @@ Make sure to free all memory before returning! */ int main(int argc, char **argv) { - //YOUR CODE HERE + if(argc!=2){ + printf("usage: %s filename\n",argv[0]); + printf("filename is an ASCII PPM file (type P3) with maximum value 255.\n"); + return -1; + } + + char*filename=argv[1]; + + Image* ppm=readData(filename); + Image* hidden_ppm=steganography(ppm); + writeData(hidden_ppm); + freeImage(hidden_ppm); + freeImage(ppm); + + return 0; }