You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
6.5 KiB

{\rtf1\ansi\ansicpg936\cocoartf2822
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
\f0\fs24 \cf0 import java.io.BufferedReader;\
import java.io.FileReader;\
import java.io.File;\
import java.util.Scanner;\
\
/**\
* \uc0\u28201 \u24230 \u36716 \u25442 \u22120 \u31243 \u24207 \u65288 Java\u65289 \
* \uc0\u31561 \u25928 \u31227 \u26893 Python\u28201 \u24230 \u36716 \u25442 \u31243 \u24207 \u65292 \u25903 \u25345 \u25668 \u27663 /\u21326 \u27663 \u20114 \u36716 \
* \uc0\u39069 \u22806 \u25903 \u25345 \u65306 \u21629 \u20196 \u34892 \u21442 \u25968 \u27169 \u24335 \u12289 \u25991 \u20214 \u25209 \u37327 \u36716 \u25442 \u27169 \u24335 \
*/\
public class TemperatureConverter \{\
\
/**\
* \uc0\u23558 \u25668 \u27663 \u24230 \u36716 \u25442 \u20026 \u21326 \u27663 \u24230 \
* @param c \uc0\u36755 \u20837 \u30340 \u25668 \u27663 \u28201 \u24230 \u65292 \u25968 \u25454 \u31867 \u22411 \u20026 \u21452 \u31934 \u24230 \u28014 \u28857 \u22411 \
* @return \uc0\u36716 \u25442 \u21518 \u30340 \u21326 \u27663 \u28201 \u24230 \u65292 \u21452 \u31934 \u24230 \u28014 \u28857 \u22411 \
*/\
public static double celsiusToFahrenheit(double c) \{\
return c * 9.0 / 5.0 + 32.0;\
\}\
\
/**\
* \uc0\u23558 \u21326 \u27663 \u24230 \u36716 \u25442 \u20026 \u25668 \u27663 \u24230 \
* @param f \uc0\u36755 \u20837 \u30340 \u21326 \u27663 \u28201 \u24230 \u65292 \u25968 \u25454 \u31867 \u22411 \u20026 \u21452 \u31934 \u24230 \u28014 \u28857 \u22411 \
* @return \uc0\u36716 \u25442 \u21518 \u30340 \u25668 \u27663 \u28201 \u24230 \u65292 \u21452 \u31934 \u24230 \u28014 \u28857 \u22411 \
*/\
public static double fahrenheitToCelsius(double f) \{\
return (f - 32.0) * 5.0 / 9.0;\
\}\
\
/**\
* \uc0\u25209 \u37327 \u36716 \u25442 \u65306 \u20174 \u25991 \u20214 \u35835 \u21462 \u22810 \u34892 \u28201 \u24230 \u25968 \u25454 \u24182 \u23436 \u25104 \u36716 \u25442 \u65288 \u21152 \u20998 \u39033 \u65289 \
* @param filename \uc0\u23384 \u20648 \u28201 \u24230 \u25968 \u25454 \u30340 \u25991 \u20214 \u21517 \u65292 \u23383 \u31526 \u20018 \u31867 \u22411 \
*/\
public static void batchConvert(String filename) \{\
try (BufferedReader br = new BufferedReader(new FileReader(filename))) \{\
String line;\
while ((line = br.readLine()) != null) \{\
convertAndPrint(line.trim()); // \uc0\u35843 \u29992 \u32479 \u19968 \u36716 \u25442 \u26041 \u27861 \
\}\
\} catch (Exception e) \{\
System.out.println("\uc0\u25991 \u20214 \u35835 \u21462 \u22833 \u36133 \u65306 " + e.getMessage());\
\}\
\}\
\
/**\
* \uc0\u32479 \u19968 \u36716 \u25442 \u36923 \u36753 \u65306 \u35299 \u26512 \u36755 \u20837 \u24182 \u36755 \u20986 \u32467 \u26524 \u65288 \u22797 \u29992 \u26680 \u24515 \u36923 \u36753 \u65289 \
* @param input \uc0\u24453 \u35299 \u26512 \u30340 \u28201 \u24230 \u19982 \u21333 \u20301 \u23383 \u31526 \u20018 \u65292 \u22914 "36.6 C"\
*/\
public static void convertAndPrint(String input) \{\
if (input.isEmpty()) \{\
System.out.println("\uc0\u36755 \u20837 \u20026 \u31354 \u65292 \u36339 \u36807 \u36716 \u25442 \u12290 ");\
return;\
\}\
String[] parts = input.split("\\\\s+");\
try \{\
double value = Double.parseDouble(parts[0]);\
// \uc0\u19982 \u21407 Python\u36923 \u36753 \u19968 \u33268 \u65306 \u26410 \u36755 \u20837 \u21333 \u20301 \u26102 \u40664 \u35748 \u25353 \u25668 \u27663 \u24230 \u22788 \u29702 \
String unit = parts.length > 1 ? parts[1].toUpperCase() : "C";\
\
if (unit.startsWith("C")) \{\
double f = celsiusToFahrenheit(value);\
System.out.printf("%.2f \'b0C = %.2f \'b0F%n", value, f);\
\} else if (unit.startsWith("F")) \{\
double c = fahrenheitToCelsius(value);\
System.out.printf("%.2f \'b0F = %.2f \'b0C%n", value, c);\
\} else \{\
System.out.println("\uc0\u26410 \u30693 \u21333 \u20301 \u65292 \u35831 \u20351 \u29992 C \u25110 F\u12290 ");\
\}\
\} catch (Exception e) \{\
System.out.println("\uc0\u36755 \u20837 \u35299 \u26512 \u22833 \u36133 \u65292 \u35831 \u25353 \u31034 \u20363 \u36755 \u20837 \u65288 \u22914 36.6 C\u65289 \u12290 ");\
\}\
\}\
\
/**\
* \uc0\u31243 \u24207 \u20027 \u20837 \u21475 \u65306 \u25972 \u21512 \u19977 \u31181 \u36816 \u34892 \u27169 \u24335 \
* @param args \uc0\u21629 \u20196 \u34892 \u21442 \u25968 \u65292 \u25903 \u25345 \u26080 \u21442 \u25968 \u65288 \u20132 \u20114 \u24335 \u65289 \u12289 \u21442 \u25968 \u20026 \u28201 \u24230 \u21333 \u20301 \u65288 \u21629 \u20196 \u34892 \u27169 \u24335 \u65289 \u12289 \u21442 \u25968 \u20026 \u25991 \u20214 \u21517 \u65288 \u25209 \u37327 \u27169 \u24335 \u65289 \
*/\
public static void main(String[] args) \{\
// \uc0\u27169 \u24335 1\u65306 \u25209 \u37327 \u25991 \u20214 \u36716 \u25442 \u65288 \u21152 \u20998 \u39033 \u65289 - \u20165 1\u20010 \u21442 \u25968 \u19988 \u20026 \u25991 \u20214 \u26102 \u35302 \u21457 \
if (args.length == 1 && new File(args[0]).exists()) \{\
batchConvert(args[0]);\
return;\
\}\
\
Scanner scanner = new Scanner(System.in);\
String input;\
\
// \uc0\u27169 \u24335 2\u65306 \u21629 \u20196 \u34892 \u21442 \u25968 \u27169 \u24335 \u65288 \u21152 \u20998 \u39033 \u65289 - \u22810 \u21442 \u25968 \u26102 \u30452 \u25509 \u25340 \u25509 \u20026 \u36755 \u20837 \
if (args.length > 0) \{\
input = String.join(" ", args);\
\} else \{\
// \uc0\u27169 \u24335 3\u65306 \u20132 \u20114 \u24335 \u36755 \u20837 \u65288 \u21407 Python\u26680 \u24515 \u21151 \u33021 \u65292 \u24517 \u20570 \u65289 \
System.out.print("\uc0\u35831 \u36755 \u20837 \u35201 \u36716 \u25442 \u30340 \u28201 \u24230 \u19982 \u21333 \u20301 \u65288 \u20363 \u22914 36.6 C \u25110 97 F\u65289 \u65306 ");\
input = scanner.nextLine().trim();\
\}\
\
convertAndPrint(input); // \uc0\u25191 \u34892 \u26680 \u24515 \u36716 \u25442 \
scanner.close();\
\}\
\}}