Docsity

Ada95 Programs for Palindrome Checking and String Conversion Algorithms, Exercises of Engineering

Partial preview of the text

Download Ada95 Programs for Palindrome Checking and String Conversion Algorithms and more Exercises Engineering in PDF only on Docsity!

Home Work 7 Solutions

Part a. Write an algorithm to check if a user entered string is a palindrome.

Algorithm:

Get input string and length from the user.

Set Flag to True

For I in 1 .. Length/2 loop

If String(I) /= String(Length โ€“ I+1) then

Display โ€˜Input_String not a Palindromeโ€

Set Flag to False

If Flag = True then

Display โ€œInput String is a Palindromeโ€

Part b. Implement your algorithm as an Ada95 program.

  1. -- Program to check if a user input string is a
  2. -- palindrome
  3. -- Programmer: Joe B
  4. -- Date Last Modified : March 16, 2004

  5. with Ada.Text_Io;
  6. procedure Check_Palindrome is
  7. -- defines a string with maximum length of 80
  8. User_Input : String (1..80);
  9. -- defines a variable to hold the length from get_line
  10. User_Length : Integer;
  11. -- flag to determine if the string was a palindrome
  12. -- remains true if the string is a palindrome
  13. Flag : Boolean := True;
  14. begin
  15. Ada.Text_Io.Put("Please Enter a String: ");
  16. Ada.Text_Io.Get_Line(User_Input, User_Length);
  17. for I in 1 .. User_Length/2 loop
  18. if User_Input(I) /= User_Input(User_Length - I+1) then
  19. Ada.Text_Io.Put_Line("Input is not a palindrome");
  20. Flag := False;
  21. exit;
  22. end if;
  23. end loop;
  24. if Flag = True then
  25. Ada.Text_Io.Put_Line("Input string is a palindrome");
  26. end if;
  27. end Check_Palindrome;

32 lines: No errors

2. Modify the program above to read inputs from a text file and store the reversed string

in an output text file. The program should:

Main Program Algorithm:

Open input_file

Create output_file

While not end_of_file (input_file)

Read input_string and length from the file.

If Is_Palindrome(input_string) then

Store input_string in output_file

Else

Reverse input_string

Store reversed_string in output_file

Close input_file

Close output_file

Reverse String Algorithm:

Get Input_String and Length

For I in 1 .. Length/

Temp := Input_String(I)

Input_String(I) := Input_String(Length โ€“ I + 1)

Input_String(Length-I + 1) := Temp

Store String Algorithm:

Get Input_String and Length

For I in 1 .. Length

Put Input_String(I) in Output_File

end loop

Put new_line in Output_File

Part b. Implement your algorithm as an Ada95 program.

Hw_7 Package Specification Listing

GNAT 3.15p (20020523) Copyright 1992-2002 Free Software Foundation, Inc.


  1. -- package specification containing subprograms
  2. -- to reverse a string and check if the string is
  3. -- a palindrome
  4. -- Specifier : Joe B
  5. -- Date Last Modified : March 16, 2004

  6. with Ada.Text_Io;
  7. package Hw_7 is
  8. -- checks if a string is a palindrome, returns true if it is
  9. function Check_Palindrome (
  1. procedure Store_String (
  2. User_Input : in String;
  3. User_Length : in Integer;
  4. Storage_File : in out Ada.Text_Io.File_Type ) is
  5. begin
  6. for I in 1 .. User_Length loop
  7. Ada.Text_Io.Put(Storage_File,User_Input(I));
  8. end loop;
  9. Ada.Text_Io.New_Line(Storage_File);
  10. end Store_String;
  11. end Hw_7;

47 lines: No errors

Main Program Listing

GNAT 3.15p (20020523) Copyright 1992-2002 Free Software Foundation, Inc.

. ----------------------------------------------- . -- Program to read input string from a file . -- if it is a palindrome . -- store in output file . -- else . -- reverse string and store in output file . -- Programmer: Joe B . -- Date Last Modified : March 16, 2004 . ------------------------------------------------ . . with Ada.Text_Io; . with Hw_7; . . procedure File_Based_Palindrome is . . Input_File : Ada.Text_Io.File_Type; . Input_File_Name : String := "my_program_input.txt"; . . Output_File : Ada.Text_Io.File_Type; . Output_File_Name : String := "my_program_output.txt"; . . -- variable to store the string read from input_file . Input_String : String (1..80); . -- variable to store the length of the input_string . Input_Length : Integer; . . begin . -- open input file . Ada.Text_Io.Open(Input_File, Ada.Text_Io.In_File, Input_File_Name); . -- create the output file . Ada.Text_Io.Create(Output_File, Ada.Text_Io.Out_File, Output_File_Name); . . -- repeat while there are strings in the file . while not Ada.Text_Io.End_Of_File(Input_File) loop . -- read input from input_file . Ada.Text_Io.Get_Line(Input_File, Input_String, Input_Length);

  1. if (Hw_7.Check_Palindrome(Input_String, Input_Length)) then
  2. -- store the string directly if it is a palindrome
  3. Hw_7.Store_String(Input_String, Input_Length, Output_File);
  4. else
  5. -- reverse the string because it is not a palindrome
  6. Hw_7.Reverse_String(Input_String, Input_Length);
  7. -- store the string in the output file
  8. Hw_7.Store_String(Input_String, Input_Length, Output_File);
  9. end if;
  10. end loop;
  11. -- close both files
  12. Ada.Text_Io.Close(Input_File);
  13. Ada.Text_Io.Close(Output_File);
  14. end File_Based_Palindrome;

53 lines: No errors

Display Display the contents of the Queue

b. Modify the expression conversion algorithm shown in class to include unary operators.

To resolve the ambiguity between unary and binary operators which use the same

symbol, two new symbols are created for use within the program:

  • โ€˜+โ€™ is represented using โ€˜!โ€™
  • โ€˜-โ€™ is represented using โ€˜~โ€™

The Is_Operator function has to be modified to include these two symbols:

If character = โ€˜(โ€™ | โ€˜)โ€™ | โ€˜+โ€™ | โ€˜-โ€™ | โ€˜*โ€™ | โ€˜/โ€™ | โ€˜!โ€™ | โ€˜~โ€™

The key thing to remember is that unary โ€˜+โ€™ and โ€˜-โ€™ have the highest precedence. Hence

the Precedence function has to be modified to account for it as follows:

Switch operator

When โ€˜!โ€™ | โ€˜~โ€™ => return 4

When โ€˜*โ€™ | โ€˜/โ€™ => return 3

When โ€˜+โ€™ | โ€˜-โ€™ => return 2

When โ€˜(โ€™ | โ€˜)โ€™ => return 1

Algorithm to Identify if operator is Unary:

Get the infix_string and current_position of operator

Location := current_position-

If the unary operator is the first non-blank character in the string

Return true

While Location >= 1 and Infix_String(Location) /= โ€˜(โ€™

if Infix_String(Location) = โ€˜ โ€™ then

Location := Location -

elsIf Infix_String(Location) is an operator

Return true

else

Return false

Return true

Algorithm to convert โ€˜+โ€™ or โ€˜-โ€™ into unary operators:

If Infix_String(I) = โ€˜+โ€™ or Infix_String(I) = โ€˜-โ€™ then

Check if Infix_String(I) is unary.

If it is unary

Replace โ€˜+โ€™ with โ€˜!โ€™

Replace โ€˜-โ€™ with โ€˜~โ€™

A similar replace operation takes place when the postfix string is being generated.

If โ€˜!โ€™ is encountered, replace it with โ€˜+โ€™

If โ€˜~โ€™ is encountered, replace it with โ€˜-โ€™

c)

Code Listing

my_postfix_converter specification partial file listing

  1. -- function to check if the operator is unary
  2. -- this is a new function
  3. function Is_Unary(Infix: String; Position: Integer) return Boolean;
  4. -- function to convert infix to postfix
  5. -- this function is modified
  6. procedure Infix_To_Postfix(Infix : in out String; Length: in Integer; postfix : out string);

Note that the function became a procedure because the string was being manipulated. An

alternative solution is to keep the original function and create a local string variable that

is used throughout the Infix_To_Postfix conversion function

my_postfix_converter package body partial file listing

GNAT 3.15p (20020523) Copyright 1992-2002 Free Software Foundation, Inc.


  1. -- Package body for My_Postfix_Converter
  2. --
  3. -- Implementer: Joe B
  4. -- Date Last Modified : March 15, 2004

  5. with Ada.Text_Io;
  6. use Ada.Text_Io;

12 - 95. regular stack operations hence not listed

  1. -- checks if input is an operator
  2. function Isoperator (
  3. Operator : Character )
  4. return Boolean is
  5. begin
  6. case Operator is
  7. when '~'|'!'|'*'| '/'|'+'|'-'|'('|')' =>
  8. return True;
  9. when others =>
  10. return False;
  11. end case;
  12. end Isoperator;
  13. โ€“ returns precedence of the operator