foundation 下的converter 程序- MVC

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.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.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.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;
}


    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值