1

如果要下载项目文件,请在此处:http: //files.me.com/knyck2/918odc

因此,我正在编写“Objective c 2.0 编程”一书并进行练习 8.5,其中您必须构建一堆类并将自制抽象类子类化为方形类、三角形类和圆形类。您还必须通过每个对象上的方法计算面积和周长/周长,并将其显示在 nslog 上。

一切都已经设置好,但是当我从三角形类中显示每一边的大小时,会返回一些奇怪的值,我不知道为什么。其中一侧显示为 0.000000,另外两个显示为这些巨大的浮点数/双精度数:

521556413499497987605515504756958465074645713473678706698073680750705671496313493175192637227720116636212033961634738517717834352497805734697302078530634879721347399811072.000000

661675923659048238438515844320261245751425513854584815925028483891542482488445146590620251584462848.000000

当我去调试时,它看起来好像所有的值都正确分配了,所以我不知道为什么它显示得如此时髦。

感谢您的任何帮助,您可以提供,

缺口

这是主要的:

#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "Triangle.h"
#import "Circle.h"

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Rectangle *myRect = [[Rectangle alloc] init];
    Circle    *myCircle = [[Circle alloc] init];
    Triangle *myTriangle = [[Triangle alloc] init];

    myRect.fillColor = 12345;
    myRect.filled = NO;
    myRect.lineColor = 29999;
    [myRect setWidth:15.3 andHeight:22.3];

    NSLog(@"the rectangle has a filled color of %i,line color of %i",
            myRect.fillColor, myRect.lineColor);

    if (myRect.filled == YES) 
        NSLog(@"The rectangle is also filled");
    else if (myRect.filled == NO) 
    NSLog(@"The rectangle is not filled");

    NSLog(@" rectangle %f, %f, area %f, perimeter %f",
           myRect.w , myRect.h, myRect.area, myRect.perimeter);                    

    myCircle.fillColor = 15555;
    myCircle.filled = NO;
    myCircle.lineColor = 32349;
    [myCircle setR:15.2];

    NSLog(@"the circle has a radius of %f ,color of %i, line color of %i",
           myCircle.r, myCircle.fillColor, myCircle.lineColor);

    NSLog(@"Also the circles area is %f and the circumference is %f",
            myCircle.area, myCircle.circumference  );


    myTriangle.fillColor = 71611;
    myTriangle.filled = NO;
    myTriangle.lineColor = 78998;
    [myTriangle setSide1:13];
    [myTriangle setSide2:19];
    [myTriangle setSide3: 27.5];

    NSLog(@"triangle sides %g, %g and %g."),  // << error
            myTriangle.side1 , myTriangle.side2 , myTriangle.side3 ;
    NSLog(@"triangle area %f, perimeter %f.",
            myTriangle.area, myTriangle.perimeter );
    NSLog(@"triangle fill color %i, line color %i",
            myTriangle.fillColor, myTriangle.lineColor);

    [myCircle release];
    [myRect release];
    [myTriangle release];

    [pool drain];
    return 0;

}

这是接口和实现:

#import <Foundation/Foundation.h>
#import "GraphicObject.h"

@interface Triangle : GraphicObject 
{
    double side1;
    double side2;
    double side3;
}

@property double side1, side2, side3;

-(void) setSides: (double)s1: (double)s2 : (double)s3;
-(double) area;
-(double) perimeter;

@end

三角.m

#import "Triangle.h"
#import <math.h>

@implementation Triangle
@synthesize side1, side2, side3;

-(void) setSides: (double) s1: (double) s2:(double) s3
{
    side1 = s1; 
    side2 = s2;
    side3 = s3;
}

-(double) area
{
    return sqrt(  (side1 + side2 + side3) 
                * (side1 + side2 - side3)
                * (side2 + side3 - side1)
                * (side3 + side1 - side2) / 16);
}

-(double) perimeter
{
    return side1 + side2 + side3;
}
@end

这里也是超类,GraphicObject.h

#import <Foundation/Foundation.h>    
@interface GraphicObject : NSObject 
{
    int fillColor; 
    BOOL filled; 
    int lineColor;  
}

@property int fillColor, lineColor;
@property BOOL filled;
@end

图形对象.m

#import "GraphicObject.h"
@implementation GraphicObject
@synthesize fillColor, lineColor, filled;
@end
4

1 回答 1

3

问题是这一行:

NSLog(@"the triangle has the sides of sizes %g, %g and %g."), myTriangle.side1 , myTriangle.side2 , myTriangle.side3 ;

使用)before,意味着您对 NSLog 的调用没有提供值来替代%g格式化输出时的值。这似乎是无意的,正如我的老朋友常说的“所有错误都源于复制和粘贴......所有错误”。

解决方法是将 移到)之前的位置,;如下所示:

NSLog(@"the triangle has the sides of sizes %g, %g and %g.", myTriangle.side1 , myTriangle.side2 , myTriangle.side3);
于 2010-01-04T21:35:51.670 回答