-- The program accepts as input the encoded message and produces -- the decoded message as output. -- It is known that each message begins with the word 'Agent'. with text_io; use text_io; procedure agent is Input_File : File_Type; -- The input File with encoded message. Result_File : File_Type; -- The output File with decoded message. c : CHARACTER; -- The character being read. shift : INTEGER; -- The value of shift. bigapos : constant INTEGER := CHARACTER'POS('A');-- POS value of A smlapos : constant INTEGER := CHARACTER'POS('a');-- POS value of a bigzpos : constant INTEGER := CHARACTER'POS('Z');-- POS value of Z smlzpos : constant INTEGER := CHARACTER'POS('z');-- POS value of z -- This function accepts the character and returns the decoded character function DECODE( N : in INTEGER; C : in CHARACTER) return CHARACTER is -- Preconditions : N contains the shift value used to encode and -- C is the character to be decoded. -- Postconditions: The decoded character for C is returned. CH : CHARACTER := C; -- A character variable to be returned. begin -- Check if C is a vowel and do the rotation specific to vowels. case CH is when 'A' => CH := 'U'; when 'E' => CH := 'A'; when 'I' => CH := 'E'; when 'O' => CH := 'I'; when 'U' => CH := 'O'; when 'a' => CH := 'e'; when 'e' => CH := 'i'; when 'i' => CH := 'o'; when 'o' => CH := 'u'; when 'u' => CH := 'a'; when others => CH := CH; end case; -- Now do the rotation for upper case letter. if ( CH in 'A'..'Z' ) then if ( (CHARACTER'POS(CH) - N) < bigapos ) then CH := CHARACTER'VAL(bigzpos- ( bigapos- (CHARACTER'POS(CH) - N) - 1 )); else CH := CHARACTER'Val( CHARACTER'POS(CH) - N ); end if; end if; -- Now do the rotation for lower case letter. if ( CH in 'a'..'z' ) then if ( (CHARACTER'POS(CH) - N) < smlapos ) then CH := CHARACTER'VAL(smlzpos - ( smlapos- (CHARACTER'POS(CH) - N) - 1 )); else CH := CHARACTER'Val( CHARACTER'POS(CH) - N ); end if; end if; return CH; end DECODE; begin -- setup files -- OPEN (Input_File,In_File,"encoded1.txt"); -- Open File -- SET_INPUT(Input_File); -- set as default input file -- OPEN (Result_File,Out_File,"decoded1.txt"); -- Open File -- SET_OUTPUT(Result_File); -- set as default input file -- The first character is treated differently in order to derive -- the value of N. We know that the first word is always Agent. get(c); -- Check if c is a vowel and rotate back accordingly. case c is when 'A' => c := 'U'; when 'E' => c := 'A'; when 'I' => c := 'E'; when 'O' => c := 'I'; when 'U' => c := 'O'; when others => c := c; end case; -- Now find the difference between this character and A. -- This is the amount by which other letters will be shifted. shift := CHARACTER'POS(c) - CHARACTER'POS('A'); put(DECODE(shift, c)); -- read and decode the file till it is over. while( not END_OF_FILE ) loop if ( END_OF_LINE ) then new_line; skip_line; else get(c); put(DECODE(shift, c)); end if; end loop; end agent;