1

我写了一个函数来清除键盘缓冲区,因为我认为我在获取后有剩余的输入,但它只是永远接受输入。

这是 get(String(1..10)

--getString10--
procedure getString10(s : in out string10) is
   Last: Integer;
begin
   s := (others => ' ');
   Get_Line(S, Last);
end getString10;

这是我所做的刷新,几乎是从关于清除键盘缓冲区的 wiki 复制的

--flush--
procedure flush is
   char : character;
   more : boolean;
begin
   loop
      get_immediate(char, more);
      exit when not more;
   end loop;
end flush;      

每当调用刷新时,我输入的任何内容都会在屏幕上输出,直到我退出程序。

此外,我的 getString10 函数并不总是等待用户输入。例如,如果我有

put("Enter a name: ");
getString10(name);
put("Enter a number: ");
getString10(number);

输出将是

Enter a name: Enter a number: exampleinput

我在 Gnat Programming Studio 上使用 Ada 2005。

更新了整个主要内容:

with Ada.Text_IO, Ada.Integer_Text_IO, BinarySearchTree;
use Ada.Text_IO, Ada.Integer_Text_IO;

procedure lab4 is

   subtype String10 is String(1..10);

   -- function "<"--
   function "<"(TheKey: in String10; ARecord: in String10) return Boolean is
   begin
      for i in integer range 1..10 loop
         if TheKey(i) <= ARecord(i) then
            return true;
         else
            return false;
         end if;
              end loop;

      return false;

   end "<";

   -- function ">"--
   function ">"(TheKey: in String10; ARecord: in String10) return Boolean is
   begin
      for i in integer range 1..10 loop
         if TheKey(i) >= ARecord(i) then
            return true;
         else
            return false;
         end if;
      end loop;

      return false;
   end ">";

   -- function "="--
   function "="(TheKey: in String10; ARecord: in String10) return Boolean is
   begin
      for i in integer range 1..10 loop
         if TheKey(i) /= ARecord(i) then
            return false;
             end if;
      end loop;

      return true;
   end "=";

   --getString10--
   procedure getString10(s : in out string10) is
      Last: Integer;
   begin
          s := (others => ' ');
      Get_Line(S, Last);
   end getString10;

   --flush--
   procedure flush is
      char : character;
      more : boolean;
   begin
      loop
         get_immediate(char, more);
         exit when not more;
      end loop;
   end flush;      

   package BST is new BinarySearchTree(String10, String10, "<", ">", "=");

   Root, found : BST.BinarySearchTreePoint;
   choice : integer;
   nameTemp, phoneTemp : String10;
begin
   BST.setRoot(Root);

   loop
      new_line;
      put_line("Options:");
      put_line("1 - Insert a record");
      put_line("2 - Find a person iteratively and print their phone number");
      put_line("3 - Find a person recursively and print their phone number");
      put_line("4 - Traverse the tree from a person to a person");
      put_line("0 - Quit program");
      put("Choose an option: ");
      get(choice); put(choice, 0); new_line;

      case choice is
         --case 1
         when 1 =>
            put("Enter the name: ");
            get(nameTemp); put(nameTemp); new_line;

            put("Enter the phone number : ");
            get(phoneTemp); put(phoneTemp); new_line;

            BST.InsertBinarySearchTree(root, nameTemp, phoneTemp);
         --case 2
         when 2 =>
            put("Enter the name of the person to find: ");
            get(nameTemp); put(nameTemp);
            BST.FindCustomerIterative(root, nameTemp, found);

            if BST.isNull(found) then
               new_line;
               put("Customer not found!");
            else
               new_line;
               put("The phone number is: ");
               put(BST.CustomerPhone(found));
            end if;
         --case 3
         when 3 =>
            put("Enter the name of the person to find: ");
            get(nameTemp); put(nameTemp);
            BST.FindCustomerRecursive(root, nameTemp, found);

            if BST.isNull(found) then
               new_line;
               put_line("Customer not found!");
            else
               new_line;
               put("The phone number is: ");
               put(BST.CustomerPhone(found));
            end if;
            new_line;

         --case 4
         when 4 =>
            put("Enter of the name of the person to start traversal at: ");
            get(nameTemp); put(nameTemp);
            BST.FindCustomerRecursive(root, nameTemp, found);

            put("Enter then name of the person to stop traversal at: ");
            get(phoneTemp); put(phoneTemp); --using phoneTemp for a name here

            BST.FindCustomerRecursive(Root, nameTemp, found);
            while BST.isNull(found) /= true loop
               put_line("Name = " & BST.CustomerName(found));
               BST.setNode(found, BST.InOrderSuccessor(found));
            end loop;

         --case 0
         when 0 =>
            exit;
         --others
         when others =>
            put_line("Invalid choice!"); new_line;
      end case;

   end loop;

end lab4;

我用 get() 切换了所有的 getString10(),因为我正在尝试调试线程二叉搜索树。我正在使用一个输入文件,所以现在很好,我可以弄清楚为什么其他方法不起作用。nameTemp 和 phoneTemp 上的所有 get 调用都应该是 getString10() 调用。

4

1 回答 1

1

您可以使用 get_line() 函数,它会自动清除键盘缓冲区。

with ADA.TEXT_IO;                use ADA.TEXT_IO;
with ADA.TEXT_IO.UNBOUNDED_IO;   use ADA.TEXT_IO.UNBOUNDED_IO;
with ADA.STRINGS.UNBOUNDED;      use ADA.STRINGS.UNBOUNDED;

procedure MAIN is

   type STRING_10 is new STRING(1..10);

   procedure GET_STRING_10(S : in out STRING_10) is
      BUF   :  UNBOUNDED_STRING;
   begin
      BUF := GET_LINE;

      for I in STRING_10'range loop
         if I <= LENGTH(BUF) then
            S(I) := ELEMENT(BUF, I);
         else
            S(I) := ' ';
         end if;
      end loop;
   end GET_STRING_10;

   S : STRING_10;

begin

   GET_STRING_10(S);
   PUT_LINE(STRING(S));

end MAIN;

阅读整个主要内容后进行编辑:您应该插入一个SKIP_LINE;after GET(CHOICE);。然后,您可以在不同的情况下将 every 替换GET为 a GETSTRING10

通常,get 必须始终跟在 skip_line 之后。无需使用 get_line 执行此操作,因此您不必修改 getstring10 过程。

于 2012-12-07T08:43:50.383 回答