【问题标题】:How to compare strings in ada 95如何比较 ada 95 中的字符串
【发布时间】:2016-01-29 15:38:41
【问题描述】:

我刚开始学习 Ada 95,但在比较字符串时遇到了一些问题。

代码如下:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; 
with Ada.Command_Line;
with Ada.Strings.Unbounded;

procedure Test1 is

   vad : String(1..9);
   Amount : Integer;
   Main : Integer;
   Second : Integer;
   Third : Integer;

begin
   Main := 1;
   Second := 0;
   Third := 0;

   Put("What do you want to do?");
   New_Line(1);
   Get(vad);
   New_Line(1);

   if Vad  = "fibonacci" then
      Put("How long do you want the sequence to be");
      New_Line(1);

      Get(Amount);
      New_Line(1);

      Amount := Amount -1;

      for I in 1 .. Amount loop
         Put(Main); 
         New_Line(1);
         --Put(" ");
         Third := Second;
         Second := Main;

         Main := (Second + third);
      end loop;

      New_Line(2);

   elsif Vad = "two" then
      Put("How long do you want the sequence to be?");
      New_Line(1);
      Get(Amount); 
      New_Line(1);
      for U in 1 .. Amount loop
         Put(U * 2);
         Put(", ");
      end loop;

   else
      Put("ok");

   end if;

end Test1;

就像现在一样,当我输入fibonacci 时,if 语句会识别,但当我输入two 时,它只会转到代码的“else”部分。

有什么想法可能是错的吗?

提前致谢。

【问题讨论】:

    标签: string if-statement compare ada


    【解决方案1】:

    您可能会感到困惑的是String 类型实际上是一个固定长度 字符串。

    这意味着将Vad 与任何不完全是 9 个字符的字符串进行比较都会失败。

    一种选择是声明Vad 并使用Ada.Text_IO.Get_Line 的输出一次性对其进行初始化:

    Vad : constant String := Ada.Text_IO.Get_Line;
    

    通过这种方式,您将准确读取写入的内容,直到(但不包括)输入Vad 的下一个换行符。

    【讨论】:

    • 这是最好的。但另一种方法是比较来自 Vad 的子字符串:if Vad(other'range) = other then ...if Vad(1 ..3) = "two" then
    【解决方案2】:

    Jacob 的解释是正确的。这是修改为使用 Ada.Text_IO.Get_Line 的代码

    我删除了“with Ada.Strings.Unbounded”行,因为不需要该包。

    with Ada.Text_IO;         use Ada.Text_IO;
    with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; 
    with Ada.Command_Line;
    
    procedure Test1 is
       Amount : Integer;
       Main   : Integer := 1;
       Second : Integer := 0;
       Third  : Integer := 0;
    begin
       Put_Line("What do you want to do?");
    
       declare
          vad : constant string := Get_Line;
       begin
          if Vad  = "fibonacci" then
             Put_Line("How long do you want the sequence to be?");
             Get(Amount);
             Amount := Amount -1;
    
             for I in 1 .. Amount loop
                Put(Main); 
                New_Line;
                --Put(" ");
                Third  := Second;
                Second := Main;
                Main   := (Second + third);
             end loop;
             New_Line(2);
    
          elsif Vad = "two" then
    
             Put_Line("How long do you want the sequence to be?");
             Get(Amount); 
             for U in 1 .. Amount loop
                Put(integer'image(U * 2) & ", ");
             end loop;
          else
             Put("ok");
          end if;
       end;
    end Test1;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多