1 首先判定字符串输入是否合格(是否含有其他非法字符)
2 去掉字符串多余的0
3 提取小数点位置,吧小数点后面有几位记录下来,最后结果中确定结果含有几位小数点,然后在加上(结果小数点的位数=两个字符串小数点后位数相加)
4 用字符串实现大数相乘(去掉小数点),对结果进行复原(在合理位置加上小数点),
5去掉多余的0
public static void main(String
[] srgs
) {
new Decimal().Resolve();
}
public void Resolve(){
String d1
,d2
;
int floatnum1
=0;
int floatnum2
=0;
d1
=Input();
d2
=Input();
d1
=RemoveZeroBegin(d1
);
d1
=RemoveZeroEnd(d1
);
d2
=RemoveZeroBegin(d2
);
d2
=RemoveZeroEnd(d2
);
String
[] decimal1
=d1
.split(",");
String
[] decimal2
=d2
.split(",");
if(decimal1
.length
>1)
{
floatnum1
=decimal1
[1].length();
d1
=decimal1
[0]+decimal1
[1];
}
if(decimal2
.length
>1)
{
d2
=decimal2
[0]+decimal2
[1];
floatnum2
=decimal2
[1].length();
}
String sbString
= Multiply(d1
, d2
);
StringBuilder sb
=new StringBuilder(sbString
);
if(floatnum1
+floatnum2
!=0)
{
sb
.insert(sbString
.length()-floatnum1
-floatnum2
, ',');
}
sbString
=sb
.toString();
sbString
=RemoveZeroBegin(sbString
);
sbString
=RemoveZeroEnd(sbString
);
System
.out
.println(sbString
);
}
@SuppressWarnings("resource")
public String
Input() {
Scanner input
=new Scanner(System
.in
);
return input
.next();
}
String
Multiply(String s1
,String s2
) {
int len1
=s1
.length();
int len2
=s2
.length();
int[] res
=new int[len1
+len2
];
for(int i
=0;i
<res
.length
;i
++) {
res
[i
]=0;
}
for(int i
=s1
.length()-1;i
>=0;i
--) {
for(int j
=s2
.length()-1;j
>=0;j
--) {
res
[ len1
-1-i
+ len2
-1-j
] += (s1
.charAt(i
)-'0') * (s2
.charAt(j
)-'0');
}
}
String sb
="";
for(int i
=0;i
<res
.length
-1;i
++) {
res
[i
+1] += res
[i
]/10;
res
[i
]%=10;
sb
+= res
[i
];
}
sb
+=res
[res
.length
-1];
sb
= Reverse(sb
);
return sb
;
}
String
Reverse(String str
){
char[] chars
= str
.toCharArray();
String reverse
= "";
for (int i
= chars
.length
- 1; i
>= 0; i
--) {
reverse
+= chars
[i
];
}
return reverse
;
}
String
RemoveZeroBegin(String str
) {
String s
="";
int i
=0;
while(str
.charAt(i
)=='0' && i
<str
.length() && str
.charAt(i
+1)!=',') {
i
++;
}
s
=str
.substring(i
, str
.length());
return s
;
}
String
RemoveZeroEnd(String str
) {
String s
="";
int i
=str
.length()-1;
while(str
.charAt(i
)=='0') {
i
--;
if(str
.charAt(i
)==',') {
i
--;
break;
}
}
s
=str
.substring(0, i
+1);
return s
;
}
题目中的小数点我用的是 “,” 因为我用了java的split函数,打开源码发现在split函数里面吧 “.” 屏蔽了所以暂时用逗号代替一下,如图:
if (((regex
.value
.length
== 1 &&
".$|()[{^?*+\\".indexOf(ch
= regex
.charAt(0)) == -1) ||
(regex
.length() == 2 &&
regex
.charAt(0) == '\\' &&
(((ch
= regex
.charAt(1))-'0')|('9'-ch
)) < 0 &&
((ch
-'a')|('z'-ch
)) < 0 &&
((ch
-'A')|('Z'-ch
)) < 0)) &&
(ch
< Character
.MIN_HIGH_SURROGATE
||
ch
> Character
.MAX_LOW_SURROGATE
))
而且因为我没学过java,java实验要用java来写,所以java代码写的很烂。。。。。。。