Question: Can I get with ADA programming? (build a simple calculator, which handle floating point) The main part of this assignment is to implement a simple
Can I get with ADA programming?
(build a simple calculator, which handle floating point)
The main part of this assignment is to implement a simple integer calculator with binary operators
addition (+), subtraction (-), multiplication (*), and division (/) - no unary operators.
Use the algorithm we will go over in class. There are two stacks: and operator stack and an operand
stack. The operator stack can be a stack of characters representing the pending operators (or an
enumeration type if you prefer) and the operand stack is a stack of integers. The program reads a string
and goes through it left to right (in a single pass). When an operand (an integer) is encountered, it is
pushed onto the operand stack. When an operator is encountered, all of the pending operators (on the
operator stack) whose precedence is higher or equal to the current operator are applied (see the next
paragraph) and then the current operator is pushed onto the operator stack. This continues until there
are no more characters in the string. At this point, apply any remaining pending operators (on the
operator stack) and the single value remaining on the operand stack is the value of the expression. If
there is not exactly one operand on the operand stack, then the expression is not valid.
Applying an operator means to pop the operator off the top of the operator stack and pop two operands
off the operand stack (the right one then the left one). The. perform the corresponding operation (add,
subtract, multiple, or divide) on the operands, being careful of their order, and push the result on the
operand stack. If there are not two operands on the stack, then the expression is not valid.
Source code:
unbound_stack.ads
generic
type Item_Type is private;
package Unbound_Stack is
type Stack is private;
Underflow : exception;
procedure Push (Item : in Item_Type; Onto : in out Stack);
procedure Pop (Item : out Item_Type; From : in out Stack);
function Is_Empty (S : Stack) return Boolean;
private
type Cell;
type Stack is access Cell;
end Unbound_Stack;
unbound_stack.adb
package body Unbound_Stack is
type Cell is record
Item : Item_Type;
Next : Stack;
end record;
procedure Push (Item : in Item_Type; Onto : in out Stack) is
begin
Onto := new Cell'(Item => Item, Next => Onto);
end Push;
procedure Pop (Item : out Item_Type; From : in out Stack) is
begin
if Is_Empty(From) then
raise Underflow;
else
Item := From.Item;
From := From.Next;
end if;
end Pop;
function Is_Empty (S : Stack) return Boolean is
begin
return S = null;
end Is_Empty;
end Unbound_Stack;
string_reverse.adb (This is the example code, code goes here)
with Ada.Text_IO, Unbound_Stack;
use Ada.Text_IO;
procedure String_Reverse is
package Unbound_Character_Stack is new Unbound_Stack (Character);
use Unbound_Character_Stack;
Buffer : String (1..80);
Last : Natural;
Char_Stack : Unbound_Character_Stack.Stack;
begin
Get_Line (Buffer, Last);
for i in Natural range 1..Last loop
Push (Buffer(i), onto => Char_Stack);
end loop;
while not Is_Empty(Char_Stack) loop
declare
Ch : Character;
begin
Pop (Ch, from => Char_Stack);
Put (Ch);
end;
end loop;
New_Line;
end String_Reverse;
build_string_reverse.sh
gcc -c unbound_stack.adb
gcc -c string_reverse.adb
gnatbind string_reverse
gnatlink string_reverse
or
gnatmake string_reverse
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
