0

My goal is to receive from standard input an equation, store that in an array for later use/re-printing, then output a line printing the whole equation AND the answer after just like so:

Input: 2+3=

Output: 2 + 3 = 5

I am very confused on how to go about doing this due to the inability of Ada to have dynamic strings and such.

This is a rough idea that I have in pseudo-code..

 Until_loop:                 
       loop 

         get(INT_VAR);
         --store the int in the array?
         get(OPERATOR_VAR);
         --store the operator in the following index of that array? and
         --repeat until we hit the equal sign, signaling end of the equation

         get(CHECK_FOR_EQUALSIGN);
     exit Until_loop when CHECK_FOR_EQUALSIGN = "=";

     end loop Until_loop;

 --now that the array is filled up with the equation, go through it and do the math
 --AND print out the equation itself with the answer

I am guessing the array should look like:

[2][+][5][=][7]

I am also a beginner with Ada, so it's even more difficult to get a grasp of things, I am very good with Java, but I can't get used to the strongly typed syntax. Please ask if you need more info.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user2855405
  • 495
  • 2
  • 7
  • 20

3 Answers3

3

Ada can use dynamic fixed strings, without resorting to Unbounded_String or Containers, or allocation and pointers, although these are options.

The insight making this possible is that a string can get its size from its initialisation expression when it is declared - but that declaration can be inside a loop, so that it is executed anew each time round the loop. You can't always structure a program so that this makes sense, though it's possible surprisingly often, with a little thought.

Another feature is that later on, these "declare" blocks make great candidates for very easy refactoring into procedures.

with Ada.Text_IO; use Ada.Text_IO;

procedure Calculator is
begin
   loop
      Put("Enter expression: ");
      declare
         Expression : String := Get_Line;
      begin
         exit when Expression = "Done";
         -- here you can parse the string character by character
         for i in Expression'range loop
            put(Expression(i));
         end loop;
         New_Line;
      end;
   end Loop;
end Calculator;

You should get

brian@Gannet:~/Ada/Play$ gnatmake calculator.adb
gcc-4.9 -c calculator.adb
gnatbind -x calculator.ali
gnatlink calculator.ali
brian@Gannet:~/Ada/Play$ ./calculator
Enter expression: hello
hello
Enter expression: 2 + 2 =
2 + 2 =
Enter expression: Done
brian@Gannet:~/Ada/Play$ 

You still have to write the calculator...

1

If all you're trying to do is input a variable length string, submit it to some evaluator that parses and evaluates the string, and then reflects it back out along with the calculated value, for the dynamic string handling you can simply use Unbounded_Strings and Unbounded_IO:

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
use Ada.Text_IO;

procedure Evaluate_Expression is

   function Evaluate (E : Unbounded_String) return Unbounded_String is
   begin
      ...
   end Evaluate;

   Expression : Unbounded_String;

begin
   Put("Input: ");
   Get_Line(Expression);  -- This is Unbounded_IO.Get_Line.
   Put_Line(To_Unbounded_String("Output: ")
            & Expression
            & To_Unbounded_String(" = ")
            & Evaluate(Expression));
end Evaluate_Expression;
Marc C
  • 8,664
  • 1
  • 24
  • 29
0

the inability of Ada to have dynamic strings and such

Ada doesn't have an inability as such, you just need to use structure to provide for the dynamic ability you want. In this case you need to use objects and pointers (records and access). Your objects encapsulate the input data and provide the functions to combine them. Obviously you also have different types of input data, numbers and operators, so you need to build this into your objects (using inheritance).

Basically you want to use OOP to store and operate on the entered data.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I agree on the OOP part, but with Ada 2012 there is no need to mess with pointers (access types) yourself. The standard library includes plenty of container types for indefinite types. – Jacob Sparre Andersen Mar 01 '15 at 10:29