• API
• FAQ
• Tools
• Archive
SHARE
TWEET

Untitled

a guest Sep 17th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import 'dart:io';
2.
3. /*
4.   Encapsulates the data model for the program.
5. */
7.   List<int> _relations = []; //Stores relationships between nodes as Adjacency Descriptive Integers.
8.   List<String> _data = []; //Stores the data about each node.
9.
10.   bool isInGraph({input: String}) => _data.contains(input);
11.   int nodeIndex({input: String}) => _data.indexOf(input);
12.
13.   /*
14.     Add a node to the graph.
15.   */
17.     if (isInGraph(input: name)) {
18.       throw Exception;
19.     }
20.     else {
23.     };
24.   }
25.
26.   /*
27.     Connect a node with and a list of other nodes.
28.     If the connecting nodes are not yet in the graph,
29.     add them to the end of the container.
30.   */
31.   void connectNodes({data: String, nodes: List}) {
32.     //If the subject data is not in the graph, add it.
33.     if (!isInGraph(input: data)) {
36.     };
37.
38.     //Make the connection using the magic of ADIs.
39.     for (int i = 0; i < nodes.length; i++) {
40.       int n = nodeIndex(input: data);
41.       if (isInGraph(input: nodes[i])) {
42.         int f = nodeIndex(input: nodes[i]);
43.         _relations[n] |= (1 << f);
44.       } else {
46.         int f = nodeIndex(input: nodes[i]);
47.         _relations[n] |= (1 << f);
48.       };
49.     };
50.   }
51.
52.   /*
53.     Print all nodes in the graph, in order of addition.
54.   */
55.   void printNodes(){
56.     for (String datum in _data){
57.       stdout.writeln(datum);
58.     };
59.   }
60.
61.   /*
62.     List nodes that share an edge with a given node.
63.   */
64.   void listConnections({node: String}) {
65.     if(isInGraph(input: node)) {
66.       int r = _relations[nodeIndex(input: node)];
67.       for (int i = 0; i < _data.length; i++) {
68.         if ((r & (1 << i)) != 0) {
69.           stdout.writeln(_data[i]);
70.         };
71.       };
72.       stdout.writeln();
73.     } else {
74.       throw Exception;
75.     };
76.   }
77.
78. } // END class ADIGraph
79.
80. /*
81.   Encapsulates the controller of the program.
82. */
83. class Commands {
85.     graph.printNodes();
86.   }
87.
89.     String candidate;
90.
91.     stdout.write('\nWho would you like to add to the graph?\nName: ');
93.
94.     try{
96.     } catch (e) {
97.       stdout.write('\$candidate is already a graph member.\n');
98.     };
99.
100.   }
101.
103.     stdout.write('\nWhose friends are these? ');
105.
106.     stdout.write('Please enter a friend\'s name: ');
107.     List<String> group = [];
110.     bool flag = true;
111.     while (flag) {
112.       stdout.write('Enter the next friend\'s name (press Enter to finish): ');
114.       if (friend.isNotEmpty) {
116.       } else {
117.         flag = false;
118.       };
119.     };
120.
121.     graph.connectNodes(data: subject, nodes: group);
122.     stdout.write('Done!\n');
123.   }
124.
126.     stdout.writeln('\nWhose friends do you want to see? ');
128.     try {
129.       stdout.writeln('\$subject\'s friends are:');
130.       graph.listConnections(node: subject);
131.     } catch (e) {
132.       stdout.writeln('Error: \$subject is not part of the graph.');
133.     };
134.   }
135.
137.     stdout.write('\n\'a\': Add someone to the graph. \n\'c\': Connect friends to graph member. \n\'f\': Print someone\'s friend list. \n\'p\': Print graph members. \n\'m\': View this menu. \n\'q\': Quit the program.\n\n');
138.   }
139.
140. } //END class Commands
141.
142. void RunProgram({input: Object}){
143.   Commands menu = new Commands();
144.   String command;
145.
146.   while (command != 'q') {
147.
148.     stdout.writeln('\n'+('-'*60));
149.     stdout.write('Welcome to the Social Graph. What would you like to do? \n(enter \'m\' to view the menu and \'q\' to quit): ');
151.
152.     switch (command) {
153.       case 'a':
155.         break;
156.       case 'c':
158.         break;
159.       case 'f':
161.         break;
162.       case 'm':
164.         break;
165.       case 'p':
167.         break;
168.     };
169.   };
170. }
171.
172. void main() {