每行的开头要加上空格或制表符,很麻烦。
如果要培灶羡显示行号的话,就更麻烦了。
因此,我用 C# 语言写了小程序,建设一个 ASP.NET 4 网站来解决上述两个麻烦:
在这个网页中:
Line Count 复选框表示是否需要加上行号。
Prefix 中的的 Space 和 Tab 无线按钮让你选择每行开头是增加空格还是制表符。
Prefix Count 文本框让你输入缩进的层次。默认是缩进一层 。配拍但是如果遇到在有序列表或无序列表中的程序代码,就需要缩进两层,甚至更多层了。
这个网站的总体结构如下所示:
网站的配置文件 Web.config 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<httpRuntime requestValidationMode="2.0" />
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
</system.web>
</configuration>
网站的 Web 页面文件 CodeFormat.aspx 如下所示:
<%@ Page validateRequest="false" Language="C#" inherits="Skyiv.Ben.Web.CodeFormatPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />
<title>Code Format</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button Onclick="Submit" Text="Submit" Runat="Server" />
<span style="background-color:LightBlue">
<asp:CheckBox Id="chkLineCount" Text="Line Count" Checked="True" Runat="Server" />
</span>
<span style="background-color:LightBlue">
Prefix:
<asp:RadioButton Id="rbnSpace" Text="Space" Checked="True"
GroupName="Prefix" Runat="Server" />
<asp:RadioButton Id="rbnTab" Text="Tab"
GroupName="Prefix" Runat="Server" />
</span>
<span style="background-color:LightBlue">
Prefix Count:
<asp:TextBox Runat="Server" Id="tbxLevel" Text="1" Columns="2" MaxLength="1" />
</span>
<hr />
<div>
<asp:TextBox Runat="Server" Id="tbxInput" Wrap="False"
TextMode="MultiLine" Columns="80" Rows="10" />
<br />
<asp:TextBox Runat="Server" Id="tbxOutput" ReadOnly="True" Wrap="False"
TextMode="MultiLine" BackColor="LightBlue" Columns="80" Rows="10" />
</div>
</form>
</body>
</html>
以及对应的后台 C# 代码 CodeFormat.aspx.cs:
1: using System
2: using System.IO
3: using System.Web
4: using System.Web.UI
5: using System.Web.UI.WebControls
6: using Skyiv.Utils
7:
8: namespace Skyiv.Ben.Web
9: {
10: public class CodeFormatPage : Page
11: {
12: protected TextBox tbxInput
13: protected TextBox tbxOutput
14: protected TextBox tbxLevel
15: protected CheckBox chkLineCount
16: protected RadioButton rbnTab
17:
18: protected void Page_Load(object sender, EventArgs e)
19: {
20: tbxOutput.Text = string.Format(" OS: {1} ({2}-bit){0}CLR: {3}",
21: Environment.NewLine, Environment.OSVersion,
22: Environment.Is64BitOperatingSystem ? 64 : 32,
23: Environment.Version)
24: }
25:
26: protected void Submit(object sender, EventArgs e)
27: {
28: var writer = new StringWriter()
29: new CodeFormat(new StringReader(tbxInput.Text),
30: writer).Run(chkLineCount.Checked, rbnTab.Checked, GetLevel(tbxLevel.Text))
31: tbxOutput.Text = writer.ToString()
32: }
33:
34: int GetLevel(string str)
35: {
36: int n
37: if (!int.TryParse(str, out n)) n = 1
38: return Math.Min(5, Math.Max(0, n))
39: }
40: }
41: }
#include <stdio.h>坦伍main()
{
int i=1,flag=0
double a=0,b=0,result=0
char sym
printf("(%d)\n",i)
scanf("%lf%c%lf",&a,&sym,&b)
while(sym!='#')
{
switch(sym)
{
case '+':
result=a+b
break
case '-':
result=a-b
break
case '让察或*':
result=a*b
break
case '/':
result=a/b
break
default :
if(b==0)flag=2
}
if(flag==0)
{
printf("%lf\没弊n",result)
i++
}
else flag=2
printf("(%d)\n",i)
scanf("%lf%c%lf",&a,&sym,&b)
}
}
图灵机停机问题(The Halting Problem)的不可判定性图灵机停机问题: 能否给出一个判断任意一个图灵机是否停机的一般方法? 答案是NO.
这个问题实际上是问: 是否存在一台"万能的"图灵机 H, 把任意一台图灵机 M 输入给 H, 它都能判定 M 最终慎亮是否停机, 输出一个明确的 "yes" 或 "no" 的答案? 可以利用反证法来证明这样的 H 不可能存在. 假定存在一个能够判定任意一台图灵机是否停机的万能图灵机 H(M), 如果 M 最终停机, H 输宽袜宽出 "halt"如果 M 不停机, H 输出 "loop". 我们把 H 当作子程序, 构造如下程序 P:
function P(M) {
if (H(M)=="loop") return "halt"
else if (H(M)=="halt") while(true)// loop forever
}
因为 P 本身也是一台图灵机, 可以表示为一个字符串, 所以我们可以把 P 输入给它自己, 然后问 P(P) 是否停机. 按照好烂程序 P 的流程, 如果 P 不停机无限循环, 那么它就停机, 输出"halt"如果 P 停机, 那么它就无限循环, 不停机这样无论如何我们都将得到一个矛盾, 所以假设前提不成立, 即不存在这样的 H. 或者说, 图灵机停机问题是不可判定的(undecidable).
以上摘自http://hpguo2005.spaces.live.com/blog/cns!edcc7e8a4dfd15ce!132.entry
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)