-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruedas.cpp
executable file
·166 lines (141 loc) · 2.29 KB
/
ruedas.cpp
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
#include "ruedas.hpp"
#include <stdio.h>
#include <string.h>
// Constructor de clase. Hace falta configurar después las ruedas
RUEDA::RUEDA()
{
setID(NOID);
setWire(UNPLUG);
setRing('A');
setKey('A');
NTO=0;
return;
}
// Avanza una posición. Retorna 1 si pasa por una letra de TurnOver
int RUEDA::avanza()
{
int av=0,i;
i=0;
if(NTO!=0)
{
do
{
if(TO[i]==pos)
av=1;
i++;
}while((av==0)&&(i<NTO));
}
pos++;
if(pos>'Z')
{
pos='A';
}
return(av);
}
// Comprueba si debe hacer el doble paso
int RUEDA::IsTO(char t)
{
int i,flag=0;
i=0;
if(NTO>0)
{
do{
if(TO[i]==t)
flag=1;
i++;
}while((flag==0)&&(i<NTO));
}
return(flag);
}
int RUEDA::doblestep()
{
int av=0,i;
i=0;
if(NTO>0)
{
do{
if(TO[i]==pos)
av=1;
i++;
}while((av==0)&&(i<NTO));
}
return(av);
}
// Retorna la posición del anillo actual
char RUEDA::getRing()
{
return(Ring);
}
// Retorna la posición de la letra actual
char RUEDA::getKey()
{
return(pos);
}
// configura el ringstellung
void RUEDA::setRing(char c)
{
Ring=c;
return;
}
// configura la posición de la rueda
void RUEDA::setKey(char c)
{
pos=c;
return;
}
// COnfigura las conexiones de la rueda
void RUEDA::setWire(char *wr)
{
int i;
strcpy(Wiring,wr);
for(i=0;i<26;i++)
BWiring[Wiring[i]-'A']='A'+i;
return;
}
void RUEDA::setTO(char *to)
{
if(to!=NULL)
{
strcpy(TO,to);
NTO=strlen(TO);
}
else
NTO=0;
return;
}
void RUEDA::setID(char *tx)
{
strcpy(ID,tx);
return;
}
int RUEDA::code(int i,int dir)
{
int o;
int ps;
if (dir==FWD)
{
ps=i+(pos-Ring);
// fprintf(stderr,"**%i",ps);
if(ps>25) ps-=26;
if(ps<0) ps+=26;
// fprintf(stderr,"**%i\n",ps);
o=(Wiring[ps]-'A')-(pos-Ring);
if(o>25) o-=26;
else if(o<0) o+=26;
}
else
{
ps=i+(pos-Ring);
if(ps<0) ps+=26;
else if(ps>25) ps-=26;
o=(BWiring[ps]-'A')-(pos-Ring);
if(o>25) o-=26;
else if(o<0) o+=26;
}
return(o);
}
void RUEDA::PrintW()
{
fprintf(stdout,"%s-%s\n",ID,Wiring);
return;
}