我写了一个清除键盘缓冲区的函数,因为我觉得我在get之后有剩余的输入,但它只是永远地输入。
这里是get(String(1..10)
--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;
每当调用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()s切换了所有getString10(),因为我正在尝试调试线程二进制搜索树。我正在使用输入文件,所以它现在很好,我只能弄清楚为什么其他方法不起作用。 对nameTemp和phoneTemp的所有get调用都应该是getString10()调用。
答案 0 :(得分: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;
之后插入GET(CHOICE);
。然后,您可以在不同情况下用GET
替换每个GETSTRING10
。
通常,get必须始终后跟skip_line。使用get_line不需要这样做,因此您不必修改getstring10过程。