Skip to content

Commit d517478

Browse files
committed
update Chapter 4
1 parent ec425af commit d517478

8 files changed

+189
-0
lines changed

week04/Lecture04.pptx

23.5 KB
Binary file not shown.

week04/array-illustration.xlsx

11.4 KB
Binary file not shown.

week04/examples/enum.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
enum color {WHITE, BLACK, RED, GREEN, BLUE, YELLOW, NUM_COLORS};
5+
enum datatype {TYPE_INT8=1, TYPE_INT16=2, TYPE_INT32=4, TYPE_INT64=8};
6+
7+
struct Point{
8+
enum datatype type;
9+
union {
10+
std::int8_t data8[3];
11+
std::int16_t data16[3];
12+
std::int32_t data32[3];
13+
std::int64_t data64[3];
14+
};
15+
};
16+
17+
size_t datawidth(struct Point pt)
18+
{
19+
return size_t(pt.type) * 3;
20+
}
21+
22+
int64_t l1norm(struct Point pt)
23+
{
24+
int64_t result = 0;
25+
switch(pt.type)
26+
{
27+
case (TYPE_INT8):
28+
result = abs(pt.data8[0]) + abs(pt.data8[1]) + abs(pt.data8[2]);
29+
break;
30+
case (TYPE_INT16):
31+
result = abs(pt.data16[0]) + abs(pt.data16[1]) + abs(pt.data16[2]);
32+
break;
33+
case (TYPE_INT32):
34+
result = abs(pt.data32[0]) + abs(pt.data32[1]) + abs(pt.data32[2]);
35+
break;
36+
case (TYPE_INT64):
37+
result = abs(pt.data64[0]) + abs(pt.data64[1]) + abs(pt.data64[2]);
38+
break;
39+
}
40+
return result;
41+
}
42+
43+
int main()
44+
{
45+
enum color pen_color = RED;
46+
pen_color = color(3); //convert int to enum
47+
cout << "We have " << NUM_COLORS << " pens." << endl;
48+
//pen_color += 1; //error!
49+
int color_index = pen_color;
50+
color_index += 1;
51+
cout << "color_index = " << color_index << endl;
52+
53+
//declaration and initialization
54+
struct Point point1 = {.type=TYPE_INT8, .data8={-2,3,4}};
55+
struct Point point2 = {.type=TYPE_INT32, .data32={1,-2,3}};
56+
57+
cout << "Data width = " << datawidth(point1) << endl;
58+
cout << "Data width = " << datawidth(point2) << endl;
59+
60+
cout << "L1 norm = " << l1norm(point1) << endl;
61+
cout << "L1 norm = " << l1norm(point2) << endl;
62+
63+
64+
return 0;
65+
}

week04/examples/stringelement.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
int main()
6+
{
7+
string str = "Hello, SUSTech!";
8+
for(int i = 0; i <= str.length(); i++) //no bound check
9+
cout << i << ": " << str[i] << endl;
10+
11+
return 0;
12+
}

week04/examples/struct.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdbool.h>
4+
5+
typedef
6+
struct _Student{
7+
char name[4];
8+
int born;
9+
bool male;
10+
} Student;
11+
12+
int main()
13+
{
14+
Student stu = {"Yu", 2000, true}; //initialization
15+
// strcpy(stu.name, "Yu");
16+
// stu.born = 2000;
17+
// stu.male = true;
18+
19+
printf("Student %s, born in %d, gender %s\n",
20+
stu.name,
21+
stu.born,
22+
stu.male ? "male" : "female");
23+
24+
Student students[100];
25+
students[50].born = 2002;
26+
27+
return 0;
28+
}

week04/examples/structpadding.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
struct Student1{
5+
int id;
6+
bool male;
7+
char label;
8+
float height;
9+
};
10+
11+
struct Student2{
12+
int id;
13+
bool male;
14+
float height;
15+
char label;
16+
};
17+
18+
int main()
19+
{
20+
cout << "Size of Student1: " << sizeof(Student1) << endl;
21+
cout << "Size of Student2: " << sizeof(Student2) << endl;
22+
return 0;
23+
}

week04/examples/typedef.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
typedef int myint;
5+
typedef unsigned char vec3b[3];
6+
typedef struct _rgb_struct{ // name _rgb_struct can be omit
7+
unsigned char r;
8+
unsigned char g;
9+
unsigned char b;
10+
} rgb_struct;
11+
12+
int main()
13+
{
14+
myint num = 32;
15+
16+
// the following two lines are identical
17+
//unsigned char color[3] = {255, 0, 255};
18+
vec3b color = {255, 0, 255};
19+
cout << hex;
20+
cout << "R=" << +color[0] << ", ";
21+
cout << "G=" << +color[1] << ", ";
22+
cout << "B=" << +color[2] << endl;
23+
24+
rgb_struct rgb = {0, 255, 128};
25+
cout << "R=" << +rgb.r << ", ";
26+
cout << "G=" << +rgb.g << ", ";
27+
cout << "B=" << +rgb.b << endl;
28+
29+
cout << sizeof(rgb.r) << endl;
30+
cout << sizeof(+rgb.r) << endl; //why 4?
31+
32+
return 0;
33+
}

week04/examples/union.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
union ipv4address{
5+
std::uint32_t address32;
6+
std::uint8_t address8[4];
7+
};
8+
9+
int main()
10+
{
11+
union ipv4address ip;
12+
13+
cout << "sizeof(ip) = " << sizeof(ip) << endl;
14+
15+
ip.address8[3] = 127;
16+
ip.address8[2] = 0;
17+
ip.address8[1] = 0;
18+
ip.address8[0] = 1;
19+
20+
cout << "The address is " ;
21+
cout << +ip.address8[3] << ".";
22+
cout << +ip.address8[2] << ".";
23+
cout << +ip.address8[1] << ".";
24+
cout << +ip.address8[0] << endl;
25+
26+
cout << std::hex;
27+
cout << "in hex " << ip.address32 << endl;
28+
}

0 commit comments

Comments
 (0)