Advertisement
JKattackk

DecoderSwitch

Feb 6th, 2020
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.45 KB | None | 0 0
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % Course: ENCMP 100
  3. % Assignment: 2
  4. % Name:
  5. % CCID:
  6. % U of A ID:
  7. %
  8. % Acknowledgements:
  9. % I refered to the MatLab documentation while writing.
  10. %
  11. % Description:
  12. % This program is meant to decode a secret code according to rules outlined
  13. % in the project description.
  14. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  15.  
  16. clc;
  17. clear;
  18. code = int2str(input("Please enter a code to break: "));
  19. intArr = (1:9);
  20. if(length(code) == 9)  %%Test for correct code length
  21.     sum = 0;
  22.     for x = 1:length(code)  %%Put the code into an integer array
  23.         intArr(x) = str2double(code(x));
  24.     end
  25.     for i = 1:length(intArr)  %%Sum all digits of the code
  26.         sum = sum + intArr(i);
  27.     end
  28.     if(2*int8(sum / 2) == sum)  %% Test for even code
  29.         day = intArr(1)*intArr(3) - intArr(5);
  30.         sDay = '';
  31.         switch day
  32.             case 1
  33.                 sDay = 'Monday';
  34.             case 2
  35.                 sDay = 'Tuesday';
  36.             case 3
  37.                 sDay = 'Wednesday';
  38.             case 4
  39.                 sDay = 'Thursday';
  40.             case 5
  41.                 sDay = 'Friday';
  42.             case 6
  43.                 sDay = 'Saturday';
  44.             case 7
  45.                 sDay = 'Sunday';
  46.             otherwise
  47.                 disp("Decoy message: Invalid rescue day.");
  48.                 return;
  49.         end
  50.        
  51.        
  52.             if(rem(intArr(2)*intArr(4)-intArr(6), 3) == 0)  %%If divisible by 3
  53.                 loc = intArr(7) - intArr(9);
  54.             else
  55.                 loc = intArr(8) - intArr(9);
  56.             end
  57.             sLoc = '';
  58.             switch loc
  59.                 case 1
  60.                     sLoc = 'the bridge.';
  61.                 case 2
  62.                     sLoc = 'the library.';
  63.                 case 3
  64.                     sLoc = 'the river crossing.';
  65.                 case 4
  66.                     sLoc = 'the airport.';
  67.                 case 5
  68.                     sLoc = 'the bus terminal.';
  69.                 case 6
  70.                     sLoc = 'the hospital.'
  71.                 case 7
  72.                     sLoc = 'St. Petes Church.';
  73.                 otherwise
  74.                     disp("Decoy message: Invalid rendezvous point.");
  75.                     return;
  76.             end
  77.             disp("Rescue on " + sDay + " at " + sLoc);
  78.                     else
  79.         disp("Decoy message: Sum is odd.");
  80.     end
  81. else
  82.     disp("Decoy message: Not a nine-digit number.");
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement