❌ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayNews from the Ada programming language world

How to divide a character with a string in Ada

I'm trying to figure out how to divide a character with a string and get a float as the quota. I've done the following in my code:

 Procedure extwo is

 function  "-"(Strang: in String;
               Char: in Character) return Float is
  
   F: Float := Float'Value(Strang);
  
 begin
        
   return Float(Character'Pos(Char)-48) - Float'Value(Strang);
 
 end "-"; 

 Differensen: Float;
 Char: Character;
 Strang: String(1.
                
Begin 

 Put("Mata in ett tecken: ");
 Get(Char);
  
 Put("Mata in en strΓ€ng med exakt 3 tecken: ");
 Get(Strang);

 Differensen:= Char-Strang;

 Put("Du matade in tecknet: ");
 Put(Char);
 Put(" och strΓ€ngen: ");
 Put(Strang);
 Put(" och differensen blev ");
 Put(Differensen, Fore=> 0); 

end extwo;

With this I get the error messages: " invalid operand types for operator "-" ", " left operand has type "Standard.Character" " and " right operand has subtype of "Standard.String" defined at line 59 " all on line 95:22 which is the line where it says "Differensen:= Char-Strang;"

Dividing a character with a Float in Ada

So iΒ΄m trying to divide a character with a float using an operator, but I dont know why my code gets the error message "unexpected argument for "Position" attribute"

function "/"(Teck: in Character;
             Flyt: in Float) return Float is
  
  
begin
  
  return Float(Character'Position(Teck))/Flyt;
  
end "/";

Can somebody explain how Character'position works and what I need to change here, because IΒ΄ve used pretty much the same code previously in a different assigment.

Ada: operator with type conversion and rounding

I have a specific problem that I got some issues figuring out. I want to create an operator in Ada that can divide a float value with a character and then return as an integer.

I understand that the operator should be a function with "/" and some kind of type conversion from float value and a character to an integer. But how would the return value look like and which rounding of the float value would be appropriate?

update.

Let's say I would like to put in the float value -21.8 and the character '2'. The answer should be -11. I created a subprogram for this but I feel like the solution could be something more simple.

      function "/"(Float_Val : in Float;
        Ch      : in Character) return integer is
      
   begin
      if Float_Val < Float(0) then return
    (Integer(Float'Rounding(Float_Val)) - (Character'Pos(Ch) - (Character'Pos('0'))) + 1) / (Character'Pos(Ch) - (Character'Pos('0')));
      else return
    (Integer(Float'Rounding(Float_Val)) + (Character'Pos(Ch) - (Character'Pos('0'))) - 1) / (Character'Pos(Ch) - (Character'Pos('0')));
      end if;
   end "/";

and "/" is called in my main program by

 Put(Float_Val / Ch, Width => 0);
❌
❌