--------------------------- -- Math Package -- October 18, 2002 -- Malia Kilpinen -- 16.35 Problem Set 2 Example Solution --------------------------- with Math_Functions; use Math_Functions; with Ada.Float_Text_Io; with Ada.Text_Io; with Ada.Integer_Text_Io; use Ada.Float_Text_Io; use Ada.Text_Io; use Ada.Integer_Text_Io; procedure Menu is -- declare and initialize variables Continue_Loop : Integer; -- variable used for continuing loop Menu_Selection : Integer; Number : Integer := - 1; Factorial_Answer : Integer := 0; Inputfloat1 : Float; Inputfloat2 : Float; Maximum_Answer : Float; Inputinteger1 : Integer; Inputinteger2 : Integer; Sum_Answer : Integer; begin Continue_Loop := 1; while Continue_Loop=1 loop loop -- for exceptions begin --exception block -- Display Menu Options Ada.Text_Io.New_Line; Ada.Text_Io.New_Line; Ada.Text_Io.Put ("*******************Menu*******************"); Ada.Text_Io.New_Line; Ada.Text_Io.Put ("Select a math function:"); Ada.Text_Io.New_Line; Ada.Text_Io.Put ("1) n!"); Ada.Text_Io.New_Line; Ada.Text_Io.Put ("2) max of two floats"); Ada.Text_Io.New_Line; Ada.Text_Io.Put ("3) add two integers"); Ada.Text_Io.New_Line; Ada.Text_Io.Put ("4) exit program"); Ada.Text_Io.New_Line; Ada.Text_Io.Put ("Enter selection number: "); -- Get user input Ada.Integer_Text_Io.Get( Item => Menu_Selection, Width => 10); Ada.Text_Io.Skip_Line; -- in case, input is invalid. clear buffer. -- Determine if the user input is a valid integer while Menu_Selection <1 or Menu_Selection >4 loop Ada.Text_Io.New_Line; Ada.Text_Io.Put("Invalid input"); Ada.Text_Io.New_Line; Ada.Text_Io.Put("Select option 1, 2, 3, or 4"); Ada.Text_Io.New_Line; Ada.Text_Io.Put("Reenter selection number: "); Ada.Integer_Text_Io.Get(Menu_Selection, Width=>10); Ada.Text_Io.Skip_Line; -- in case, input is invalid. clear buffer. end loop; -- Perform menu functions -- This can be implemented in several ways... this is just an example -- using if statements. A case statement could be used easily. ------- Option 1 -------- -- N! if Menu_Selection = 1 then while Number<0 loop -- 1) Prompt user for integer Ada.Text_Io.New_Line; Ada.Text_Io.Put("N! function"); Ada.Text_Io.New_Line; Ada.Text_Io.Put("Enter N as an integer: "); -- 2) Get user input Ada.Integer_Text_Io.Get(Number, Width=>5); Ada.Text_Io.Skip_Line; -- 3) Error check if Number<0 then Ada.Text_Io.New_Line; Ada.Text_Io.Put("You must enter an integer >= 0"); Ada.Text_Io.New_Line; end if; end loop; -- 4) Call function Factorial_Answer:= N_Factorial(N => Number); -- 5) Print answer to screen Ada.Text_Io.New_Line; Ada.Text_Io.Put("The answer is: "); Ada.Integer_Text_Io.Put( Item => Factorial_Answer, Width => 15); -- end of n! function, will loop back to main menu ------- Option 2 -------- -- Max Value elsif Menu_Selection=2 then -- 1) Prompt/get user for two float inputs Ada.Text_Io.New_Line; Ada.Text_Io.Put("Enter first float:"); Ada.Float_Text_Io.Get(Inputfloat1, Width=>15); Ada.Text_Io.Skip_Line; Ada.Text_Io.New_Line; Ada.Text_Io.Put("Enter second float:"); Ada.Float_Text_Io.Get(Inputfloat2, Width=>15); Ada.Text_Io.Skip_Line; -- 2) Call function Maximum_Answer := Max_Value( Num1 => Inputfloat1, Num2 => Inputfloat2); -- 3) Print answer screen Ada.Text_Io.Put("The maximum value is: "); Ada.Float_Text_Io.Put( Item => Maximum_Answer, Fore => 15, Aft => 3, Exp => 0); -- end of max value function, will loop back to main menu ------- Option 3 -------- -- Add two integers elsif Menu_Selection=3 then -- 1) Prompt/get user for integer inputs Ada.Text_Io.New_Line; Ada.Text_Io.Put("Enter first integer value: "); Ada.Integer_Text_Io.Get(Inputinteger1, Width=>10); Ada.Text_Io.Skip_Line; Ada.Text_Io.Put("Enter second integer value: "); Ada.Integer_Text_Io.Get(Inputinteger2, Width=>10); Ada.Text_Io.Skip_Line; -- 2) Call function Sum_Answer := Add_Integers( Num1 => Inputinteger1, Num2 => Inputinteger2); -- 3) Print answer to screen Ada.Text_Io.Put("The sum is: "); Ada.Integer_Text_Io.Put( Item => Sum_Answer, Width => 15); Ada.Text_Io.New_Line; -- end of sum function ------- Option 4 -------- -- Exit loop elsif Menu_Selection=4 then Continue_Loop := 0; Ada.Text_Io.New_Line; Ada.Text_Io.Put("Exiting..."); Ada.Text_Io.New_Line; exit; end if; -- Do exception handling. Determine if user input is of correct type. -- Example: If a float is inputed instead of an int. exception when Ada.Text_Io.Data_Error => Ada.Text_Io.New_Line; Ada.Text_Io.Put("Data input is not the correct type."); Ada.Text_Io.Skip_Line; -- clear input buffer Ada.Text_Io.New_Line; when Constraint_Error => Ada.Text_Io.New_Line; Ada.Text_Io.Put("Constraint error. Data overflow."); Ada.Text_Io.Skip_Line; -- clear input buffer Ada.Text_Io.New_Line; end; -- end of exception block end loop; -- end of loop for exceptions end loop; -- end of menu loop Ada.Text_Io.New_Line; Ada.Text_Io.Put("End of program!"); end Menu;