Skip to content

Commit 6f19f70

Browse files
committed
grabbing section 2
Merge branch 'main' of github.com:cs61/cs61-sections
2 parents d8f432e + db351ac commit 6f19f70

25 files changed

+378
-19
lines changed

datareps1/flapmap1.cc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "flapmap.hh"
2+
3+
int main() {
4+
printf("Hi! This program is a placeholder.\n");
5+
printf("If you want to experiment with `flapmap` code, either edit `flapmap1.cc` and `make`,\n");
6+
printf("or make a file called `flapmapNN.cc` and `make`.\n");
7+
}

datareps1/solutions/flapmaps5.cc

+6-7
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ bool can_coalesce_up(flapmap_iter it) {
2929
}
3030

3131
void coalesce_up(flapmap_iter it) {
32-
if (can_coalesce_up(it)) {
33-
auto next = it;
34-
++next;
35-
it->second.duration += next->second.duration;
36-
it->second.flapcount += next->second.flapcount;
37-
flapmap.erase(next);
38-
}
32+
assert(can_coalesce_up(it));
33+
auto next = it;
34+
++next;
35+
it->second.duration += next->second.duration;
36+
it->second.flapcount += next->second.flapcount;
37+
flapmap.erase(next);
3938
}
4039

4140
bool can_coalesce_down(flapmap_iter it) {

datareps1/solutions/flapmaps6.cc

+6-7
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ bool can_coalesce_up(flapmap_iter it) {
2929
}
3030

3131
void coalesce_up(flapmap_iter it) {
32-
if (can_coalesce_up(it)) {
33-
auto next = it;
34-
++next;
35-
it->second.duration += next->second.duration;
36-
it->second.flapcount += next->second.flapcount;
37-
flapmap.erase(next);
38-
}
32+
assert(can_coalesce_up(it));
33+
auto next = it;
34+
++next;
35+
it->second.duration += next->second.duration;
36+
it->second.flapcount += next->second.flapcount;
37+
flapmap.erase(next);
3938
}
4039

4140
bool can_coalesce_down(flapmap_iter it) {

datareps2/.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
membug[0-9]
2+
membug[0-9][0-9]
3+
[sbl][0-9]
4+
[sbl][0-9][0-9]
5+
ll[0-9]
6+
ll[0-9][0-9]
7+
hulk

datareps2/GNUmakefile

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# build all programs with names like `[sbl][0-9] [sbl][0-9][0-9]`
2+
XPROGRAMS = $(patsubst %.cc,%,$(wildcard [sbl][0-9].cc [sbl][0-9][0-9].cc ll[0-9].cc ll[0-9][0-9].cc))
3+
PROGRAMS = $(XPROGRAMS) hulk
4+
all: $(PROGRAMS)
5+
6+
ALLPROGRAMS = $(PROGRAMS) inv testinsert0 greet1
7+
8+
include ../common/rules.mk
9+
10+
LIBS = -lm
11+
12+
13+
# Rules for making object files (i.e., parts of executables)
14+
# from source files
15+
16+
%.o: %.cc $(BUILDSTAMP)
17+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(DEPCFLAGS) $(O) -o $@ -c $<
18+
19+
20+
# Rules for making executables (runnable programs) from object files
21+
22+
$(XPROGRAMS): \
23+
%: %.o hexdump.o
24+
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(O) -o $@ $^ $(LIBS)
25+
26+
hulk: \
27+
%: %.o hexdump.o
28+
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(O) -o $@ $^ $(LIBS)
29+
30+
31+
clean:
32+
rm -rf $(ALLPROGRAMS) *.o $(DEPSDIR)
33+
rm -rf $(wildcard [sbl][0-9] [sbl][0-9][0-9] ll[0-9] ll[0-9][0-9])
34+
35+
.PHONY: all clean

datareps2/b1.cc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <cstdio>
2+
3+
const char* f(int) {
4+
return "example";
5+
}
6+
7+
int main() {
8+
printf("%s\n", f(2));
9+
}

datareps2/b2.cc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <cstdio>
2+
3+
const char* f(int) {
4+
char buf[100] = "example";
5+
return buf;
6+
}
7+
8+
int main() {
9+
printf("%s\n", f(2));
10+
}

datareps2/b3.cc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <cstdio>
2+
3+
const char* f(int) {
4+
char buf[100] = "example";
5+
return buf;
6+
}
7+
8+
int main() {
9+
printf("%p\n", f(2));
10+
}

datareps2/b4.cc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <cstdio>
2+
#include <cstring>
3+
4+
const char* f(int) {
5+
char* buf = new char[100];
6+
strcpy(buf, "example");
7+
return buf;
8+
}
9+
10+
int main() {
11+
const char* s = f(2);
12+
printf("%s\n", s);
13+
delete[] s;
14+
}

datareps2/b5.cc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <cstdio>
2+
#include <cstring>
3+
4+
const char* f(int) {
5+
char* buf = new char[100];
6+
strcpy(buf, "example");
7+
return buf;
8+
}
9+
10+
int main() {
11+
const char* s = f(2);
12+
delete[] s;
13+
printf("%s\n", s);
14+
}

datareps2/b6.cc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <cstdio>
2+
#include <cstring>
3+
4+
const char* f(int) {
5+
char* buf = new char[strlen("example")];
6+
strcpy(buf, "example");
7+
return buf;
8+
}
9+
10+
int main() {
11+
const char* s = f(2);
12+
printf("%s\n", s);
13+
delete[] s;
14+
}

datareps2/hexdump.cc

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "hexdump.hh"
2+
#include <cassert>
3+
4+
static void fhexdump_ascii(FILE* f, const unsigned char* p, size_t pos);
5+
6+
void hexdump(const void* ptr, size_t size, const char* objname) {
7+
fhexdump_at(stdout, (size_t) ptr, ptr, size, objname);
8+
}
9+
10+
void fhexdump(FILE* f, const void* ptr, size_t size, const char* objname) {
11+
fhexdump_at(f, (size_t) ptr, ptr, size, objname);
12+
}
13+
14+
void fhexdump_at(FILE* f, size_t first_offset, const void* ptr, size_t size,
15+
const char* objname) {
16+
const char* addrfmt = objname ? " %08zx" : "%08zx";
17+
const unsigned char* p = (const unsigned char*) ptr;
18+
if (objname != nullptr) {
19+
fprintf(f, "%s:\n", objname);
20+
}
21+
for (size_t i = 0; i != size; ++i) {
22+
if (i % 16 == 0) {
23+
fprintf(f, addrfmt, first_offset + i);
24+
}
25+
fprintf(f, "%s%02x", (i % 8 == 0 ? " " : " "), (unsigned) p[i]);
26+
if (i % 16 == 15 || i == size - 1) {
27+
fhexdump_ascii(f, p, i);
28+
}
29+
}
30+
}
31+
32+
static void fhexdump_ascii(FILE* f, const unsigned char* p, size_t pos) {
33+
// Print an ASCII report that ends with byte p[pos].
34+
// The first byte printed is p[first], where first is the max multiple
35+
// of 16 having `first < pos`. The report starts at column 51.
36+
size_t first = pos - (pos % 16); // first char to print
37+
int n = pos + 1 - first; // # chars to print
38+
char buf[17];
39+
for (size_t i = first; i != first + n; ++i) {
40+
buf[i - first] = (p[i] >= 32 && p[i] < 127 ? p[i] : '.');
41+
}
42+
fprintf(f, "%*s|%.*s|\n", 51 - (3 * n + (n > 8)), "", n, buf);
43+
}

datareps2/hexdump.hh

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef CS61_HEXDUMP_HH
2+
#define CS61_HEXDUMP_HH
3+
#include <cstdio>
4+
#include <cassert>
5+
#include <cstdlib>
6+
7+
// hexdump(ptr, size)
8+
// Print a hexdump of the `size` bytes of data starting at `ptr`
9+
// to the standard output. The hexdump format is:
10+
// XXXXXXXX BB BB BB BB BB BB BB BB BB BB BB BB BB BB BB BB |CCCCCCCCCCCCCCCC|
11+
// where XXXXXXXX is the address of the first byte in the line,
12+
// the BBs are hexadecimal values of those bytes, and the Cs show
13+
// the printable ASCII characters corresponding to those bytes
14+
// (`.` is displayed for non-printable characters).
15+
void hexdump(const void* ptr, size_t size, const char* objname = nullptr);
16+
17+
// hexdump_object(object)
18+
// Like hexdump(&object, sizeof(object)).
19+
#define hexdump_object(object) hexdump(&(object), sizeof((object)))
20+
21+
// hexdump_named_object(object)
22+
// Like hexdump(&object, sizeof(object)), but also print a header with
23+
// the name of `object`.
24+
#define hexdump_named_object(object) hexdump(&(object), sizeof((object)), #object)
25+
26+
27+
// fhexdump(f, ptr, size)
28+
// Like `hexdump(ptr, size)`, but print to file `f` rather than standard
29+
// output.
30+
void fhexdump(FILE* f, const void* ptr, size_t size, const char* objname = nullptr);
31+
32+
// fhexdump_object(f, object)
33+
// Like `fhexdump(f, &object, sizeof(object))`.
34+
#define fhexdump_object(f, object) fhexdump((f), &(object), sizeof((object)))
35+
36+
// fhexdump_named_object(f, object)
37+
// Like `fhexdump(f, &object, sizeof(object))`.
38+
#define fhexdump_named_object(f, object) fhexdump((f), &(object), sizeof((object)), #object)
39+
40+
41+
// fhexdump_at(f, first_offset, ptr, size)
42+
// Like fhexdump, but start with offset `first_offset` instead of the
43+
// address of `ptr`.
44+
void fhexdump_at(FILE* f, size_t first_offset, const void* ptr, size_t size,
45+
const char* objname = nullptr);
46+
47+
#endif

datareps2/hulk.cc

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <cctype>
2+
#include <cstdio>
3+
#include <cstring>
4+
5+
const char* hulk_players[] = {
6+
"RUFFALO",
7+
"BANA",
8+
"FERRIGNO",
9+
"MIYAUCHI"
10+
};
11+
12+
void hulk_greeting(const char* name) {
13+
char uc_name[16];
14+
15+
// make uppercase version of `name`
16+
for (size_t i = 0; name[i] != 0; ++i) {
17+
uc_name[i] = toupper(name[i]);
18+
}
19+
uc_name[strlen(name)] = 0;
20+
printf("HULK SAY HELLO TO %s\n", uc_name);
21+
22+
// find player
23+
for (size_t i = 0; hulk_players[i] != nullptr; ++i) {
24+
if (strcmp(hulk_players[i], uc_name) == 0) {
25+
printf("YOU GOOD ACTOR\n");
26+
return;
27+
}
28+
}
29+
printf("YOU NO PLAY HULK! HULK SMASH\n");
30+
}
31+
32+
33+
int main(int argc, char* argv[]) {
34+
// Run `./hulk` to call `hulk_greeting("Ruffalo")`,
35+
// or `./hulk NAME` to call `hulk_greeting(NAME)`.
36+
if (argc > 1) {
37+
hulk_greeting(argv[1]);
38+
} else {
39+
hulk_greeting("Ruffalo");
40+
}
41+
return 0;
42+
}

datareps2/ll.hh

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef DATAREPS2_LL_HH
2+
#define DATAREPS2_LL_HH
3+
4+
struct node {
5+
int value;
6+
node* prev;
7+
node* next;
8+
};
9+
10+
extern node* head;
11+
12+
inline node* find(int v) {
13+
node* trav = head;
14+
while (trav && trav->value != v) {
15+
trav = trav->next;
16+
}
17+
return trav;
18+
}
19+
20+
#endif

datareps2/ll1.cc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "ll.hh"
2+
3+
node* head = nullptr;
4+
5+
int main() {
6+
printf("Hi! This program is a placeholder.\n");
7+
printf("If you want to experiment with linked list code, either edit `ll1.cc` and `make`,\n");
8+
printf("or make a file called `llNN.cc` and `make`.\n");
9+
}

datareps2/ll6.cc

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "ll.hh"
2+
3+
node* head = nullptr;
4+
5+
// Insert node `n` into the list immediately before node `before`.
6+
// If `before == nullptr`, append `n` to the list.
7+
void insert(node* before, node* n) {
8+
if (!before) {
9+
node* prev = head;
10+
while (prev->next != nullptr) {
11+
prev = prev->next;
12+
}
13+
n->prev = prev;
14+
n->next = nullptr;
15+
prev->next = n;
16+
} else {
17+
n->prev = before->prev;
18+
n->next = before;
19+
before->prev = n;
20+
}
21+
}
22+
23+
int main() {
24+
// Your code here to test `insert`
25+
}

datareps2/s1.cc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <cstdio>
2+
3+
int f(int x) {
4+
return x;
5+
}
6+
7+
int main() {
8+
printf("%d\n", f(2));
9+
}

datareps2/s2.cc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <cstdio>
2+
3+
int global = 0;
4+
5+
int* f(int) {
6+
return &global;
7+
}
8+
9+
int main() {
10+
int* ptr = f(2);
11+
printf("%d\n", *ptr);
12+
}

datareps2/s3.cc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <cstdio>
2+
3+
int* f(int x) {
4+
return &x;
5+
}
6+
7+
int main() {
8+
int* ptr = f(2);
9+
printf("%d\n", *ptr);
10+
}

0 commit comments

Comments
 (0)