请求建议如何清理C代码以验证字符串表示整数 [英] Requesting advice how to clean up C code for validating string represents integer

查看:50
本文介绍了请求建议如何清理C代码以验证字符串表示整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究几种语言的编程示例,所有这些都是在CGI下运行的
(除了PHP),这样我就可以显示来源

文件和在线实际运行的例子。在解码HTML FORM内容之后,第一组

示例仅仅是

验证字段中的文本以确保它是有效的

表示整数,没有抛出任何垃圾,即它必须满足正则表达式:^ * [ - +]?[0-9] + * $


如果该字段的内容有误,我想尽可能多地诊断

合理的错误,而不仅仅是说语法错误。


因为perl和PHP包含对正则表达式的支持,所以它很明显是如何做到的,并且很容易实现:
http://www.rawbw.com/~rem/HelloPlus/...html# h4intperl
http: //www.rawbw.com/~rem/HelloPlus/....html#h4intphp


因为Common Lisp具有良好的实用性es用于扫描字符串,大多数是

使用position,position-if和position-if-not,它同样很简单,而且同样显而易见的是,如何做到这一点:
http://www.rawbw .com / ~rem / HelloPlus / ... html#h4intlisp


Java API缺少一些可用的函数

Lisp,所以我不得不增加API,但是它就像在

Common Lisp中一样简单,使用几乎相同的算法:
http://www.rawbw.com/~rem/HelloPlus/...html#h4intjava


现在我们来到C:我现在有一个可怕的混乱:
http://www.rawbw.com/~rem/HelloPlus/...4s.html#h4intc

我想要拔掉所有的东西e字符案例测试到一个

函数,它将一个字符转换为一个类号(例如1

表示空格,2表示数字,3表示符号等。 ),调用遍布

的地方,并在结果上使用SELECT语句,

不会改变代码的逻辑但可能会成功更整洁。

或者,我可以手工编写Lisp / Java

实用程序的替代品,用于扫描字符串,或者在其中一个C / $ $ b $中找到一些东西。 b库可以提供帮助,然后将Lisp或Java代码翻译成
到C.你们中有没有其他想法我可以做些什么来清理

up the C码?不要为我编写代码,但只是提示一下

库例程可能会为我做90%的工作,或者建议

重新设计算法?我不想做的一件事是

为C下载一个REGEX包。我试图举例说明

如何从头开始做事C,不是如何简单地使用某人

else'的程序,即使REGEX模块的源是

可用。如果某个东西不在C的标准库中,那么

它不会出现在这个项目中。 (我做的唯一

例外是用于收集和解码HTML的模块

FORM内容,这是整个项目的先决条件。)

解决方案




如果该字段的内容有误,我想尽可能多地诊断

什么是错的,而不仅仅是说语法错误。


因为perl和PHP包含对正则表达式的支持,所以

很明显怎么样做到这一点,很容易实现:
http://www.rawbw.com/~rem/HelloPlus/...html#h4intperl
http://www.rawbw.com/~rem/HelloPlus/....html#h4intphp


因为Common Lisp具有良好的扫描字符串实用程序,大多数是使用position,position-if和posi来获得
如果不是,它同样很容易,同样显而易见,如何做到这一点:
http://www.rawbw.com/~rem/HelloPlus/...html#h4intlisp


Java API缺少Common

Lisp中可用的一些函数,所以我不得不增加API,但是它就像在
中一样简单
Common Lisp,几乎相同的算法:
http://www.rawbw.com/~rem/HelloPlus/...html#h4intjava


现在我们来到C:I目前有一个可怕的混乱:
http ://www.rawbw.com/~rem/HelloPlus/...4s.html#h4intc

我想把所有的角色案例测试都拉进去

转换角色的函数到一个班级编号(例如1

代表空格,2代表数字,3代表签名等),称之为全部

这个地方,以及使用关于结果的SELECT语句,

不会改变代码的逻辑但可能会使它更整洁。

或者我可能会为Lisp手工编写替换代码/ Java

用于扫描字符串的实用程序,或在其中一个有用的C / / $
库中查找某些内容,然后翻译Lisp或Java代码

到C.你们有没有其他任何想法我可以做些什么来清理?b $ B $ C $ b?不要为我编写代码,但只是提示一下

库例程可能会为我做90%的工作,或者建议

重新设计算法?我不想做的一件事是

为C下载一个REGEX包。我试图举例说明

如何从头开始做事C,不是如何简单地使用某人

else'的程序,即使REGEX模块的源是

可用。如果某个东西不在C的标准库中,那么

它不会出现在这个项目中。 (我所做的唯一

例外是用于收集和解码HTML的模块

FORM内容,这是整个项目的先决条件。)

2月11日上午6:57,rem6 ... @ yahoo.com(罗伯特马斯,见http://

tinyurl.com/uh3t)问关于以下代码:


验证字段中的文本以确保它是一个整数的有效

表示,没有抛出任何垃圾in,即它
必须满足正则表达式:^ * [ - +]?[0-9] + *




如果该字段的内容有误,我想尽可能多地诊断

合理的错误,而不仅仅是说语法错误。



[snip]


你们有没有其他任何想法我可以做些什么来清理

C代码?不要为我编写代码,但只是提示一下

库例程可能会为我做90%的工作,或者建议

重新设计算法?



您可以尝试这样简单的事情:


strtol(字符串,& end,BASE);

if(* end!=''\''')

fprintf(stderr,语法错误从''%c''\ n开始',*结束);


我不确定这会给你带来尽可能多的语法错误

,但它告诉你它出现在哪里。 (另外,

这并不完全符合你的规格,因为

这不允许尾随空格,但那是'

a琐碎修复。)


-

Bill Pursell


I''m working on examples of programming in several languages, all
(except PHP) running under CGI so that I can show both the source
files and the actually running of the examples online. The first
set of examples, after decoding the HTML FORM contents, merely
verifies the text within a field to make sure it is a valid
representation of an integer, without any junk thrown in, i.e. it
must satisfy the regular expression: ^ *[-+]?[0-9]+ *$

If the contents of the field are wrong I want to diagnose as much
as reasonable what''s wrong, not just say "syntax error".

Because perl and PHP include support for regular expressions, it
was obvious how to do it, and easy to accomplish:
http://www.rawbw.com/~rem/HelloPlus/...html#h4intperl
http://www.rawbw.com/~rem/HelloPlus/....html#h4intphp

Because Common Lisp has good utilities for scanning strings, mostly
using position, position-if, and position-if-not, it was equally
easy, and equally obvious, how to do it:
http://www.rawbw.com/~rem/HelloPlus/...html#h4intlisp

The Java API is missing some of the functions available in Common
Lisp, so I had to augment the API, but then it was as easy as in
Common Lisp, with nearly the same algorithm:
http://www.rawbw.com/~rem/HelloPlus/...html#h4intjava

Now we come to C: I presently have a horrible mess:
http://www.rawbw.com/~rem/HelloPlus/...4s.html#h4intc
I''m thinking of pulling out all the character-case testing into a
function that converts a character into a class-number (such as 1
for space, 2 for digit, 3 for sign, etc.), calling that all over
the place, and the using a SELECT statement on the result, which
won''t change the logic of the code but might make it tidier.
Alternately I might hand-code replacements for the Lisp/Java
utilities for scanning strings, or find something in one of the C
libraries that would help, and then translate the Lisp or Java code
to C. Do any of you have any other ideas what I might do to clean
up the C code? Don''t write my code for me, but just give hints what
library routines might do 90% of the work for me, or suggest
re-design of the algorithm? One thing I don''t want to do is
download a REGEX package for C. I''m trying to give examples of how
to do things from scratch in C, not how to simply use somebody
else''s program, even if the source for the REGEX module is
available. If something isn''t in the a standard library for C, then
it doesn''t exist for the purpose of this project. (The only
exception I made is the module for collecting and decoding HTML
FORM contents, which is a prerequisite for this whole project.)

解决方案



If the contents of the field are wrong I want to diagnose as much
as reasonable what''s wrong, not just say "syntax error".

Because perl and PHP include support for regular expressions, it
was obvious how to do it, and easy to accomplish:
http://www.rawbw.com/~rem/HelloPlus/...html#h4intperl
http://www.rawbw.com/~rem/HelloPlus/....html#h4intphp

Because Common Lisp has good utilities for scanning strings, mostly
using position, position-if, and position-if-not, it was equally
easy, and equally obvious, how to do it:
http://www.rawbw.com/~rem/HelloPlus/...html#h4intlisp

The Java API is missing some of the functions available in Common
Lisp, so I had to augment the API, but then it was as easy as in
Common Lisp, with nearly the same algorithm:
http://www.rawbw.com/~rem/HelloPlus/...html#h4intjava

Now we come to C: I presently have a horrible mess:
http://www.rawbw.com/~rem/HelloPlus/...4s.html#h4intc
I''m thinking of pulling out all the character-case testing into a
function that converts a character into a class-number (such as 1
for space, 2 for digit, 3 for sign, etc.), calling that all over
the place, and the using a SELECT statement on the result, which
won''t change the logic of the code but might make it tidier.
Alternately I might hand-code replacements for the Lisp/Java
utilities for scanning strings, or find something in one of the C
libraries that would help, and then translate the Lisp or Java code
to C. Do any of you have any other ideas what I might do to clean
up the C code? Don''t write my code for me, but just give hints what
library routines might do 90% of the work for me, or suggest
re-design of the algorithm? One thing I don''t want to do is
download a REGEX package for C. I''m trying to give examples of how
to do things from scratch in C, not how to simply use somebody
else''s program, even if the source for the REGEX module is
available. If something isn''t in the a standard library for C, then
it doesn''t exist for the purpose of this project. (The only
exception I made is the module for collecting and decoding HTML
FORM contents, which is a prerequisite for this whole project.)


On Feb 11, 6:57 am, rem6...@yahoo.com (robert maas, see http://
tinyurl.com/uh3t) was asking about code that:

verifies the text within a field to make sure it is a valid
representation of an integer, without any junk thrown in, i.e. it
must satisfy the regular expression: ^ *[-+]?[0-9]+ *




If the contents of the field are wrong I want to diagnose as much
as reasonable what''s wrong, not just say "syntax error".

[snip]

Do any of you have any other ideas what I might do to clean
up the C code? Don''t write my code for me, but just give hints what
library routines might do 90% of the work for me, or suggest
re-design of the algorithm?


You could try something as simple as this:

strtol( string, &end, BASE );
if( *end != ''\0'' )
fprintf( stderr, "syntax error starting at ''%c''\n", *end);

I''m not sure that this gives you as much syntax error
as you want, but it tells you where it occurs. (Also,
this doesn''t exactly match your specification, since
this doesn''t allow trailing whitespace, but that''s
a trivial fix.)

--
Bill Pursell


这篇关于请求建议如何清理C代码以验证字符串表示整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆