题目要求:
编写一个程序,用以接受用户输入的数字。将该数左 --右反转,然后显示反转后的数。
--
***********************************************
--
编写一个程序,用以接受用户输入的数字。将该数左
--
右反转,然后显示反转后的数。
--
***********************************************
--
设置参数使dbms_output.put_line()的结果显示在屏幕上
set
serveroutput
on
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
--
-----------------------------------------------
--
注:
--
-----------------------------------------------
--
“用户的键盘输入”以下简称“输入”
--
题目中对数字转换的格式要求并不十分明确,所以
--
自己规定了一下转换要求
--
-----------------------------------------------
--
-----------------------------------------------
--
要求(A).规定反转时先转成简单格式再反转
--
-----------------------------------------------
--
预期测试用例:
--
1.输入0105.0000反转后为501
--
2.输入01050反转后为501
--
3.输入0.5反转后为5
--
-----------------------------------------------
--
-----------------------------------------------
--
要求(B).规定反转时先反转再转成简单格式
--
-----------------------------------------------
--
预期测试用例:
--
1.输入0105.0000反转后为0.501
--
2.输入01050反转后为5010
--
3.输入0.5反转后为5
--
-----------------------------------------------
--
-----------------------------------------------
--
对于 要求(A)
--
-----------------------------------------------
--
规则思考:
--
1.以小数点开头或以小数点结尾的输入不是数字
--
2.包含超过一个小数点的输入不是数字
--
3.包含既非数字又非小数点的字符的输入不是数字
--
实现步骤设计:
--
1.如果不满足以上规则,提示非数字
--
2.修剪数字两边的0
--
例:'0105.0000' 修剪成 '105.',
--
'01050' 修剪成 '105',
--
'0.5' 修剪成 '.5'
--
3.修剪数字两边的小数点
--
例:'105.' 修剪成 '105',
--
'105' 修剪成 '105',
--
'.5' 修剪成 '5'
--
4.将剩下的数字反转
--
例:'105' 反转成 '501',
--
'105' 反转成 '501',
--
'5' 反转成 '5'
--
-----------------------------------------------
--
要求(A)的具体实现
--
-----------------------------------------------
declare
v_i
int
;
v_c
char
(
1
);
v_userinput
varchar2
(
100
);
v_result v_userinput
%
type;
e_NumberFormatException EXCEPTION;
v_prompt constant
char
(
14
) :
=
'
反转后的数字:
'
;
begin
--
从键盘接收用户输入
v_userinput :
=
'
&需要反转的数字
'
;
--
判断输入是否以小数点开头,或以小数点结尾,如是,提示非数字
if
(substr(v_userinput,
1
,
1
)
=
'
.
'
or
substr(v_userinput,length(v_userinput),
1
)
=
'
.
'
)
then
raise e_NumberFormatException;
end
if
;
--
判断输入是否包含1个以上的小数点,如是,提示非数字
if
(length(v_userinput)
-
length(
replace
(v_userinput,
'
.
'
,
''
)))
>
1
then
raise e_NumberFormatException;
end
if
;
--
删除输入两侧的0
execute
immediate
'
select nvl(ltrim(rtrim(:ui,:c1),:c2),0) from dual
'
into
v_userinput using v_userinput,
'
0
'
,
'
0
'
;
--
删除输入两侧的小数点
execute
immediate
'
select nvl(ltrim(rtrim(:ui,:c1),:c2),0) from dual
'
into
v_userinput using v_userinput,
'
.
'
,
'
.
'
;
--
反向遍历输入中每一个字符,并把该字符附加到结果变量中
v_i :
=
0
;
for
v_i
in
reverse
1
..length(v_userinput)
loop
v_c :
=
substr(v_userinput,v_i,
1
);
--
如果输入中有字符非数字也非小数点,则提示非数字
if
v_c
<>
'
.
'
and
not
(v_c
>=
'
0
'
and
v_c
<=
'
9
'
)
then
raise e_NumberFormatException;
end
if
;
v_result :
=
v_result
||
v_c;
end
loop;
--
输入最后的结果
dbms_output.put_line(v_prompt
||
v_result);
exception
when
e_NumberFormatException
then
dbms_output.put_line(
'
您输入的不是数字
'
);
end
;
/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
--
-----------------------------------------------
--
对于 要求(B)
--
-----------------------------------------------
--
规则思考:
--
1.以小数点开头或以小数点结尾的输入不是数字
--
2.包含超过一个小数点的输入不是数字
--
3.包含既非数字又非小数点的字符的输入不是数字
--
实现步骤设计:
--
1.如果不满足以上规则,提示非数字
--
2.将输入反转
--
例:'0105.0000' 反转成 '0000.5010',
--
'01050' 反转成 '05010',
--
'0.5' 反转成 '5.0'
--
3.如果输入中包含小数点,则修剪两侧的0,否则,只修剪左侧的0
--
例:'0000.5010' 修剪成 '.501'
--
'05010' 修剪成 '5010'
--
'5.0' 修剪成 '5.'
--
4.修剪右侧的小数点
--
例:'.501' 修剪成 '.501'
--
'5010' 修剪成 '5010'
--
'5.' 修剪成 '5'
--
5.如果结果以小数点开头,则在左侧补0
--
例:'.501' 转换成 '0.501'
--
'5010' 转换成 '5010'
--
'5' 转换成 '5'
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
--
-----------------------------------------------
--
要求(B)的具体实现
--
-----------------------------------------------
declare
v_i
int
;
v_c
char
(
1
);
v_userinput
varchar2
(
100
);
v_result v_userinput
%
type;
e_NumberFormatException EXCEPTION;
v_prompt constant
char
(
14
) :
=
'
反转后的数字:
'
;
begin
--
从键盘接收用户输入
v_userinput :
=
'
&需要反转的数字
'
;
--
判断输入是否以小数点开头,或以小数点结尾,如是,提示非数字
if
(substr(v_userinput,
1
,
1
)
=
'
.
'
or
substr(v_userinput,length(v_userinput),
1
)
=
'
.
'
)
then
raise e_NumberFormatException;
end
if
;
--
判断输入是否包含1个以上的小数点,如是,提示非数字
if
(length(v_userinput)
-
length(
replace
(v_userinput,
'
.
'
,
''
)))
>
1
then
raise e_NumberFormatException;
end
if
;
--
反向遍历输入中每一个字符,并把该字符附加到结果变量中
v_i :
=
0
;
for
v_i
in
reverse
1
..length(v_userinput)
loop
v_c :
=
substr(v_userinput,v_i,
1
);
--
如果输入中有字符非数字也非小数点,则提示非数字
if
v_c
<>
'
.
'
and
not
(v_c
>=
'
0
'
and
v_c
<=
'
9
'
)
then
raise e_NumberFormatException;
end
if
;
v_result :
=
v_result
||
v_c;
end
loop;
--
如果有小数点,刚删除输入两侧的0,如无小数点,则仅删除左侧的0
if
(instr(v_result,
'
.
'
)
>
0
)
then
execute
immediate
'
select nvl(ltrim(rtrim(:ui,:c1),:c2),0) from dual
'
into
v_result using v_result,
'
0
'
,
'
0
'
;
else
execute
immediate
'
select nvl(ltrim(:ui,:c1),0) from dual
'
into
v_result using v_result,
'
0
'
;
end
if
;
--
删除右侧的小数点
execute
immediate
'
select nvl(rtrim(:ui,:c1),0) from dual
'
into
v_result using v_result,
'
.
'
;
if
(substr(v_result,
1
,
1
)
=
'
.
'
)
then
v_result :
=
'
0
'
||
v_result ;
end
if
;
--
输出最后的结果
dbms_output.put_line(v_prompt
||
v_result);
exception
when
e_NumberFormatException
then
dbms_output.put_line(
'
您输入的不是数字
'
);
end
;
/
转载于:https://www.cnblogs.com/zxsoft/archive/2008/04/29/1176875.html