ConsoleApplication4 cpp Defines the entry point for the console applic

 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
// ConsoleApplication4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int length = 5;
int i, j, k;
int arr_from[length] = { 1, 3, 0, 0, 2 };
int arr_to[length] = { 2, 2, 1, 3, 0 };
int res_from[length] = { 0 };
int res_to[length] = { 0 };
int head[length] = { 0 };
int body[length] = { 0 };
cout << "Graph:" << endl;
for (i = 0; i < length; ++i)
{
cout << arr_from[i] << " -> " << arr_to[i] << endl;
}
cout << endl;
for (i = 0; i < length; ++i)
{
for (j = k = 0; j < length; ++j)
if (arr_from[j] < arr_from[i] || (arr_from[i] == arr_from[j] && i < j)) k++;
res_from[k] = arr_from[i];
res_to[k] = arr_to[i];
}
cout << endl;
cout << "Sorted:" << endl;
for (i = 0; i < length; ++i)
{
cout << res_from[i] << " -> " << res_to[i] << endl;
}
cout << endl;
k = 0;
for (i = 0; i < length; ++i)
{
if (res_from[i] == res_from[i + 1])
{
head[k] = res_to[i];
body[k] = res_to[i + 1];
}
else
{
for (j = 0; j < length; ++j)
{
if (i == res_from[j])
{
head[k] = res_to[j];
body[k] = -1;
}
}
}
k++;
}
cout << endl;
cout << "List: " << endl;
for (i = 0; i < length; ++i)
{
cout << head[i] << " -> " << body[i] << endl;
}
return 0;
}