Skip to content

Commit a3598c3

Browse files
ClémentClément
authored andcommitted
Tidying source code.
1 parent f0466dc commit a3598c3

2 files changed

Lines changed: 71 additions & 75 deletions

File tree

source/code/projects/CAStack/CAStack/CAStack.cs

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,87 +2,82 @@
22

33
class CAStack<T>
44
{
5+
private T[] mArray;
56

6-
private T[] mArray;
7-
// Contains the number of the next empty cell.
8-
private int top = 0;
7+
// Contains the number of the next empty cell.
8+
private int top = 0;
99

10-
// Our default stack size is 10.
11-
public CAStack(int sizeP = 10)
12-
{
13-
if (sizeP <= 0)
14-
throw new ApplicationException("Stack size must be strictly greater than 0.");
15-
else
16-
mArray = new T[sizeP];
17-
}
10+
// Our default stack size is 10.
11+
public CAStack(int sizeP = 10)
12+
{
13+
if (sizeP <= 0)
14+
throw new ApplicationException(
15+
"Stack size must be strictly greater than 0."
16+
);
17+
else
18+
mArray = new T[sizeP];
19+
}
1820

19-
public void Clear()
20-
{
21-
top = 0;
22-
}
21+
public void Clear()
22+
{
23+
top = 0;
24+
}
2325

24-
public bool IsEmpty()
25-
{
26-
return top == 0;
27-
}
26+
public bool IsEmpty()
27+
{
28+
return top == 0;
29+
}
2830

29-
public void Push(T value)
31+
public void Push(T value)
32+
{
33+
if (top < mArray.Length)
3034
{
31-
if (top < mArray.Length)
32-
{
33-
mArray[top] = value;
34-
top++;
35-
}
36-
else
37-
{
38-
throw new ApplicationException("Stack is full.");
39-
}
35+
mArray[top] = value;
36+
top++;
4037
}
41-
42-
public T Pop()
38+
else
4339
{
44-
if (IsEmpty())
45-
throw new ApplicationException(
46-
"An empty stack cannot be popped."
47-
);
48-
return mArray[--top];
40+
throw new ApplicationException("Stack is full.");
4941
}
42+
}
5043

44+
public T Pop()
45+
{
46+
if (IsEmpty())
47+
throw new ApplicationException(
48+
"An empty stack cannot be popped."
49+
);
50+
return mArray[--top];
51+
}
5152

52-
public T Peek()
53-
{
54-
if (IsEmpty())
55-
throw new ApplicationException(
56-
"An empty stack cannot be peeked."
57-
);
58-
return mArray[top - 1];
59-
}
60-
61-
public int Count
62-
{
63-
get
64-
{
65-
return top;
66-
}
67-
}
53+
public T Peek()
54+
{
55+
if (IsEmpty())
56+
throw new ApplicationException(
57+
"An empty stack cannot be peeked."
58+
);
59+
return mArray[top - 1];
60+
}
6861

62+
public int Count
63+
{
64+
get { return top; }
65+
}
6966

70-
public override string ToString()
67+
public override string ToString()
68+
{
69+
string returned = "";
70+
if (!IsEmpty())
7171
{
72-
string returned = "";
73-
if (!IsEmpty())
74-
{
75-
int counter = top-1;
76-
while (counter >= 0)
77-
{
78-
if (returned.Length > 0)
79-
returned += ":";
80-
returned += mArray[counter];
81-
counter--;
82-
}
83-
}
84-
return returned;
85-
72+
int counter = top - 1;
73+
while (counter >= 0)
74+
{
75+
if (returned.Length > 0)
76+
returned += ":";
77+
returned += mArray[counter];
78+
counter--;
79+
}
8680
}
87-
81+
return returned;
82+
}
8883
}

source/code/projects/CAStack/CAStack/Program.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ static void Main(string[] args)
1717
myStack1.Push(4);
1818
myStack1.Push(5);
1919
myStack1.Push(5);
20-
try
21-
{
22-
myStack1.Push(5);
23-
} catch(Exception ex)
24-
{
25-
Console.WriteLine(ex.Message);
26-
}
20+
try
21+
{
22+
myStack1.Push(5);
23+
}
24+
catch (Exception ex)
25+
{
26+
Console.WriteLine(ex.Message);
27+
}
2728
Console.WriteLine(myStack1);
2829

2930
/* Second example. */

0 commit comments

Comments
 (0)