受这个问题的启发。
创建一个程序,提示用户存储一些数据,当程序退出时,吐出程序本身,除非会话数据发生了更改。
然后,用户打开新生成的程序,并可以回忆以前程序中的数据。
命令
KEY VALUE:将会话变量KEY设置为VALUE*:清除所有数据! KEY:删除KEY? KEY:查询KEY (如果不存在:不打印并继续前进)键或值都不能包含任何空格。新生成的程序的文件名必须标识程序的版本,可以使用日期或计数器。
示例交互:
name test store name = test
data is now { name: test }
0 1 data is now { name: test, 0: 1 }
? name output: test
! 0 delete 0
data is now { name: test }
hello good world data is now { name: test, hello: good }
the extra word "world" is ignored
egiwiwegiuwe the "otherwise" case: quit program用户打开新生成的程序。
? name output: test
name retest data is now { name: retest }
* clear
data is now { }示例实现:https://gist.github.com/1128876
规则
发布于 2011-08-06 13:13:54
enum c=q{string[string] m;import std.stdio;import std.array;void main(){foreach(string s;lines(stdin)){auto a=s.split;if(!a.length)goto e;switch(a[0]){case "*":m.clear;break;case "!":m.remove(a[1]);break;case "?":writeln(m.get(a[1],""));break;default:if(a.length<2){goto e;}m[a[0]]=a[1];}stdout.flush;}e:write("static this(){");foreach(i,v;m)writef("m[`%s`]=`%s`;",i,v);write("}enum c=q{",c,"};mixin(c);");}};mixin(c);格式化:
enum c=q{
string[string] m;
import std.stdio;
import std.array;
void main(){
foreach(string s;lines(stdin)){
auto a=s.split;
if(!a.length)goto e;
switch(a[0]){
case "*":m.clear;break;
case "!":m.remove(a[1]);break;
case "?":writeln(m.get(a[1],""));break;
default:if(a.length<2){goto e;}m[a[0]]=a[1];
}
stdout.flush;
}
e:write("static this(){");
foreach(i,v;m)writef("m[`%s`]=`%s`;",i,v);
write("}enum c=q{",c,"};mixin(c);");
}
};mixin(c);我的丁奎因变体
*命令依赖于m.clear;才能正确工作,而在DMD2.52(编译器中的错误)中却不正确。
对stdout.flush;的需求取决于是否启用了自动刷新(它不在我的机器上)
发布于 2011-08-08 08:21:57
(function(o,N){while(a=prompt()){a=a.split(' ')
b=a[0]
c=a[1]
if(b=='*')o={}
else if(b=='?'){if(o[c]!=N)alert(o[c])}
else if(b=='!')delete o[a[1]]
else if(c!=N)o[b]=c
else break}alert('('+arguments.callee+')('+JSON.stringify(o)+')')}({}))https://codegolf.stackexchange.com/questions/3351
复制相似问题