Guest User

Untitled

a guest
Jan 16th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <iostream> #include <string> #include <cstdlib >
  2. using namespace std ;
  3. void
  4. inputError (){
c o u t <<” u s a g e :<< e n d l ; cout<<”./a1q3a.o {e,d}<<endl; exit (1);
  5. }void normalize(string ⇤txt , int size){
  6. char ⇤c;for (int i=0;i<size;i++){
  7. c=&(⇤txt )[ i ];if ((⇤c>=’a’)&&(⇤c<=’z’)){
  8. } ⇤ c =96;else if((⇤c>=’A’)&&(⇤c<=’Z’)){
  9. } ⇤ c =64;
  10. else{⇤c=0;
  11. } }
  12. }int main(int argc, const char ⇤ argv[]){
  13. if (argc !=2){ inputError ();
  14. }
  15. string cmd=argv [1]; bool encode =1;
  16. if (cmd==”e”){ }
  17. else if (cmd==”d”){ encode =0;
  18. }
  19. else{inputError (); }
  20. string input , key , output ;
  21. 11
  22. CO 487: Assignment #1
  23. Denis Miroshnikov 20447593
  24. i f ( encode ){ cout<<”Plain”;
  25. }
  26. else{cout<<”Cipher”; }
  27. cout<<”text :<<endl<<endl ; getline(cin ,input); cout<<endl<<”Key:<<endl<<endl ; getline(cin ,key);
  28. int size=min(input.size(),key.size());
  29. normalize(&input , size ); normalize(&key , size );
  30. output.resize(size,’ ’); char c;
  31. for(int j=0;j<size ; j++){
  32. i f ( encode ){
c=(input [ j ]+key [ j ])%27;
  33. }
  34. e l s e {c = ( i n p u t [ j ] k e y [ j ] ) ; if (c<0){
  35. c +=27;
  36. } }
  37. if (c){
output [ j]=c+64;
  38. }e l s e {o u t p u t [ j ] = c + 3 2 ;
  39. } }
  40. cout<<endl ;if (! encode){
  41. cout<<”Plain”;
  42. }else{cout<<”Cipher”; }
  43. 12
  44. CO 487: Assignment #1
  45. Denis Miroshnikov 20447593
  46. cout<<”text :<<endl<<endl ;
  47. cout<<output<<endl ; }
Advertisement
Add Comment
Please, Sign In to add comment