With Maple how can i prompt the user to answer a boolean expression?

别来无恙 提交于 2019-12-12 01:09:30

问题


The exact question would be "is the equation you want to use x=f(Xo)". This is in an if statement already so if true then continue if not then prompt user to enter a different function.


回答1:


Your bit about its already being inside an if statement isn't very workable because that doesn't allow for an alternative value to be assigned to something in the case that the initial response is negative.

You should be able to work with something like this. Call p(), and assign its result to a ans, say, and then work with that value (and/or test it for some properties).

restart:

p := proc()
local answer, oldprompt, res1, res2;
  oldprompt := interface(':-prompt'=``);
  try
    printf("Is the equation you want to use x=f(Xo)?  (y/n)\n");
    res1 := readline(-1);
    if member(res1,{"y;","y","yes;","yes"}) then
      answer := x=f(Xo);
    elif member(res1,{"n;","n","no;","no"}) then
      printf("Enter your equation.\n");
      res2 := readline(-1);
      answer := parse(res2);
    else
      printf("Response not recognized\n");
    end if;
  catch:
  finally
    interface(':-prompt'=oldprompt);
  end try;
  if answer='answer' then NULL else answer end if;
end proc:

ans := p();

[edited below]

It is possible to get it a little closer to your original. With procedure p as below the returned result will be one of true/false/FAIL and could be used in a conditional. In the case that the return values if false (because of the response to the initial query) then a second query is made about the choice of another expression.

This version of p takes two arguments, the first is the suggested initial equation. The second is a name which can be assigned any alternative.

restart:

p := proc(candidate, resultvar)
local result, oldprompt, res1, res2;
  oldprompt := interface(':-prompt'=``);
  try
    printf(sprintf("Is the equation you want to use %a?  (y/n)\n",
                   candidate));
    res1 := readline(-1);
    if member(res1,{"y;","y","yes;","yes"}) then
      result := true;
      assign(resultvar,candidate);
    elif member(res1,{"n;","n","no;","no"}) then
      result := false;
      printf("Enter your equation.\n");
      res2 := readline(-1);
      assign(resultvar,parse(res2));
    else
      printf("Response not recognized\n");
      result := FAIL;
    end if;
  catch:
  finally
    interface(':-prompt'=oldprompt);
  end try;
  return result;
end proc:

Now we can test it out.

p(x=f(X0), 'ans');

ans;

We could also use the call to p inside an if statement. Eg,

if p(x=f(X0), 'ans') then
   "accepted";
else
   "new choice made";
end if;

ans;

Here, answering "n" to the first query will make the conditional test see a false value, but the named argument ans will get assigned to as a side-effect.



来源:https://stackoverflow.com/questions/21506372/with-maple-how-can-i-prompt-the-user-to-answer-a-boolean-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!