试用EF开发WEB应用程序(7): Query String应用,简单计算器
来源:互联网 发布:中卫 大数据 编辑:程序博客网 时间:2024/05/21 05:21
题记:用“易语言.飞扬”(EF)开发WEB应用程序,此前还没有先例。但因为EF本地开发包(EFNDK)已经发布,用C/C++开发一个EF类库,使其支持EF开发WEB应用程序,应该并非难事。当然也可想而知,其中必有诸多难点有待解决。此系列文章,为本人探索过程之记录,对外人未必有多大价值。如有网友乐观其事,还请理性待之。作者:liigo。转载请务必注明出处:http://blog.csdn.net/liigo/。在线留言。
试用EF开发WEB应用程序(7): Query String应用,简单计算器
前面的系列文章讲了很多Query String,现在给大家看一个Query String的实际应用,简单计算器。这个小小的网络程序功能很简单,就是对用户输入的两个数值进行加减乘除运算,输出计算结果。
下面请看“易语言.飞扬”源代码:
引入 fastcgi;
公开 类 启动类
{
公开 静态 启动()
{
FCGI fcgi = new FCGI;
while(fcgi.Accept() >= 0)
{
文本 html = html_template.替换全部("$(title)", "简单计算器(由EF开发)");
文本 result = "?";
if(fcgi.QUERY_STRING != "")
result = doCalc(fcgi.QUERY_STRING("op"), fcgi.QUERY_STRING("op1"), fcgi.QUERY_STRING("op2"));
html = html.替换("$(op1)", fcgi.QUERY_STRING("op1"));
html = html.替换("$(op2)", fcgi.QUERY_STRING("op2"));
html = html.替换("$(result)", result);
html = setSelectedOp(html, fcgi.QUERY_STRING("op"));
fcgi.Output(html.到UTF8());
}
}
静态 文本 doCalc(文本 op, 文本 op1, 文本 op2)
{
假如(op)
{
为 "+": 返回 (op1.到小数() + op2.到小数()).到文本();
为 "-": 返回 (op1.到小数() - op2.到小数()).到文本();
为 "*": 返回 (op1.到小数() * op2.到小数()).到文本();
为 "/": 返回 (op1.到小数() / op2.到小数()).到文本();
为其他: 返回 "[error]";
}
}
静态 文本 setSelectedOp(文本 html, 文本 op)
{
文本 s1,s2,s3,s4;
假如(op)
{
为 "+": s1 = "selected";
为 "-": s2 = "selected";
为 "*": s3 = "selected";
为 "/": s4 = "selected";
}
html = html.替换("$(isSelected+)", s1);
html = html.替换("$(isSelected-)", s2);
html = html.替换("$(isSelected*)", s3);
html = html.替换("$(isSelected/)", s4);
返回 html;
}
常量 文本 html_template = ["Content-type: text/html
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>$(title)</title>
</head>
<body>
<h1>$(title)</h1>
<hr></hr>
<form>
<input type="text" name="op1" size=10 maxlength=10 value="$(op1)">
<select name="op">
<option value="+" $(isSelected+)> + </option>
<option value="-" $(isSelected-)> - </option>
<option value="*" $(isSelected*)> * </option>
<option value="/" $(isSelected/)> / </option>
</select>
<input type="text" name="op2" size=10 maxlength=10 value="$(op2)">
= $(result)
<p><input type="submit" value="计算"></input></p>
</form>
<hr></hr>
<p>by liigo, <a href="http://blog.csdn.net/liigo/">http://blog.csdn.net/liigo/</a></p>
</body>
</html>
"];
}
公开 类 启动类
{
公开 静态 启动()
{
FCGI fcgi = new FCGI;
while(fcgi.Accept() >= 0)
{
文本 html = html_template.替换全部("$(title)", "简单计算器(由EF开发)");
文本 result = "?";
if(fcgi.QUERY_STRING != "")
result = doCalc(fcgi.QUERY_STRING("op"), fcgi.QUERY_STRING("op1"), fcgi.QUERY_STRING("op2"));
html = html.替换("$(op1)", fcgi.QUERY_STRING("op1"));
html = html.替换("$(op2)", fcgi.QUERY_STRING("op2"));
html = html.替换("$(result)", result);
html = setSelectedOp(html, fcgi.QUERY_STRING("op"));
fcgi.Output(html.到UTF8());
}
}
静态 文本 doCalc(文本 op, 文本 op1, 文本 op2)
{
假如(op)
{
为 "+": 返回 (op1.到小数() + op2.到小数()).到文本();
为 "-": 返回 (op1.到小数() - op2.到小数()).到文本();
为 "*": 返回 (op1.到小数() * op2.到小数()).到文本();
为 "/": 返回 (op1.到小数() / op2.到小数()).到文本();
为其他: 返回 "[error]";
}
}
静态 文本 setSelectedOp(文本 html, 文本 op)
{
文本 s1,s2,s3,s4;
假如(op)
{
为 "+": s1 = "selected";
为 "-": s2 = "selected";
为 "*": s3 = "selected";
为 "/": s4 = "selected";
}
html = html.替换("$(isSelected+)", s1);
html = html.替换("$(isSelected-)", s2);
html = html.替换("$(isSelected*)", s3);
html = html.替换("$(isSelected/)", s4);
返回 html;
}
常量 文本 html_template = ["Content-type: text/html
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>$(title)</title>
</head>
<body>
<h1>$(title)</h1>
<hr></hr>
<form>
<input type="text" name="op1" size=10 maxlength=10 value="$(op1)">
<select name="op">
<option value="+" $(isSelected+)> + </option>
<option value="-" $(isSelected-)> - </option>
<option value="*" $(isSelected*)> * </option>
<option value="/" $(isSelected/)> / </option>
</select>
<input type="text" name="op2" size=10 maxlength=10 value="$(op2)">
= $(result)
<p><input type="submit" value="计算"></input></p>
</form>
<hr></hr>
<p>by liigo, <a href="http://blog.csdn.net/liigo/">http://blog.csdn.net/liigo/</a></p>
</body>
</html>
"];
}
有机会将提供在线演示地址(欢迎推荐支持FastCGI的优秀的虚拟空间),现在只能先看图片了,下面是运行结果:
认为以上EF代码太多太复杂的想法可以休矣。我写这个程序时,花费了很多的笔墨,用于提高程序可操作性,其中包括:自带FORM,记忆上次提交的操作数和运算符,显示计算结果。如果仅仅只是显示计算结果的话,代码其实可以大大地简化:
引入 fastcgi;
公开 类 启动类
{
公开 静态 启动()
{
FCGI fcgi = new FCGI;
while(fcgi.Accept() >= 0)
{
文本 op1 = fcgi.QUERY_STRING("op1");
文本 op2 = fcgi.QUERY_STRING("op2");
文本 op = fcgi.QUERY_STRING("op");
fcgi.Output("Content-type: text/plain ");
if(op1 != "" && op2 != "" && op != "")
fcgi.Output(op1 + " " + op + " " + op2 + " = " + doCalc(op, op1, op2));
else
fcgi.Output("[parameters error]");
}
}
静态 文本 doCalc(文本 op, 文本 op1, 文本 op2)
{
假如(op)
{
为 "+": 返回 (op1.到小数() + op2.到小数()).到文本();
为 "-": 返回 (op1.到小数() - op2.到小数()).到文本();
为 "*": 返回 (op1.到小数() * op2.到小数()).到文本();
为 "/": 返回 (op1.到小数() / op2.到小数()).到文本();
为其他: 返回 "[error]";
}
}
}
公开 类 启动类
{
公开 静态 启动()
{
FCGI fcgi = new FCGI;
while(fcgi.Accept() >= 0)
{
文本 op1 = fcgi.QUERY_STRING("op1");
文本 op2 = fcgi.QUERY_STRING("op2");
文本 op = fcgi.QUERY_STRING("op");
fcgi.Output("Content-type: text/plain ");
if(op1 != "" && op2 != "" && op != "")
fcgi.Output(op1 + " " + op + " " + op2 + " = " + doCalc(op, op1, op2));
else
fcgi.Output("[parameters error]");
}
}
静态 文本 doCalc(文本 op, 文本 op1, 文本 op2)
{
假如(op)
{
为 "+": 返回 (op1.到小数() + op2.到小数()).到文本();
为 "-": 返回 (op1.到小数() - op2.到小数()).到文本();
为 "*": 返回 (op1.到小数() * op2.到小数()).到文本();
为 "/": 返回 (op1.到小数() / op2.到小数()).到文本();
为其他: 返回 "[error]";
}
}
}
上面这个代码就简单多了,当然可操作性就弱了很多,只能显示计算结果而已。使用此程序时需提供URL参数或通过FORM提交参数。这个简化后的“简单计算器”(在线演示)的执行结果见下图:
下文预告:CGI环境变量
- 试用EF开发WEB应用程序(7): Query String应用,简单计算器
- 试用EF开发WEB应用程序(3): Query String (附图)
- 试用EF开发WEB应用程序(4): 缓存Query String
- 试用EF开发WEB应用程序(6): 解析Query String中的各参数值
- 试用EF开发WEB应用程序(15): EF Servlet, or EFSP?
- 试用EF开发WEB应用程序(2): Hi, FastCGI (附图)
- 试用EF开发WEB应用程序(8): CGI环境变量
- 试用EF开发WEB应用程序(9): 在线演示
- 试用EF开发WEB应用程序(10): 配置Apache
- 试用EF开发WEB应用程序(11): 在线留言系统
- 试用EF开发WEB应用程序(12): 网页计数器
- 试用EF开发WEB应用程序(1): 方案选择,为什么是FastCGI
- 试用EF开发WEB应用程序(5): URL编码和URL解码
- jsp简单应用:web计算器
- 如何应用ajax开发web应用程序(3)
- Web应用程序实践:数据库应用开发
- Android开发应用实例:简单计算器+多点触控
- PowerBuilder开发简单计算器
- Inside ObjectBuilder 范例
- 软件测试内容全貌——全景图 (1)
- Inside ASP.NET 2.0 – Controls Model
- 面对当今的研究生教育——只有无奈
- Windows API一日一练(87)CreateProcess函数
- 试用EF开发WEB应用程序(7): Query String应用,简单计算器
- Windows API一日一练(86)GetClipboardData函数
- Inside ObjectBuilder Part 4
- 如何定义测试用例的质量标准?
- 理解web服务模式(一):应该采用哪一种 WSDL样式?
- Windows API一日一练(85)OpenClipboard函数
- Developing ASP.NET Custom Control With C# Builder铪
- Windows API一日一练(84)FlushInstructionCache函数
- 《软件过程管理》电子课件