-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
169 lines (125 loc) · 3.25 KB
/
Program.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
namespace GaleShapely
{
class Person
{
public string[] Preference_List { get; set; }
public Queue<string> prefQueue { get; set; }
public Person Partner { get; set; }
public bool Married;
public int name;
public string[] GetPref(){
return Preference_List;
}
public Person(string Preferences, int nm){
prefQueue = new Queue<string>();
name = nm;
Preference_List = Preferences.Split (' ');
foreach (string element in Preference_List) {
prefQueue.Enqueue (element);
}
Married = false;
}
public int GetName(){
return(name);
}
public void GetMarried(Person Pt){
this.Married = true;
Pt.Married = true;
this.Partner = Pt;
Pt.Partner = this;
}
public void GetDivorced(){
this.Married = false;
Partner.Married = false;
this.Partner = null;
//this.Partner.Partner = null;
}
public Person GetMPartner(){
return(this.Partner);
}
public int GetPartner(){
return(this.Partner.name);
}
public bool Prefers(Person Suitor){
foreach(string element in this.prefQueue){
if(Convert.ToInt32(element) == Suitor.name){
return true;
} else if (Convert.ToInt32(element) == this.Partner.name){
return false;
}
}
return false;
}
class Man:Person {
public Man(string Preferences, int nm)
: base(Preferences, nm) {
Married = false;
}
}
class MainClass
{
public static void Main (string[] args)
{
List<Man> men = new List<Man> ();
List<Person> women = new List<Person> ();
int num = 0;
int count = 0;
string line = Console.ReadLine ();
while (line != "0") {
//Gives the number of pairs to make
if (line.Length == 1) {
if (count > 0) {
Console.WriteLine ("Instance " + count + ":");
for (int i = 0; i < num; i++) {
Console.WriteLine (men [i].Partner.name);
}
}
count++;
num = Convert.ToInt32 (line);
men.Clear();
women.Clear();
//Forms the Preference List for Men
for (int i = 0; i < num; i++) {
line = Console.ReadLine ();
men.Add (new Man (line, i+1));
}
//Forms the Preference List for Women
for (int i = 0; i < num; i++) {
line = Console.ReadLine ();
women.Add (new Person (line, i+1));
}
Man currentMan = null;
int freemen = num;
while (freemen != 0) {
for (int i = 0; i < num; i++) {
if (men [i].Married == false) {
currentMan = men [i];
break;
}
}
int choice = Convert.ToInt32 (currentMan.prefQueue.Dequeue());
if (women [choice - 1].Married == false) {
currentMan.GetMarried (women [choice - 1]);
freemen--;
} else {
Console.WriteLine ("Conflict: " + currentMan.prefQueue.Peek());
if (women [choice - 1].Prefers (currentMan)) {
Console.WriteLine ("Prefers New Man");
women [choice - 1].GetDivorced ();
currentMan.GetMarried (women [choice - 1]);
}
}
}
}
line = Console.ReadLine();
}
Console.WriteLine ("Instance " + count + ":");
for (int i = 0; i < num; i++) {
Console.WriteLine (men [i].Partner.name);
}
}
}
}
}