本文共 1398 字,大约阅读时间需要 4 分钟。
#import @interface TemperatureConverter : NSObject - (double)celsiusToFahrenheit:(double)celsiusTemperature; - (double)fahrenheitToCelsius:(double)fahrenheitTemperature); @end int main(int argc, char **argv) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // 检查命令行参数 if (argc != 3) { NSLog(@"使用说明: %s <温度> <转换方向> \n", argv[0]); return EXIT_FAILURE; } double temperature = atof(argv[1]); NSString *direction = argv[2]; if (!direction || [direction length] != 2) { NSLog(@"错误:转换方向必须是 'C' 或 'F'\n"); return EXIT_FAILURE; } double convertedTemp; if ([direction isEqualToString:@"C"]) { convertedTemp = [converter celsiusToFahrenheit:temperature]; } else { convertedTemp = [converter fahrenheitToCelsius:temperature]; } NSLog(@"转换结果: %.6f\n", convertedTemp); [pool release]; return EXIT_SUCCESS; } @implementation TemperatureConverter - (double)celsiusToFahrenheit:(double)celsiusTemperature) { return (celsiusTemperature * 9/5) + 32; } - (double)fahrenheitToCelsius:(double)fahrenheitTemperature) { return (fahrenheitTemperature - 32) * 5/9; } @end 转换方向> 温度> 关键词:[Objective-C, 温度转换, 摄氏温度, 华氏温度, 命令行程序]
转载地址:http://nqifk.baihongyu.com/