473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Rotor class reverses wierdly

I'm writing a program that simulates the Enigma machine. I'm basing it
around a class called Rotor. It has a ReverseRotor() fn that...reverse
the rotor by how many steps. But it works strangely. Compile and run
this:

#include <iostream>
#include <cstdlib>
using namespace std;

class Rotor
{
public:
Rotor():CurPos(0)
{
memcpy(Alphabet,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456 789",36);
}
void SetRotorPosition(int NewPos)
{
while(NewPos < 0)
{
NewPos +=36;
}
CurPos = NewPos%36;
}
void AdvanceRotor(int Steps)
{
CurPos += Steps;
while(CurPos < 0)
{
CurPos +=36;
}
CurPos %=36;
}
void ReverseRotor(int Steps)
{
CurPos -= Steps;
while(CurPos < 0)
{
CurPos-=36;
}
CurPos%=36;
}
char GetCurrentCharacter()
{
return Alphabet[CurPos];
}
char GetCharacterIndex(int Index)
{
return Alphabet[(CurPos+Index)%36];
}
private:
char Alphabet[36];
int CurPos;
};

int main()
{
Rotor rotor;
int index;
for(int i=0;i<36;i++)
{
cout << "Enter index: " << endl;
cin >index;
cout << "Character at index: " << rotor.GetCharacterIndex(index) <<
endl;
rotor.ReverseRotor(1);
}
system("PAUSE");
return EXIT_SUCCESS;
}

Enter 1 and you'll get B, as expected. Enter 1 again and you'll
get...E? Not A as I would expect. Something's wrong here, and I can't
figure it out. Any help here? Thanks!!!!

BTW, AdvanceRotor() works just fine.

Dec 30 '06 #1
1 1768

"Protoman" <Pr**********@gmail.comwrote in message
news:11*********************@k21g2000cwa.googlegro ups.com...
I'm writing a program that simulates the Enigma machine. I'm basing it
around a class called Rotor. It has a ReverseRotor() fn that...reverse
the rotor by how many steps. But it works strangely. Compile and run
this:

#include <iostream>
#include <cstdlib>
using namespace std;

class Rotor
{
public:
Rotor():CurPos(0)
{
memcpy(Alphabet,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456 789",36);
}
void SetRotorPosition(int NewPos)
{
while(NewPos < 0)
{
NewPos +=36;
}
CurPos = NewPos%36;
}
void AdvanceRotor(int Steps)
{
CurPos += Steps;
while(CurPos < 0)
{
CurPos +=36;
}
CurPos %=36;
}
void ReverseRotor(int Steps)
{
CurPos -= Steps;
while(CurPos < 0)
{
CurPos-=36;
Didn't you actually mean
CurPos += 36;
here?
}
CurPos%=36;
}
char GetCurrentCharacter()
{
return Alphabet[CurPos];
}
char GetCharacterIndex(int Index)
{
return Alphabet[(CurPos+Index)%36];
}
private:
char Alphabet[36];
int CurPos;
};

int main()
{
Rotor rotor;
int index;
for(int i=0;i<36;i++)
{
cout << "Enter index: " << endl;
cin >index;
cout << "Character at index: " << rotor.GetCharacterIndex(index) <<
endl;
rotor.ReverseRotor(1);
}
system("PAUSE");
return EXIT_SUCCESS;
}

Enter 1 and you'll get B, as expected. Enter 1 again and you'll
get...E? Not A as I would expect. Something's wrong here, and I can't
figure it out. Any help here? Thanks!!!!

BTW, AdvanceRotor() works just fine.

Dec 30 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: Robin Becker | last post by:
It seems that the rotor module is being deprecated in 2.3, but there doesn't seem to be an obvious alternative. I'm using it just for obfuscation. It seems we have ssl available in 2.3 for sockets,...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
4
by: Chua Wen Ching | last post by:
Hi there, I had some questions in mind. Why rotor if there is already .NET Framework? Is it rotor allows us to see some source code part of .NET implementation? If it is, we can use...
3
by: Murphy Wong | last post by:
Dear all, I've installed python 2.4.2 and Zope 2.8.5 on a 64-bit Linux. As I'm installing FLE (http://fle3.uiah.fi/), I find that it will call the rotor module i npython. However, rotor is...
1
by: Mohammad Alshraideh | last post by:
Dear All, I have extended the JScript compiler in rotor. Some of my code is added to the JScript classes and some of my code is new classes and I have put these in a new namespace "mynamespace"...
5
by: rony steelandt | last post by:
I'm in the midle of porting a python 1.5 application to 2.4 I just discovered that the rotor encryption module isn't part anymore of the 2.4 distribution. Is there a way to add this module to...
41
by: Chris Lasher | last post by:
A friend of mine with a programming background in Java and Perl places each class in its own separate file in . I informed him that keeping all related classes together in a single file is more in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.