为什么我的程序在条件为假 (Ada) 时进入循环(并停留在那里)?
Why is my program entering a loop (and staying there) when condition is false (Ada)?
我的程序中有一个变量控制用户是否要重复名为 "done." 的程序如果 done = '0',程序重复;如果 done = '1',程序退出。如果用户输入的值不是“0”或“1”,它会进入一个循环,提示他们输入有效的内容,直到他们输入为止。问题是无论用户输入什么,它都会(无限地)进入这个循环。
我关注的代码从第63行开始;这是完整的 .adb 文件:
with ada.text_io; use ada.text_io;
with ada.strings.bounded;
with unchecked_conversion;
with stack;
procedure main is
package int_io is new ada.text_io.integer_io(integer);
stack_type : positive;
stack_size : natural;
begin
loop
-- Select stack type
put_line("Select stack type by inputting corresponding number: ");
put_line("1) Character");
put_line("2) Integer");
put_line("3) Date");
int_io.get(stack_type);
-- Select size of stack
put_line("Enter the maximum size of your stack: ");
int_io.get(stack_size);
case (stack_type) is
-- Character stack (option C/D)
when 1 =>
declare
package char_stack is new stack(stack_size, character); use char_stack;
char : character;
done : integer := 0;
num_chars : natural := 48; -- 48 = ASCII '0'
-- Reflects how many characters (including the number) of the name on top of the stack; used for limit in popping for loop
num_to_pop : natural := 0;
-- Pop and print characters from stack
procedure print_chars is
begin
for i in 0 .. (num_to_pop - 1) loop
put(char_stack.pop);
end loop;
-- Reset num_to_pop
num_to_pop := 0;
end print_chars;
begin
put_line("Enter characters; To finish sequence, terminate with '#': ");
new_line;
while (done /= 1) loop
while (char /= '#') loop
get(char);
if (char /= '#') then
push(char);
num_chars := num_chars + 1;
end if;
end loop;
-- Push number of characters onto stack; reset num_to_pop; reset num_chars
push(character'val(num_chars));
num_to_pop := (character'pos(peek)) - 47;
num_chars := 48;
-- Ask user if they wish to continue
new_line;
put_line("To repeat, enter '0'; to terminate program, enter '1': ");
new_line;
int_io.get(done);
-------------TEST----------------
new_line;
int_io.put(done);
new_line;
---------------------------------
while (done /= 0 or done /= 1) loop
put_line("Please enter either a '0' or a '1'.");
new_line;
int_io.get(done);
end loop;
end loop;
end;
-- Integer stack (option B)
when 2 =>
begin
put("asfsF");
--
end;
-- Date stack (option A)
when 3 =>
begin
put("Asfasdf");
--
end;
when others =>
begin
put("asdfasdF");
end;
end case;
end loop;
end main;
你写
while (done /= 0 or done /= 1) loop
但你应该写
while (done /= 0 and done /= 1) loop
甚至,因为 Ada 在这里不需要括号,
while done /= 0 and done /= 1 loop
或者,应用德摩根定律,
while not (done = 0 or done = 1) loop
我的程序中有一个变量控制用户是否要重复名为 "done." 的程序如果 done = '0',程序重复;如果 done = '1',程序退出。如果用户输入的值不是“0”或“1”,它会进入一个循环,提示他们输入有效的内容,直到他们输入为止。问题是无论用户输入什么,它都会(无限地)进入这个循环。
我关注的代码从第63行开始;这是完整的 .adb 文件:
with ada.text_io; use ada.text_io;
with ada.strings.bounded;
with unchecked_conversion;
with stack;
procedure main is
package int_io is new ada.text_io.integer_io(integer);
stack_type : positive;
stack_size : natural;
begin
loop
-- Select stack type
put_line("Select stack type by inputting corresponding number: ");
put_line("1) Character");
put_line("2) Integer");
put_line("3) Date");
int_io.get(stack_type);
-- Select size of stack
put_line("Enter the maximum size of your stack: ");
int_io.get(stack_size);
case (stack_type) is
-- Character stack (option C/D)
when 1 =>
declare
package char_stack is new stack(stack_size, character); use char_stack;
char : character;
done : integer := 0;
num_chars : natural := 48; -- 48 = ASCII '0'
-- Reflects how many characters (including the number) of the name on top of the stack; used for limit in popping for loop
num_to_pop : natural := 0;
-- Pop and print characters from stack
procedure print_chars is
begin
for i in 0 .. (num_to_pop - 1) loop
put(char_stack.pop);
end loop;
-- Reset num_to_pop
num_to_pop := 0;
end print_chars;
begin
put_line("Enter characters; To finish sequence, terminate with '#': ");
new_line;
while (done /= 1) loop
while (char /= '#') loop
get(char);
if (char /= '#') then
push(char);
num_chars := num_chars + 1;
end if;
end loop;
-- Push number of characters onto stack; reset num_to_pop; reset num_chars
push(character'val(num_chars));
num_to_pop := (character'pos(peek)) - 47;
num_chars := 48;
-- Ask user if they wish to continue
new_line;
put_line("To repeat, enter '0'; to terminate program, enter '1': ");
new_line;
int_io.get(done);
-------------TEST----------------
new_line;
int_io.put(done);
new_line;
---------------------------------
while (done /= 0 or done /= 1) loop
put_line("Please enter either a '0' or a '1'.");
new_line;
int_io.get(done);
end loop;
end loop;
end;
-- Integer stack (option B)
when 2 =>
begin
put("asfsF");
--
end;
-- Date stack (option A)
when 3 =>
begin
put("Asfasdf");
--
end;
when others =>
begin
put("asdfasdF");
end;
end case;
end loop;
end main;
你写
while (done /= 0 or done /= 1) loop
但你应该写
while (done /= 0 and done /= 1) loop
甚至,因为 Ada 在这里不需要括号,
while done /= 0 and done /= 1 loop
或者,应用德摩根定律,
while not (done = 0 or done = 1) loop