C/Visual C - ayuda con el programa josephus

 
Vista:
sin imagen de perfil

ayuda con el programa josephus

Publicado por andrea (10 intervenciones) el 01/05/2013 19:13:33
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
#include <stdio.h>
#include <stdlib.h>
 
#define N 5
#define M 0
 
struct cel {
	int info;
	struct cel *prox;
};
 
typedef struct cel Celula;
 
void insere (int, Celula **);
void mostra_vencedor (Celula *);
void josephus (Celula **);
void encontra (Celula **p);
void elimina (Celula **p);
 
int main () {
	int cont;
	Celula *pessoa = NULL;
	printf("%d pessoas.\n", N);
	printf("%d passes.\n", M);
	for (cont = 1; cont <= N; ++cont)
		insere (cont, &pessoa);
	josephus (&pessoa);
	mostra_vencedor (pessoa);
	return EXIT_SUCCESS;
}
 
void insere (int num, Celula **p) {
	Celula *nova;
	nova = (Celula *) malloc (sizeof (Celula));
	nova->info = num;
	if (*p == NULL) {
		*p = nova;
		(**p).prox = *p;
	}
	else {
		Celula *temp = *p;
		while ((**p).prox != temp) {
			*p = (**p).prox;
		}
		(**p).prox = nova;
		nova->prox = temp;
		*p = temp;
	}
}
 
void josephus (Celula **p) {
	if (*p != NULL) {
		while (*p != (**p).prox) {
			encontra (p);
			elimina (p);
		}
	}
}
 
void encontra (Celula **p) {
	int cont = M;
	while (cont) {
		*p = (**p).prox;
		cont--;
	}
}
 
void elimina (Celula **p) {
	Celula *morta = *p;
	while ((**p).prox !=	morta) {
		*p = (**p).prox;
	}
	(**p).prox = (**p).prox->prox;
	*p = (**p).prox;
	free(morta);
}
 
void mostra_vencedor (Celula *p) {
	if (p != NULL)
		printf ("vencedor : %d\n", p->info);
}


ayudaaa!!! no he podido hacer el programa de josephus :(
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

ayuda con el programa josephus

Publicado por luna (10 intervenciones) el 03/05/2013 17:56:15
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
170
171
172
173
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
 
 
 
struct node	{
 
int position;
node * next;
node * prev;
 
};
 
 
 
 
class circularll {
 
public:
 
void create (int, int);
void run (int, int, int);
node * head;
node * tail;
 
private:
 
};
 
 
 
 
void circularll::create (int people, int startPos) {
 
int x, y = 1;
head = tail = NULL;
 
for (x = startPos; x < (people + startPos); x++) {
 
 
if (x == startPos) {
 
node * temp = new node;
temp -> prev = NULL;
temp -> next = NULL;
temp -> position = x;
head = tail = temp;
 
}
 
else {
 
node * temp = new node;
node * curr = head = tail;
 
while(curr -> next != NULL) {
 
curr = curr -> next;
 
}
 
if (x > people) {
 
temp -> position = y;
temp -> next = NULL;
temp -> prev = curr;
curr -> next = temp;
 
}
 
else {
 
temp -> position = x;
temp -> next = NULL;
temp -> prev = curr;
curr -> next = temp;
 
}
 
 
}
 
y++;
 
}
 
node * end = head = tail;
 
while (end -> next != NULL) {
 
end = end -> next;
 
}
 
end -> next = head = tail;
head -> prev = end;
tail -> prev = end;
 
}
 
 
 
void circularll::run (int people, int passBy, int startPos) {
 
int x, i;
node * curr = head = tail;
 
for (x = 1; x < people; x++) {
 
for (i = 1; i <= passBy; i++) {
 
curr = curr -> next;
 
}
 
node * out = curr;
node * before = curr -> prev;
node * after = curr -> next;
 
cout << "La persona que estaba sentada en el asiento" << out -> position << " fue asesinado." << endl;
 
before -> next = after;
after -> prev = before;
curr = after;
delete out;
 
}
 
cout << endl;
cout << "Josephus estaba sentado en el asiento: " << curr -> position << endl << endl;
 
}
 
 
 
 
int main() {
 
 
int people, passBy, startPos;
 
 
menu: // start of menu
 
cout << "¿Cuántas personas hay? (Máximo de 50) ";
cin >> people;
 
if (people > 50) {
 
cout << "entrada no válida" << endl;
goto menu; // returns you to the beginning of the menu
 
}
 
cout << endl << endl;
cout << "¿Qué posición le gustaría empezar? " ;
cin >> startPos;
cout << endl << endl;
 
cout << "¿Cuánta gente quiere saltar cada ronda? ";
cin >> passBy;
cout << endl << endl;
 
circularll josephus;
josephus.create (people, startPos);
josephus.run (people, passBy, startPos);
 
cin >> people;
 
return 0;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar