程序无法在 nslog 中正确显示三角形对象的简单边值
如果您想下载,请在此处下载项目文件:http://files.me.com/knyck2/918odc
所以我正在写《Objective c 2.0 中的编程》这本书,并致力于练习 8.5,其中你必须构建一堆类,并将自制的抽象类子类为正方形类、三角形类和圆形类。您还必须通过每个对象的方法计算面积和周长/周长并将其显示在 nslog 上。
一切都已基本设置完毕,但是当我显示三角形类中每条边的大小时,会返回一些奇怪的值,我不确定为什么。其中一侧显示为 0.000000,另外两侧显示为这些巨大的浮点数/双精度数:
521556413499497987605515504756958465074645713473678706698073680750705671496313493175192637227720116636212033961634738517717834352497805734697302078530634879721347399811072.000000
当
661675923659048238438515844320261245751425513854584815925028483891542482488445146590620251584462848.000000
我去调试时,似乎所有值都分配正确,所以我不知道为什么它显示得如此有趣。
感谢您提供的任何帮助,
Nick
这是主要的:
#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
Triangle.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
GraphicObject.m
#import "GraphicObject.h"
@implementation GraphicObject
@synthesize fillColor, lineColor, filled;
@end
Project file here if you want to download: http://files.me.com/knyck2/918odc
So I am working on the book "programming in Objective c 2.0" and working on exercise 8.5, where you have to build a bunch of classes and subclass a homemade abstract class into a square class a triangle class and a circle class. you also have to calculate the area and perimiter/circumference through methods on each object and display it on nslog.
Everything is pretty much set, but some weird values are being returned when i display the size of each side from my triangle class and I'm not sure why. one of the sides display as 0.000000 and the other two display as these huge floats/doubles:
521556413499497987605515504756958465074645713473678706698073680750705671496313493175192637227720116636212033961634738517717834352497805734697302078530634879721347399811072.000000
and
661675923659048238438515844320261245751425513854584815925028483891542482488445146590620251584462848.000000
When i go to debug it appears as if all values assign correctly so i am at a loss as to why this is displaying so funkily.
Thanks for any help you can provide,
Nick
here's the main:
#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;
}
and here's the interface and implementation:
#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
Triangle.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
also here is the super class, GraphicObject.h
#import <Foundation/Foundation.h>
@interface GraphicObject : NSObject
{
int fillColor;
BOOL filled;
int lineColor;
}
@property int fillColor, lineColor;
@property BOOL filled;
@end
GraphicObject.m
#import "GraphicObject.h"
@implementation GraphicObject
@synthesize fillColor, lineColor, filled;
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是这一行:
在
,
之前使用)
意味着您对 NSLog 的调用在格式化时没有提供任何值来替代那些%g
输出。这似乎是无意的,正如我的老朋友常说的“所有错误都源于复制和粘贴......全部”。解决方法是将
)
移动到;
之前,如下所示:The problem is this line:
Using
)
before the,
means that your call to NSLog provides no values to substitute for those%g
when formatting for output. It appears unintentional, as my old friend used to say "All errors stem from copy and paste.... all of them".The fix is to move the
)
to just before the;
like so: