I switched to python for this one. Consider the following script:
#!/usr/bin/env python
import re
numeric_const_pattern = r"""
[-+]? # optional sign
(?:
(?: \d* \. \d+ ) # .1 .12 .123 etc 9.1 etc 98.1 etc
|
(?: \d+ \.? ) # 1. 12. 123. etc 1 12 123 etc
)
# followed by optional exponent part if desired
(?: [Ee] [+-]? \d+ ) ?
"""
rx = re.compile(numeric_const_pattern, re.VERBOSE)
with open('code.txt') as fd:
for line in fd:
num = rx.findall(line)
for n in num:
line = line.replace(n, str(float(n)*.33))
print line.rstrip()
Output:
$ ./num.py
public enum Planet {
MERCURY (1.08999e+23, 805101.0),
VENUS (1.60677e+24, 1997094.0),
EARTH (1.97208e+24, 2104786.2),
MARS (2.11893e+23, 1121076.0),
JUPITER (6.27e+26, 23592360.0),
SATURN (1.87704e+26, 19888440.0),
URANUS (2.86638e+25, 8434470.0),
NEPTUNE (3.3792e+25, 8166180.0);
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
private double mass() { return mass; }
private double radius() { return radius; }
// universal gravitational constant (m0.99 kg-0.33 s-0.66)
public static final double G = 2.20209e-11;
double surfaceGravity() {
return G * mass / (radius * radius);
}
double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
Not perfect, since all numbers basically get multiplied with 0.33
.
kg-1
gets transformed into kg-0.33
etc. But it's a start.
Next step is to ignore comments and reduce false positives...