【问题标题】:prolog, user choice coffee menuprolog,用户选择咖啡菜单
【发布时间】:2018-11-26 00:14:32
【问题描述】:

我是 Prolog 课程的初学者,我正在尝试编写代码让用户选择咖啡,然后询问冷热咖啡的大小来计算价格。我在网上寻找如何开发程序的解释,但我觉得它与我在示例中需要的不同:[动物识别][1]。你能帮我写咖啡菜单吗?

这是我尝试过的。

  go :- hypothesize(Coffee),
  write('Your order is : '),
  write(Coffee),
  write('and the price for your order =  : ')
  nl,
  undo.

    /* hypotheses to be tested */
  hypothesize(moca)   :- moca, !.
  hypothesize(hotChocolate)     :- hotChocolate, !.
  hypothesize(latte)   :- latte, !.
   hypothesize(cappuccino)     :- cappuccino, !.

  /*   rules */
  moca :-
      /* ask if you want hot or cold
       * ask the size of the coffee*/

我的方法是正确还是更好地创建一个列表,然后用户通过键入咖啡的名称来选择?

像这样添加菜单

    menu :- repeat,
    write('pleaase, Choose the Coffe to order:'),nl,
    write('1. Moca'),nl,
    write('2. Latte'),nl,
    write('3. Hot Choclate'),nl,

    write('Enter your choice number please: '),nl,
    read(Choice),
    run_opt(Choice).

【问题讨论】:

  • 您好,欢迎来到 StackOverflow。这是为了家庭作业还是自己做的?如果这是供您自己使用,您可以通过放弃输入提示并首先专注于逻辑来使生活更轻松。
  • 感谢您的回复,这是家庭作业。我应该如何专注于逻辑?
  • 作为家庭作业,如果您放弃作业以便我们了解要求,将会很有帮助。我认为您有一个选择列表和每个选择的价格,甚至可能是取决于两个或多个选项的价格。
  • 当我编写 Prolog 代码时,我倾向于先从低级逻辑开始,如果需要,再到用户界面。在这种情况下,它是必需的。
  • 基本上这个逻辑会接受一个选项列表,遍历列表来调整价格并返回一个最终价格。然后,您可以添加用户界面来收集选项列表,然后输出最终价格。

标签: list menu prolog


【解决方案1】:

这很简单。

您首先需要一个选项和价格表,但在 Prolog 中,这些可以简单地作为事实来完成。

price(moca,2.0).
price(hotChocolate,1.5).
price(latte,2.5).
price(cappuccino,3.0).

price(cold,0.1).
price(hot,0.5).

price(short,1.0).
price(tall,1.5).
price(grande,2.0).
price(venti,2.5).
price(trenta,3.0).

接下来,您需要确定谓词的参数,在这种情况下很简单,输入选项列表和输出价格。

coffeeOrder(Options,Price)

由于有一个选项列表,代码需要处理一个列表,而对于初学者来说,最简单的方法之一是使用递归调用。一组递归谓词遵循基本案例的模式

% Do something when the list is empty.
coffeeOptions([], ... ). 

以及一个用于递归处理列表的谓词

% Do something when the list is not empty.
coffeeOptions([H|T],PriceIn,PriceOut) :-
    % do something with the head, H
    coffeeOptions(T,NewPrice,PriceOut).

当生成一个值时,在这种情况下是最终价格,并且使用递归调用,通常需要一个辅助谓词来设置一个初始值,在这种情况下初始成本是 0.0。

所以谓词是:

coffeeOrder(Options,Price) :-
    coffeeOptions(Options,0.0,Price).  % This sets the initial price to 0.0.

% Do something when the list is empty.
coffeeOptions([],Price,Price).

% Do something when the list is not empty.
coffeeOptions([Option|T],Price0,Price) :-
    price(Option,Cost),
    Price1 is Price0 + Cost,
    coffeeOptions(T,Price1,Price).

还有一个快速测试。

?- coffeeOrder([moca,hot,grande],Price).
Price = 4.5.

所有代码作为一个sn-p。

coffeeOrder(Options,Price) :-
    coffeeOptions(Options,0.0,Price).

coffeeOptions([],Price,Price).

coffeeOptions([Option|T],Price0,Price) :-
    price(Option,Cost),
    Price1 is Price0 + Cost,
    coffeeOptions(T,Price1,Price).

price(moca,2.0).
price(hotChocolate,1.5).
price(latte,2.5).
price(cappuccino,3.0).

price(cold,0.1).
price(hot,0.5).

price(short,1.0).
price(tall,1.5).
price(grande,2.0).
price(venti,2.5).
price(trenta,3.0).

【讨论】:

  • 感谢您提供信息丰富的解释,但如何设置菜单以供用户选择
  • @ADH 就像说的那样,这不应该变成讨论。请提出一个新问题。
  • ok :( ,谢谢你的回复,你的回答很有帮助。
  • @ADH 你说谢谢,但你没有给我接受投票或赞成票。
  • 我做了但是根据声誉没有显示而是记录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-05
  • 2016-06-15
  • 1970-01-01
  • 2023-03-03
  • 2012-08-24
  • 1970-01-01
相关资源
最近更新 更多