-
Notifications
You must be signed in to change notification settings - Fork 0
/
weaved.html
140 lines (93 loc) · 4.53 KB
/
weaved.html
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<h1>About the program</h1>
<p>This program reads a certain description of an undirected graph and
determines the number of connected components of it.</p>
<h2>About the description of the graph</h2>
<p>The graph shall be read from a file. The file describes
the graph by way of specifying the number of nodes, the
number of edges and a description of what nodes
each edge connects.</p>
<h3>Syntax</h3>
<p>The input file consists only of a series of arbitrarily space-
separated integer constants written in plain ASCII, and
consequently, also readable as an UTF-8 plain text file.</p>
<p>The first two integers shall be the number of nodes (n), and the
number of edges (m), respectively.</p>
<p>Given this, the set of nodes is assumed to be {1, 2, … n}, and
each node will be referred simply by its number.</p>
<p>Then comes “m” pairs of integers “u” and “v” each giving the
existence of a bidirectional edge between nodes “u” and “v”.</p>
<h2>The command line interface</h2>
<p>As a binary, our program shall receive one and only one command line argument, and this will
be interpreted as the file name for the input file described in #/about/interface/syntax.</p>
<p>If the input file doesn’t comply with the #/about/interface/syntax our program will fail silently.</p>
<h1>The program</h1>
<p>As this program will be considerably small, it will consist of a single source file
in which we will put all of our code.</p>
<h2>Source file “source/app.d”</h2>
<p>The one and only sourcefile. Will contain everything.
The structure of this file will be typical.</p>
<h2>< source/app.d > +=</h2>
<pre><code>> Import declarations
> Global variable definitions
> Function definitions
</code></pre>
<h2>< Import declarations > =</h2>
<p>In this fragment will be included the import declarations that we see
fit.</p>
<h2>< Global variable definitions > =</h2>
<p>In this fragment we will be creating and initializing global variables
as we require them.</p>
<h2>< Function definitions > =</h2>
<p>In this fragment we will be inserting our function definitions.</p>
<p>As per the rules of the D programming language, the order in which we insert new fragments
into the three previous fragments won’t affect the final result.</p>
<h2>The main function</h2>
<p>The main function will be the entry point to our program. This is the starting point of execution.
The structure of this function will be typical of a D program.</p>
<h3>< Function definitions > +=</h3>
<pre><code>void main(string[] args)
{
> Local variables definitions
> Main process
}
</code></pre>
<h3>< Local variables definitions > =</h3>
<p>In this fragment we will be inserting the local variables we require.</p>
<h3>< Main process > =</h3>
<p>In this fragment we will input how our main function does its processing.</p>
<p>Where <code>args</code> is an array of strings that contains the arguments we received from the user. The first argument is always the name of the binary,
the rest are given by the user.</p>
<h3>The process</h3>
<h4>< Main process > +=</h4>
<pre><code>> Commandline arguments validation
> Read the input
> Compute the answer
> Write the answer
</code></pre>
<h4>< Commandline arguments validation > =</h4>
<p>As it is normal in a program with command-line interface, our
first job shall be to validate the arguments that we are given.</p>
<h4>< Read the input > =</h4>
<p>Given that we have a valid argument giving us the input file name.
We will try to open the given file and get it’s data (ala Succ).
This fragment’s job will imply verifying the existence and syntax of
the given file.</p>
<h4>< Compute the answer > =</h4>
<p>With the information about the graph we will now be ready to compute the
number of connected components of it.</p>
<h4>< Write the answer > =</h4>
<p>Given the answer obtained in the previous fragment. We will present it to
the user through stdout.
\paragraph{Commandline arguments validation}
To do this, it is enough to verify that we have been given one and only one user-defined commandline argument.
The first given argument in “args” is always the name of the binary, so we need to verify that the args array
is of size 2 exactly.</p>
<h4>< Commandline arguments validation > +=</h4>
<pre><code>if (args.length != 2)
{
import std.stdio: stderr;
stderr.writeln("Expected an only argument giving an input file");
return;
}
</code></pre>
<p>Before reading the input we must know how</p>