关于半角转换全角的问题(100分)

  • 主题发起人 主题发起人 dedragon
  • 开始时间 开始时间
D

dedragon

Unregistered / Unconfirmed
GUEST, unregistred user!
其中对于半角下的句号、单引号和双引号,如何变成中文中的。‘’“”。根据什么判断,其中要结合其他标点符号一起判断。麻烦给出代码
 
//标点符号半角转换全角<br>function TForm1.SignHalfToFull(SingleStr: string): string;<br>var<br>&nbsp; &nbsp;i:integer;<br>&nbsp; &nbsp;temp:string;<br>&nbsp; &nbsp;single:char;<br>&nbsp; &nbsp;sinstr:string;<br>begin<br>&nbsp; &nbsp;temp:='';<br>&nbsp; &nbsp;i:=1;<br>&nbsp; &nbsp;sinstr:=SingleStr;<br>&nbsp; &nbsp;while i&lt;=length(sinstr) do<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; single:=sinstr;<br>&nbsp; &nbsp; &nbsp; if ((ord(single)&lt;128) and (((ord(single)&gt;=32) and (ord(single)&lt;=47)) or ((ord(single)&gt;=58) and (ord(single)&lt;=64)) or ((ord(single)&gt;=91) and (ord(single)&lt;=96)) or ((ord(single)&gt;=123) and (ord(single)&lt;=126)))) then<br>&nbsp; &nbsp; &nbsp; //第33~126号共94个字符,去除英文和数字,其他为标点符号和运算符号<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp:=temp+#163+chr(ord(single)+128);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inc(i);<br>&nbsp; &nbsp; &nbsp; end <br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp:=temp+copy(sinstr,i,1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inc(i);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp; result:=temp;<br>end;<br><br>如何在这基础上改?请指教
 
其中对于半角下的句号、单引号和双引号,<br>如何变成中文中的。‘’“”。<br>.——&gt;。后面的是全角符号——&gt;.<br>'' ——&gt;‘’后面的是全角符号——&gt;''<br>""——&gt;“”后面的是全角符号——&gt;""<br>输入的模式不同一个是半角加上中文标点,一个是切换全角<br>这个用那个全半角转换的函数是不可以的!<br>
 
自动标题的问题我正在写!这个问题你还是找找其他办法吧![:(]
 
参考我以前写的东西,可能有点帮助<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=1687832
 
发错了,对不起
 
建一个替换表<br>"转换成“ 还是 ”有点问题,要根据上下文
 
unit UnitToChinese;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Memo2: TMemo;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>//通用的常规字符到全角字符的转化函数<br>function ToChinesChar(AChar: Char): WChar;<br>begin<br>&nbsp; Result := WideChar(65248 + Ord(AChar));<br>end;<br><br>//简单的转换函数,复杂的话恐怕要加上脚本标记才行,<br>//因为有很多情况全角字符对应半角字符是多对一的关系<br>//例如:&lt; 《 < 〈 &nbsp;≮ ≤;或者:[ 【 [ 〔 〖 「 『<br>//<br>function ConvertToChinses(ASource: string): string;<br>var<br>&nbsp; PSource &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : PChar;<br>&nbsp; TmpChar &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : string;<br>&nbsp; DoubleFH &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Boolean;<br>begin<br>&nbsp; Result := '';<br>&nbsp; PSource := PChar(ASource);<br>&nbsp; DoubleFH := True;<br>&nbsp; while not (PSource^ = #0) do<br>&nbsp; begin<br>&nbsp; &nbsp; case PSource^ of<br>&nbsp; &nbsp; &nbsp; '(', ')', '[', ']', '{', '}', '-', '$', ':', '!', '?', ';':<br>&nbsp; &nbsp; &nbsp; &nbsp; TmpChar := ToChinesChar(PSource^);<br>&nbsp; &nbsp; &nbsp; '.':<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((PSource - 1)^ = '.') or ((PSource + 1)^ = '.') then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TmpChar := '·'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//省略号<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TmpChar := '。'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//句号<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; '/':<br>&nbsp; &nbsp; &nbsp; &nbsp; TmpChar := '、'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//顿号<br>&nbsp; &nbsp; &nbsp; '"':<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if DoubleFH then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TmpChar := '“'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//左双引号<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TmpChar := '”'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//右双引号<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoubleFH := not DoubleFH;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; '''':<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if DoubleFH then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TmpChar := '‘'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//左单引号<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TmpChar := '’'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//右单引号<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoubleFH := not DoubleFH;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; '&lt;':<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TmpChar := '《'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//左书名号<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; '&gt;':<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TmpChar := '》'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//右书名号<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; TmpChar := PSource^;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Result := Result + TmpChar;<br>&nbsp; &nbsp; Inc(PSource);<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; Memo2.Text := ConvertToChinses(Memo1.Text);<br>end;<br><br>end.<br><br>
 
[:(]哎!又走回穷举了……
 
不一定要用穷举吧,只把这几个符号做特殊的处理应该也行吧,我也还在测试中<br>谢谢各位的帮忙,再看看再分分吧
 
哈哈,终于改好了,谢谢各位的帮忙。因为我不懂指针,所以wr960204朋友的方法我也没用,还是按照自己的方法改好了,程序如下,为了方便以后有朋友需要使用。<br><br>function TForm1.SignHalfToFull(SingleStr: string): string;<br>var<br>&nbsp; &nbsp;i:integer;<br>&nbsp; &nbsp;temp,sinstr:string;<br>&nbsp; &nbsp;single:char;<br>&nbsp; &nbsp;HalfMark,FullMark:boolean;<br>begin<br>&nbsp; &nbsp;temp:='';<br>&nbsp; &nbsp;i:=1;<br>&nbsp; &nbsp;sinstr:=SingleStr;<br>&nbsp; &nbsp;HalfMark:=true;<br>&nbsp; &nbsp;FullMark:=true;<br>&nbsp; &nbsp;while i&lt;=length(sinstr) do<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; single:=sinstr;<br>&nbsp; &nbsp; &nbsp; if ((ord(single)&lt;128) and (((ord(single)&gt;=32) and (ord(single)&lt;=47)) or ((ord(single)&gt;=58) and (ord(single)&lt;=64)) or ((ord(single)&gt;=91) and (ord(single)&lt;=96)) or ((ord(single)&gt;=123) and (ord(single)&lt;=126)))) then<br>&nbsp; &nbsp; &nbsp; //第33~126号共94个字符,去除英文和数字,其他为标点符号和运算符号<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if ord(single)=46 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp:=temp+'。';<br>&nbsp; &nbsp; &nbsp; &nbsp; if ord(single)=60 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp:=temp+'《';<br>&nbsp; &nbsp; &nbsp; &nbsp; if ord(single)=62 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp:=temp+'》';<br>&nbsp; &nbsp; &nbsp; &nbsp; if ord(single)=39 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if HalfMark=true then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp:=temp+'‘'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else temp:=temp+'’';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HalfMark:=Not HalfMark;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; if ord(single)=34 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if FullMark=true then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp:=temp+'“'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else temp:=temp+'”';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FullMark:=Not FullMark;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; if ((ord(single)&lt;&gt;34) and (ord(single)&lt;&gt;39) and (ord(single)&lt;&gt;46) and (ord(single)&lt;&gt;60) and (ord(single)&lt;&gt;62)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp:=temp+#163+chr(ord(single)+128);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inc(i);<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp:=temp+copy(sinstr,i,1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Inc(i);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp; result:=temp;<br>end;
 
多人接受答案了。
 

Similar threads

回复
0
查看
1K
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
844
DelphiTeacher的专栏
D
后退
顶部