序言,用户选择咖啡菜单 [英] prolog, user choice coffee menu

查看:81
本文介绍了序言,用户选择咖啡菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Prolog课程的初学者,我正在尝试编写代码,以允许用户选择咖啡,然后询问是否冷热,然后询问咖啡的大小来计算价格.我在网上查看有关如何开发该程序的说明,但我觉得它与示例中所需要的有所不同:[动物识别] [1].你能帮我写咖啡菜单吗?

I'm a beginner in Prolog course, I am trying to write code to allow the user to choose the coffee then ask hot or cold then the size of the coffee to calculate price. I was looking on the web explain in how to develop the program but I feel it's different from what I need in the example: [animal identification][1]. Can you please help me to write the coffee menu.

这是我尝试过的.

  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*/

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

Is my method correct or better to create a list and then the user chooses by type the name 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).

推荐答案

这很简单.

您首先需要一张期权和价格表,但是在Prolog中,这些可以作为事实简单地完成.

You first need a table of options and prices, but in Prolog these can be done simply as facts.

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).

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

Next you need to decide on the arguments for the predicate, in this case that is easy, a list of options for the input and a price for the output.

coffeeOrder(Options,Price)

由于存在选项列表,因此代码需要处理列表,而对于初学者而言,最简单的方法之一就是使用递归调用.一组递归谓词遵循基本情况的模式

Since there are a list of options the code needs to process a list and one of the easiest ways for a beginner is to use a recursive call. A recursive set of predicates follows the pattern of a base case

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

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

and a predicate to handle processing the list recursively

% 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.

When generating a value, in this case the final price, and using a recursive call, often a helper predicate is needed to set up an initial value, in this case the initial cost which is 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.

所有代码作为一个片段.

All of the code as one snippet.

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).

这篇关于序言,用户选择咖啡菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆