Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

Ada95 programs for checking if a user input string is a palindrome and for converting infix expressions to postfix notation, including unary operators. The palindrome checker includes an algorithm, an ada95 program implementation, and a modified version that reads inputs from a text file and stores the reversed string in an output text file. The string conversion algorithm includes a specification partial file listing, a package body partial file listing, and an implementation of regular stack operations.

Typology: Exercises

2011/2012

Uploaded on 07/22/2012

senapati_007
senapati_007 🇮🇳

3.8

(4)

123 documents

1 / 23

Toggle sidebar

Related documents


Partial preview of the text

Download Ada95 Programs for Palindrome Checking and String Conversion Algorithms and more Exercises Engineering in PDF only on Docsity! fs a) r a2. I ALATIO MAL Mudeie UB. ? b) p. ee be reduced enough . to peach M=] at throat: Spot ba a) For- the Up streqin sechon be be Independent : Of Lee aut pressure , He dick must tbe choked, Wo teat have M=1 at the ¢ bireat, 50 A*= A, From ssembropi-flou teble (Ardem AA) fr M=0.6,,,, Ad = 1188 Khe | so) H 4s 0, 81/75] a een —P ——» f7=/ ee om AL * }1R9 AN M4 = OG > #" yn $este- Sage u Wh, Silas 3,92 fs Pa i. "Ve. ‘aijaniok 4 8 seca here (carve bat ! q i Tie | 5S ey Fie b ago ane singe we have a shock bohm throat, This bime Aft = Va = hil i me : : Ml __, th Fae OE M,= 1,39 Ss =p. [le SH) = amp» LSI Re oe Fro normal-shock toble dais a e = 210 (fe ft =t: 34) | Pe pe = 210p, = From table; M, = Z 745 tp = “ > T= 300K" = Tel Mt) = > 270° Could also use shock tmp, rebir 2 9 wth Te To [tl Hin?) = 216k # / T= h25T, = ZFOK sone result, PRokLeEmM $l SorvTi on SPRING 2004 Engineer's Computation Pad No. 5505, o> 1. Find and plot the step response of the system Ly L2 COS HOE “oO [spe S ri S where Ly = Lp = 2H, Ry =22, and Rp =32N. First, use the Sys teu: é; loop : é, én + Re ~- Reta at fe loop * -R2ae, + dg alee + (2+ Pz) ta = 0 at To Find ste [as poms® , G) find (2) fia panbjculer Saletion ; (a) cons fants fo mate fujtrel leap mathed te write gue. for Cound) $/oms, Ho mong Sn cons Solution To fil,d hon ogeneous soletioas , set right band ‘stdy to gee - The assume ¢ ) “ 4,5 +R, -Re [ -Rz Les +R +Re | Ce Mis) lu terms of the component vel ves, Mis) = | 2s+2 “2 “2 zt | = u = le) homagenecus saly tion; addYand set tl YY o [z]- docs| ity.com PreBtemM Ss! | x Engineer's Computation Pad No. 5505 <> Finally, tHe output is pire by glel= Betz) = 3 e2lt) There fre, - é eos no -3t q (t) = a = 3 = o, tea This can be plotted asing » sag, Motlas o- Eyeel: e 2 ¢ Sf 6 & 4 Step response. 9.(t) © ° 6 AY QO 10 15, 20 Time, t (sec) 2. For the input signal 0, t<0 ut)=4 2, O<t<1 -1, t>1 > find and plot the output y(t), using superposition. Note -+thet ult) f3 a Sum of steps : ult)= 2e(t)~- 3 r(t-s) Qe docs ity.com Prompiem S$! | | Tharetors yit) = 2 9s (+) -3 gslt-1) 2 = 0 (£0) s a FI - ord ~3t g = 2@- l2 e +2Z2e ; e s s 8 0, + -3¢ 3% = 2-ibe Ze Be S = 32 -o- 71 . - oi wage OME og le) s > Again, this cas bea plotted ia Metleb or Excel: Response. ¥(0) 02 04 06 08 1 | 5 0 5 10 15 20 Time, t (sec) (oft <a) ( t 22) docsity.com Proghem S2 | Sot TION | SPRING 200% Engineer's Computation Pad No. 5505 oo A system has step response given by _ f 9, t<0 8 ( -t 3+) gs(t) = { etre, 420 a ¥e oft) Find and plot the response of the system to the input 0, t<0 2k w= {0 a So = (1-e ) -&) using Duhamel’s integral. Dahamel 's ia tegral is glel= 4ls)9slt) + f 9s 4-7) ale det In tis Case, ulo) =a, so g tt) = {- gst -*) ulle)d« ° = (> [eee vr lt-t)- o 2 ze At Note tHaot rlt-+) - 4 tat o tot There fore, git) = IP fee + on OT, et dt tb lt - —-t -3t [pze € +2e et AT ( -t¢ +t -3¢ t ze e + 22 ee | “4 } “ad Wt docsity.com docsity.com 10 w vst 0.1 0.09 |- 0.08 |- 0.07 ! co © oO AYOOJBA YSN) | LO 2 °o ™ D 0.04 0.03 0.02 0.01 time 12 14 16 18 20 12. User_Input : String; 13. User_Length : Integer ) 14. return Boolean; 15. -- reverses the elements in the string from 1 .. length 16. procedure Reverse_String ( 17. User_Input : in out String; 18. User_Length : in Integer ); 19. 20. -- stores the characters in the string from 1 .. length 21. -- into the output file and inserts a new_line 22. procedure Store_String ( 23. User_Input : in String; 24. User_Length : in Integer; 25. Storage_File : in out Ada.Text_Io.File_Type ); 26. 27. end Hw_7; 28. 28 lines: No errors Hw_7 Package Body GNAT 3.15p (20020523) Copyright 1992-2002 Free Software Foundation, Inc. 1. ------------------------------------------------------------ 2. -- Package implementation of Hw_7 3. -- Programmer : Joe B 4. -- Date Last Modified : March 16, 2004 5. -------------------------------------------------------------- 6. with Ada.Text_Io; 7. package body Hw_7 is 8. function Check_Palindrome ( 9. User_Input : String; 10. User_Length : Integer ) 11. return Boolean is 12. Flag : Boolean := True; 13. begin 14. for I in 1 .. User_Length/2 loop 15. if User_Input(I) /= User_Input(User_Length - I+1) then 16. Flag := False; 17. exit; 18. end if; 19. end loop; 20. return Flag; 21. end Check_Palindrome; 22. 23. procedure Reverse_String ( 24. User_Input : in out String; 25. User_Length : in Integer ) is 26. Temp : Character; 27. begin 28. for I in 1 .. User_Length/2 loop 29. Temp := User_Input(User_Length - I + 1); 30. User_Input(User_Length - I + 1) := User_Input(I); 31. User_Input(I) := Temp; 32. end loop; 33. end Reverse_String; 34. docsity.com 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 35. procedure Store_String ( 36. User_Input : in String; 37. User_Length : in Integer; 38. Storage_File : in out Ada.Text_Io.File_Type ) is 39. begin 40. for I in 1 .. User_Length loop 41. Ada.Text_Io.Put(Storage_File,User_Input(I)); 42. end loop; 43. Ada.Text_Io.New_Line(Storage_File); 44. end Store_String; 45. 46. 47. 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); docsity.com 37. if (Hw_7.Check_Palindrome(Input_String, Input_Length)) then 38. -- store the string directly if it is a palindrome 39. Hw_7.Store_String(Input_String, Input_Length, Output_File); 40. else 41. -- reverse the string because it is not a palindrome 42. Hw_7.Reverse_String(Input_String, Input_Length); 43. -- store the string in the output file 44. Hw_7.Store_String(Input_String, Input_Length, Output_File); 45. end if; 46. end loop; 47. -- close both files 48. Ada.Text_Io.Close(Input_File); 49. Ada.Text_Io.Close(Output_File); 50. 51. end File_Based_Palindrome; 52. 53. 53 lines: No errors docsity.com 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 63. -- function to check if the operator is unary 64. -- this is a new function 65. function Is_Unary(Infix: String; Position: Integer) return Boolean; 66. 67. -- function to convert infix to postfix 68. -- this function is modified 69. procedure Infix_To_Postfix(Infix : in out String; Length: in Integer; postfix : out string); 70. 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. ----------------------------------------------------------------- 2. -- Package body for My_Postfix_Converter 3. -- 4. -- Implementer: Joe B 5. -- Date Last Modified : March 15, 2004 6. ------------------------------------------------------------------ 7. 8. with Ada.Text_Io; 9. 10. use Ada.Text_Io; 11. 12 - 95. regular stack operations hence not listed 96. 97. -- checks if input is an operator 98. function Isoperator ( 99. Operator : Character ) 100. return Boolean is 101. begin 102. case Operator is 103. when '~'|'!'|'*'| '/'|'+'|'-'|'('|')' => 104. return True; 105. when others => 106. return False; 107. end case; 108. end Isoperator; 109. 110. – returns precedence of the operator docsity.com 111. function Precedence ( 112. Operator : Character ) 113. return Integer is 114. begin 115. case Operator is 116. when '~' | '!' => 117. return 4; 118. when '*'| '/'=> 119. return 3; 120. when '+'|'-' => 121. return 2; 122. when '('| ')' => 123. return 1; 124. when others => 125. return -1; 126. end case; 127. end Precedence; 128. 129 - 150. -- function to check balance – Not changed hence not listed 151. -- function to check if the + or - is unary 152. function Is_Unary ( 153. Infix : String; 154. Position : Integer ) 155. return Boolean is 156. Prev_Index : Integer; 157. begin 158. -- if it is the first element in the infix string, it is unary 159. if Position = 1 then 160. return True; 161. end if; 162. -- check backwards to see if it is a unary operator 163. Prev_Index:= Position-1; 164. while Prev_Index >=1 and Infix(Prev_Index) /= '(' loop 165. -- if it is a blank space, ignore and check backwards 166. if Infix(Prev_Index) = ' ' then 167. Prev_Index := Prev_Index -1; 168. -- if the previous element is an operator, then the + or - 169. -- has to be a unary operator hence return true 170. elsif Isoperator(Infix(Prev_Index)) then 171. return True; 172. -- if the element is an operand, then it has to be a binary operator 173. -- hence return false 174. else 175. return False; 176. end if; 177. end loop; 178. -- either a '(' is encountered or there are no previous elements 179. -- therefore return true. 180. return True; 181. 182. 183. end Is_Unary; 184. 185. 186. -- function to convert infix to postfix 187. procedure Infix_To_Postfix ( 188. Infix : in out String; 189. Length : in Integer; docsity.com 190. Postfix : out String ) is 191. 192. Postfix_Index : Integer; 193. Operator_Stack : My_Stack; 194. Done : Boolean; 195. Element : Character; 196. 197. begin 198. Create(Operator_Stack); 199. Postfix_Index := 1; 200. for I in 1.. Length loop 201. -- if it is an operand 202. if Isoperator(Infix(I)) = False then 203. -- ignore blank spaces 204. if (Infix(I) /= ' ') then 205. Postfix(Postfix_Index):= Infix(I); 206. Postfix_Index := Postfix_Index + 1; 207. end if; 208. else 209. -- it is an operator 210. Done:= False; 211. loop 212. exit when Done = True; 213. -- if there is possible ambiguity, check if it is unary 214. -- and change the symbol in the expression 215. if Infix(i) = '+' or infix(I) = '-' then 216. if Is_Unary(Infix, I) then 217. if Infix(I) = '+' then 218. Infix(I) := '!'; 219. else 220. Infix(I) := '~'; 221. end if; 222. end if; 223. end if; 224. 225. -- if its a ( or an empty stack then push operator onto stack 226. if Isempty(Operator_Stack) or Infix(I) = '(' then 227. Push(Operator_Stack, Infix(I)); 228. Done := True; 229. -- check the precedence of the element on top of the stack and infix(I) 230. elsif Precedence(Infix(I)) > Precedence(Stacktop(Operator_Stack)) then 231. Push(Operator_Stack, Infix(I)); 232. Done:= True; 233. else 234. -- pop until an operator of smaller precedence appears on the stack 235. Pop(Operator_Stack, Element); 236. if Element = '(' then 237. Done := True; 238. else 239. -- convert special symbols back to standard representation 240. if Element = '~' then 241. Element:= '-'; 242. end if; 243. if Element = '!' then 244. Element := '+'; 245. end if; 246. Postfix(Postfix_Index):= Element; 247. Postfix_Index := Postfix_Index + 1; docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved