質問

I want to get the user birthday as a input and break in to parts. like year, month , date. how to do it. Please help me. I have no idea how to do it.

Thanks in advance.

役に立ちましたか?

解決 2

Make user to input date as string and use parse_time/2 and stamp_date_time/3:

Example:

?- parse_time('2012-05-05 12:56:33', Stamp), 
stamp_date_time(Stamp, DateTime, 'UTC'), 
DateTime = date(Year, Month, Day, Hour, Minute, Seconds, _, _, _).

Stamp = 1336222593.0,
DateTime = date(2012, 5, 5, 12, 56, 33.0, 0, 'UTC', -),
Year = 2012,
Month = Day, Day = 5,
Hour = 12,
Minute = 56,
Seconds = 33.0.

User doesn't need to enter whole date and time. Year, month and day is enough for parse_time/2 to work. There are also other predicates to manipulate date and time. See SWI-Prolog documentation here: Time and date predicates

UPDATE:

go :-
    write('Please enter your birthday [YYYY-MM-DD]'), nl,
    read_string(Birthday),
    parse_time(Birthday, Stamp),
    stamp_date_time(Stamp, DateTime, 'UTC'),
    DateTime = date(Year, Month, Day, _, _, _, _, _, _),
    print('Year: '), print(Year), nl,
    print('Month: '), print(Month), nl,
    print('Day: '), print(Day), nl.

read_string(String) :-
    current_input(Input),
    read_line_to_codes(Input, Codes),
    string_codes(String, Codes).

Sample input and output:

?- go.
Please enter your birthday [YYYY-MM-DD]
|: 1985-01-09
Year: 1985
Month: 1
Day: 9
true.

他のヒント

here is an alternative, but I must say that I would prefer the method indicated by Grzegorz:

?- read_line_to_codes(user_input,L), phrase((integer(Y),"-",integer(M),"-",integer(D)),L).
|: 2012-12-22
L = [50, 48, 49, 50, 45, 49, 50, 45, 50|...],
Y = 2012,
M = 12,
D = 22.

you must 'include' the appropriate library before...

?- [library(dcg/basics)].
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top