oo-70-245-1

oo-70-245-1 Commit Details


Date:2019-05-28 21:21:54 (5 years 4 months ago)
Author:Natalie Adams
Branch:master
Commit:a7e8a25570d3b4dd264e1ade50bb17b7d676ae77
Parents: 7a54e90e5b07055639e9b0b77ad13a44898e80d8
Message:updating samples

Changes:

File differences

Class Examples/mutex.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace mutexexample2
{
class Program
{
public static Mutex m = new Mutex();
public static readonly int LOOP = 50000;
public static long gvar = 0;
static public void produce()
{
m.WaitOne();
for (int i = 0; i < LOOP; i++)
gvar++;
m.ReleaseMutex();
}
static public void consume()
{
m.WaitOne();
for (int i = 0; i < LOOP; i++)
gvar--;
m.ReleaseMutex();
}
static void Main(string[] args)
{
Thread t1 = new Thread(new ThreadStart(produce));
t1.Start();
Thread t2 = new Thread(new ThreadStart(consume));
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine(gvar);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace mutexexample2
{
class Program
{
public static Mutex m = new Mutex();
public static readonly int LOOP = 50000;
public static long gvar = 0;
static public void produce()
{
m.WaitOne();
for (int i = 0; i < LOOP; i++)
gvar++;
m.ReleaseMutex();
}
static public void consume()
{
// Wait for the lock to be available
// If no other thread has it locked then it is
// by default available
m.WaitOne();
for (int i = 0; i < LOOP; i++)
gvar--;
// release/unlock the mutex lock
m.ReleaseMutex();
}
static void Main(string[] args)
{
// Example using a mutex lock
// Create 2 threads that use the same mutex lock variable
Thread t1 = new Thread(new ThreadStart(produce));
t1.Start();
Thread t2 = new Thread(new ThreadStart(consume));
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine(gvar);
}
}
}
Class Examples/node-generic.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace generics
{
class Program
{
class Node<T> where T : System.IComparable<T>
{
public T element;
public Node<T> left = null;
public Node<T> right = null;
Node<T> parent = null;
public Node<T> createNode(T element)
{
Node<T> n = new Node<T>() { parent = this, element = element };
return n;
}
public Node<T> addLeft(T element)
{
this.left = createNode(element);
return this.left;
}
public Node<T> addRight(T element)
{
this.right = createNode(element);
return this.right;
}
public override string ToString()
{
return "Node element => " + element.ToString();
}
}
static Node<T> dfs<T>(T val, Node<T> parent) where T : System.IComparable<T>
{
if (parent == null)
return null;
if (parent.element.CompareTo(val) == 0)
return parent;
Node<T> ret = null;
if (parent.left != null)
ret = dfs(val, parent.left);
if (ret != null)
return ret;
if (parent.right != null)
ret = dfs(val, parent.right);
if (ret != null)
return ret;
return null;
}
static void Main(string[] args)
{
Node<int> n = new Node<int>();
n.addLeft(4).addLeft(10).addLeft(7);
n.addRight(5).addRight(2);
Console.WriteLine(dfs(2, n));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace generics
{
class Program
{
class Node<T> where T : System.IComparable<T>
{
public T element;
public Node<T> left = null;
public Node<T> right = null;
Node<T> parent = null;
public Node<T> createNode(T element)
{
Node<T> n = new Node<T>() { parent = this, element = element };
return n;
}
public Node<T> addLeft(T element)
{
this.left = createNode(element);
return this.left;
}
public Node<T> addRight(T element)
{
this.right = createNode(element);
return this.right;
}
public override string ToString()
{
return "Node element => " + element.ToString();
}
}
static Node<T> dfs<T>(T val, Node<T> parent) where T : System.IComparable<T>
{
if (parent == null)
return null;
if (parent.element.CompareTo(val) == 0)
return parent;
Node<T> ret = null;
if (parent.left != null)
ret = dfs(val, parent.left);
if (ret != null)
return ret;
if (parent.right != null)
ret = dfs(val, parent.right);
if (ret != null)
return ret;
return null;
}
static void Main(string[] args)
{
// Binary tree but with any type for the object
Node<int> n = new Node<int>();
n.addLeft(4).addLeft(10).addLeft(7);
n.addRight(5).addRight(2);
Console.WriteLine(dfs(2, n));
}
}
}
Class Examples/node.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace generics
{
class Program
{
class Node
{
public int element;
public Node left = null;
public Node right = null;
Node parent = null;
public Node createNode(int element)
{
Node n = new Node() { parent = this, element = element };
return n;
}
public Node addLeft(int element)
{
this.left = createNode(element);
return this.left;
}
public Node addRight(int element)
{
this.right = createNode(element);
return this.right;
}
public override string ToString()
{
return "Node element => " + element.ToString();
}
}
static Node dfs(int val, Node parent)
{
if (parent == null)
return null;
if (parent.element == val)
return parent;
Node ret = null;
if (parent.left != null)
ret = dfs(val, parent.left);
if (ret != null)
return ret;
if (parent.right != null)
ret = dfs(val, parent.right);
if (ret != null)
return ret;
return null;
}
static void Main(string[] args)
{
Node n = new Node();
n.addLeft(4).addLeft(10).addLeft(7);
n.addRight(5).addRight(2);
Console.WriteLine(dfs(22, n));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace generics
{
class Program
{
class Node
{
public int element;
public Node left = null;
public Node right = null;
Node parent = null;
public Node createNode(int element)
{
Node n = new Node() { parent = this, element = element };
return n;
}
public Node addLeft(int element)
{
this.left = createNode(element);
return this.left;
}
public Node addRight(int element)
{
this.right = createNode(element);
return this.right;
}
public override string ToString()
{
return "Node element => " + element.ToString();
}
}
static Node dfs(int val, Node parent)
{
if (parent == null)
return null;
if (parent.element == val)
return parent;
Node ret = null;
if (parent.left != null)
ret = dfs(val, parent.left);
if (ret != null)
return ret;
if (parent.right != null)
ret = dfs(val, parent.right);
if (ret != null)
return ret;
return null;
}
static void Main(string[] args)
{
// Typical binary tree example
Node n = new Node();
n.addLeft(4).addLeft(10).addLeft(7);
n.addRight(5).addRight(2);
Console.WriteLine(dfs(22, n));
}
}
}
Class Examples/produce-consume.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace mutexexample2
{
class Program
{
public static readonly int LOOP = 50000;
public static long gvar = 0;
static public void produce()
{
for (int i = 0; i < LOOP; i++)
gvar++;
}
static public void consume()
{
for (int i = 0; i < LOOP; i++)
gvar--;
}
static void Main(string[] args)
{
Thread t1 = new Thread(new ThreadStart(produce));
t1.Start();
Thread t2 = new Thread(new ThreadStart(consume));
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine(gvar);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace mutexexample2
{
class Program
{
public static readonly int LOOP = 50000;
public static long gvar = 0;
static public void produce()
{
for (int i = 0; i < LOOP; i++)
gvar++;
}
static public void consume()
{
for (int i = 0; i < LOOP; i++)
gvar--;
}
static void Main(string[] args)
{
// Creating 2 threads with a simple race condition
Thread t1 = new Thread(new ThreadStart(produce));
t1.Start();
Thread t2 = new Thread(new ThreadStart(consume));
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine(gvar);
}
}
}
Class Examples/swap.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace generics
{
class Program
{
static void swap<T>(ref T lhs,ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
static void Main(string[] args)
{
int a, b;
a = 10;
b = 20;
Console.WriteLine(a.ToString() + " " + b.ToString());
swap(ref a, ref b);
Console.WriteLine(a.ToString() + " " + b.ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace generics
{
class Program
{
static void swap<T>(ref T lhs,ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
static void Main(string[] args)
{
// example of a templated/generic swap
int a, b;
a = 10;
b = 20;
Console.WriteLine(a.ToString() + " " + b.ToString());
swap(ref a, ref b);
Console.WriteLine(a.ToString() + " " + b.ToString());
}
}
}
Day 1 Class Examples/example1/Program.cs
33
44
55
6
7
8
9
610
711
812
......
1317
1418
1519
16
17
1820
1921
22
23
2024
2125
2226
using System.Linq;
using System.Text;
/*
String concantation example
*/
namespace example1
{
class Program
static void Main(string[] args)
{
Console.WriteLine("Hello world");
//Test t = new Test();
//t
string s = "hello, ";
s += "world";
Test t = new Test();
// Concat strings using plus or substitution
Console.WriteLine("hello, " + s + s + s + "some other string" + s + "something");
Console.WriteLine("hello, {0.2} {1} {2} {3}", s, s, s, s);
}
Day 1 Class Examples/example2/Program.cs
1313
1414
1515
16
1617
1718
1819
Console.WriteLine("Please enter 2 numbers");
n1 = Int32.Parse(Console.ReadLine());
n2 = Int32.Parse(Console.ReadLine());
// Basic arithmetic examples
Console.WriteLine("{0} + {1} = {2}", n1, n2, n1 + n2);
Console.WriteLine("{0} * {1} = {2}", n1, n2, n1 * n2);
Console.WriteLine("{0} / {1} = {2}", n1, n2, n1 / n2);
Day 1 Class Examples/example3/Program.cs
1212
1313
1414
15
1516
1617
1718
int tal = 0;
string tmp;
Console.WriteLine("Please enter 5 numbers:");
// Simple for loop
for (int i = 0; i < 5; i++)
{
tmp = Console.ReadLine();
Day 1 Class Examples/example4/Program.cs
2525
2626
2727
28
2829
2930
3031
int n2;
int n1;
Console.WriteLine("Please enter a number and a power:");
// Using console input and function calls to implment power
n1 = Int32.Parse(Console.ReadLine());
n2 = Int32.Parse(Console.ReadLine());
Math m = new Math();
Day 1 Class Examples/example5/Program.cs
1616
1717
1818
19
1920
2021
2122
}
static void Main(string[] args)
{
// Example of recursive function
Console.WriteLine("{0}", fact(3));
}
}
Day 2 Class Examples/example6/Program.cs
3636
3737
3838
39
39
4040
4141
4242
static void Main(string[] args)
{
internalexamples.
// Different examples of function calls
int x = 4;
f1(x);
Console.WriteLine(x);
Day 2 Class Examples/example7/Program.cs
1919
2020
2121
22
2223
2324
2425
......
2728
2829
2930
31
3032
3133
3234
class Ex2 : Example
{
// inheriting and overriding a method
public override void func()
{
base.func(x);
static void Main(string[] args)
{
// Example of modifying member variables
Example e = new Example();
e.test2 = 2;
}
Day 2 Class Examples/example9/Program.cs
2626
2727
2828
29
2930
3031
3132
{
static void Main(string[] args)
{
// example showing destructors vs dispose
{
DestructTest t2 = new DestructTest();
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.11156s using 14 queries.