Reescriba el conversor Celsius-Farenheit usando find. rellene las partes que faltan:
1 #!/usr/bin/env groovy 2 def re = '''(?ix) 3 ^ # begin of string 4 \\s* 5 ([-+]?\\d+(?:\\.\\d*)?) # number 6 \\s* 7 ([cf]) # type 8 \\s* 9 $ # end of string 10 '''; 11 12 print "Enter a temperature (i.e. 32F, 100C): "; 13 14 def stdin = new BufferedReader(new InputStreamReader(System.in)) 15 print "Enter a temperature (i.e. 32F, 100C): "; 16 stdin.eachLine{ input -> 17 ...................... { .................. 18 if ((type == 'C') || (type == 'c')) { 19 celsius = num.toDouble() 20 farenheit = (celsius * 9/5)+32 21 } 22 else { 23 farenheit = num.toDouble() 24 celsius = (farenheit -32)*5/9; 25 } 26 printf "%.2f C = %.2f F\n", celsius, farenheit; 27 } // this message to System.err 28 ....... System.err.println "Expecting a temperature, so don't understand '$input'" 29 30 print "Enter a temperature (i.e. 32F, 100C): "; 31 }
Sigue un ejemplo de ejecución:
generaciondecodigos@nereida:~/src/groovy/strings$ ./c2fFindAll.groovy Enter a temperature (i.e. 32F, 100C): Enter a temperature (i.e. 32F, 100C): 32F 0,00 C = 32,00 F Enter a temperature (i.e. 32F, 100C): 100C 100,00 C = 212,00 F Enter a temperature (i.e. 32F, 100C): generaciondecodigos@nereida:~/src/groovy/strings$