62,614
社区成员
发帖
与我相关
我的任务
分享
package mars.test;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ConvertOctalToDecimal {
public static final String EMode="^(\\d)\\.(\\d)E-(\\d+)$";
public ConvertOctalToDecimal() {
Scanner in = new Scanner(System.in);
double m = in.nextDouble();
double e = 0.125d;
double total = 0.00d;
if (m > 0 & m < 1) {
//0.001=1.0E-4
String s = String.valueOf(m);
if(Pattern.matches(EMode,s))
{
Matcher matcher = Pattern.compile(EMode).matcher(s);
String one=null;
String two=null;
String three=null;
while(matcher.find())
{
one = matcher.group(1);
two = matcher.group(2);
three = matcher.group(3);
}
//
total=Integer.parseInt(one)*1d;
for(int i=0;i<two.length();i++)
{
total +=(s.charAt(i) - 48)*e;
e*=0.125d;
}
total*=Math.pow(8d,-Integer.parseInt(three));
}
else
{
for (int i = 2; i < s.length(); i++) {
total += (s.charAt(i) - 48)*e;
e *= 0.125d;
}
}
}
System.out.println(total);
}
public static void main(String[] args) {
new ConvertOctalToDecimal();
}
}
import java.util.*;
public class Test3 {
public Test3() {
Scanner in = new Scanner(System.in);
double m = in.nextDouble();
double total = 0.00;
if (m > 0 & m < 1) {
String s = String.valueOf(m);
double e = (double) 1 / 8;
for (int i = 2; i < s.length(); i++) {
total += (s.charAt(i) - 48) * e;
e *= (double)1/8;
}
}
System.out.println(total);
}
public static void main(String[] args) {
new Test3();
}
}
e *= e;
e *= (double)1/8;