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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 6 additions & 7 deletions
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

Lines changed: 6 additions & 7 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 35 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)