{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

1009 - t = buffer[i'A t = rot[2.pre[t t = rot[1.pre[t t =...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include<iostream> using namespace std; struct Rotor { int map[32]; int pre[32]; int rev; }; Rotor rot[3]; char buffer[1024]; int num; void movenext(int cur){ static int premap[32]; memcpy(premap, rot[cur].map, sizeof(premap)); int i,t; for(i=0;i<num;i++){ t = (i+num-1)%num; t = premap[t]; t = (t+1)%num; rot[cur].map[i] = t; rot[cur].pre[t] = i; } if(cur < 2){ rot[cur].rev ++; if(rot[cur].rev >= num){ movenext(cur+1); rot[cur].rev = 0; } } } void init(){ int i,j,t; for(i=0;i<3;i++){ cin>>buffer; for(j=0;buffer[j];j++){ t = buffer[j] - 'A'; rot[i].map[j] = t; rot[i].pre[t] = j; } rot[i].rev = 0; } } void decrypt(){ int i,j,t; for(i=0;buffer[i];i++){
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: t = buffer[i] - 'A'; t = rot[2].pre[t]; t = rot[1].pre[t]; t = rot[0].pre[t]; buffer[i] = t +'a'; movenext(0); } cout<<buffer<<"\n"; } void save_rot(int status){ static Rotor save_rot[3]; if(status == 0){ memcpy(save_rot, rot, sizeof(save_rot)); } else { memcpy(rot, save_rot, sizeof(save_rot)); } } void fun(){ static int tstcase = 1; int crypt; save_rot(0); if(tstcase !=1) cout<<"\n"; cout<<"Enigma "<<tstcase++<<":\n"; cin>>crypt; while(crypt --){ save_rot(1); cin>>buffer; decrypt(); } } int main(){ cin>>num; while(num){ init(); fun(); cin>>num; } return 0; }...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern