Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Lab/lab00/first_set.txt
Original file line number Diff line number Diff line change
@@ -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
Binary file added Lab/lab01/eccentri2c.out
Binary file not shown.
10 changes: 5 additions & 5 deletions Lab/lab01/eccentric.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <stdio.h>

/* 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;
Expand Down Expand Up @@ -55,4 +55,4 @@ int main(void) {
}

return 0;
}
}
Binary file added Lab/lab01/eccentric.out
Binary file not shown.
39 changes: 30 additions & 9 deletions Lab/lab01/gdb.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
1.
2.
3.
4.
5.
6.
7.
8.
9.
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
3 changes: 2 additions & 1 deletion Lab/lab01/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ int main(int argc, char *argv[]) {

printf("Thanks for waddling through this program. Have a nice day.");
return 0;
}
}

1 change: 1 addition & 0 deletions Lab/lab01/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Gingko
2 changes: 1 addition & 1 deletion Lab/lab01/interactive_hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
21 changes: 19 additions & 2 deletions Lab/lab01/ll_cycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,22 @@

int ll_has_cycle(node *head) {
/* your code here */
return 0;
}
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;
}
Empty file added Project/proj1/hidden_pc.ppm
Empty file.
68 changes: 64 additions & 4 deletions Project/proj1/imageloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,83 @@
#include <inttypes.h>
#include <string.h>
#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;i<img->rows;i++)
{
img->image[i]=(Color*)malloc(sizeof(Color)*(img->cols));
for(int j=0;j<img->cols;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;i<image->rows;i++)
{
for(int j=0;j<image->cols;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
}
for(int i=0;i<image->rows;i++)
free(image->image[i]);

free(image->image);
free(image);
return ;
}
2 changes: 1 addition & 1 deletion Project/proj1/imageloadertester.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ int main(int argc, char **argv)
image = readData(filename);
writeData(image);
freeImage(image);
}
}
Empty file added Project/proj1/output.ppm
Empty file.
Empty file added Project/proj1/output.txt
Empty file.
67 changes: 64 additions & 3 deletions Project/proj1/steganography.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;i<rows;i++)
{
img->image[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;j<cols;j++)
{
Color *nC=evaluateOnePixel(image,i,j);
if(NULL==nC){
for (int k = 0; k <= i; k++)
free(img->image[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;
}

/*
Expand All @@ -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;
}