apple 的
currency converter 教程是用AppKit , 这里是仅用Foundation 框架的类似MVC程序, 不过是转换温度。来自德国的
NeXTStep时代的原始文档。
//
// LinearModel.h
// convert01
//
//
#import < Foundation / Foundation.h >
@interface LinearModel : NSObject {
float a,b;
}
- ( void )setA:( float ) a;
- ( void )setB:( float )b;
- ( float )y:( float )x; // y=ax+b;
@end
// LinearModel.h
// convert01
//
//
#import < Foundation / Foundation.h >
@interface LinearModel : NSObject {
float a,b;
}
- ( void )setA:( float ) a;
- ( void )setB:( float )b;
- ( float )y:( float )x; // y=ax+b;
@end
//
// LinearModel.m
// convert01
//
#import " LinearModel.h "
@implementation LinearModel
- ( void ) setA:( float ) _a {
a=_a;
}
- ( void )setB:( float )_b {
b=_b;
}
- ( float )y:( float )x {
return a*x+b;
}
@end
// LinearModel.m
// convert01
//
#import " LinearModel.h "
@implementation LinearModel
- ( void ) setA:( float ) _a {
a=_a;
}
- ( void )setB:( float )_b {
b=_b;
}
- ( float )y:( float )x {
return a*x+b;
}
@end
//
// FloatView.h
// convert01
#import < Foundation / Foundation.h >
@interface FloatView : NSObject {
float f;
}
- ( void )setFloatValue:( float )f;
- ( float )floatValue;
@end
// FloatView.h
// convert01
#import < Foundation / Foundation.h >
@interface FloatView : NSObject {
float f;
}
- ( void )setFloatValue:( float )f;
- ( float )floatValue;
@end
//
// FloatView.m
// convert01
#import " FloatView.h "
@implementation FloatView
- ( void )setFloatValue:( float )_f
{
f=_f;
}
- ( float )floatValue
{
return f;
}
@end
// FloatView.m
// convert01
#import " FloatView.h "
@implementation FloatView
- ( void )setFloatValue:( float )_f
{
f=_f;
}
- ( float )floatValue
{
return f;
}
@end
//
// ConvertController.h
// convert01
#import < Foundation / Foundation.h >
@interface ConvertController : NSObject {
id model; //calculates result
id output; //receives result
}
- ( void )setModel:(id)model;
- ( void )setOutput:(id)output;
- ( void )update:(id)sender; // out<-model(sender)
@end
// ConvertController.h
// convert01
#import < Foundation / Foundation.h >
@interface ConvertController : NSObject {
id model; //calculates result
id output; //receives result
}
- ( void )setModel:(id)model;
- ( void )setOutput:(id)output;
- ( void )update:(id)sender; // out<-model(sender)
@end
//
// ConvertController.m
// convert01
//
#import " ConvertController.h "
#import " LinearModel.h "
#import " FloatView.h "
@implementation ConvertController
- ( void )setModel:(id)_model {
model=_model;
}
- ( void )setOutput:(id)_output {
output=_output;
}
- ( void )update:(id)sender {
[output setFloatValue:[model y:[sender floatValue]]];
}
@end
// ConvertController.m
// convert01
//
#import " ConvertController.h "
#import " LinearModel.h "
#import " FloatView.h "
@implementation ConvertController
- ( void )setModel:(id)_model {
model=_model;
}
- ( void )setOutput:(id)_output {
output=_output;
}
- ( void )update:(id)sender {
[output setFloatValue:[model y:[sender floatValue]]];
}
@end
//
// main.m
// convert01
//
#import < Foundation / Foundation.h >
#import " ConvertController.h "
#import " LinearModel.h "
#import " FloatView.h "
int main( int argc, char * argv[])
{
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init];
FloatView *celsius = [FloatView new];
FloatView *fahrenheit = [FloatView new];
LinearModel *c_f = [LinearModel new];
ConvertController *c2f = [ConvertController new];
char buf[BUFSIZ];
[c_f setA:9.0/5.0],[c_f setB:32];
[c2f setModel:c_f],[c2f setOutput:fahrenheit];
while (gets(buf)) {
float value;
if (sscanf(buf,"%g", &value) == 1) {
[celsius setFloatValue:value];
[c2f update:celsius];
printf(" %g ", [fahrenheit floatValue]);
}
}
[pool release];
exit (0);
return 0;
}
// main.m
// convert01
//
#import < Foundation / Foundation.h >
#import " ConvertController.h "
#import " LinearModel.h "
#import " FloatView.h "
int main( int argc, char * argv[])
{
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init];
FloatView *celsius = [FloatView new];
FloatView *fahrenheit = [FloatView new];
LinearModel *c_f = [LinearModel new];
ConvertController *c2f = [ConvertController new];
char buf[BUFSIZ];
[c_f setA:9.0/5.0],[c_f setB:32];
[c2f setModel:c_f],[c2f setOutput:fahrenheit];
while (gets(buf)) {
float value;
if (sscanf(buf,"%g", &value) == 1) {
[celsius setFloatValue:value];
[c2f update:celsius];
printf(" %g ", [fahrenheit floatValue]);
}
}
[pool release];
exit (0);
return 0;
}