编程实现:在一个视图坐标系中(原点在左上角,原点向右为正 x,原点向下为正y,坐标系宽320,高480),创建3个矩形对象,参 数如图所示(下一页)。输出这三个矩形的坐标x,坐标y,宽,高, 周长,面积。矩形不需要显示,只需要设置参数即可。需求:
定义矩形类:
实例变量:坐标x、坐标y、宽、高、矩形中心点的坐标centerX、 矩形中心点的坐标centerY、面积、周长。
方法:初始化、绘制(输出4个主要数据,面积、周长),计算周长、 计算面积。
分数(分子/分母)计算:在main.m中,创建两个分数对象,对这 两个分数进行加、减、乘、除运算,并通过执行方法输出结果。
#import
#import "Person.h"
#import "AudCar.h"
#import "MobilePhone.h"
#import "Fruit.h"
#import "Close.h"
#import "Vegetable.h"
#import "Meal.h"
#import "Flower.h"
#import "Tree.h"
#import "Classes.h"
#import "Teacher.h"
#import "Student.h"
#import "Number.h"
#import "Rectangle.h"
int main(int argc, const char * argv[])
{
// Person * per1 = [Person alloc] ;
// per1 = [per1 init ] ;
// per1 -> _name = @"郑博";
// per1 -> _age = 27;
// per1 -> _sex = @"男";
// per1 -> _hobby = @"张静静";
//
// NSLog(@"%@" ,per1 -> _name);
// [per1 sayHi];
//
// Person *per2 = [[Person alloc ] init ];
// [per2 sayHi];
//
// AudCar *aud1 = [[AudCar alloc] init ];
// [aud1 info];
//
// MobilePhone *mo1 = [[MobilePhone alloc] init ] ;
// [mo1 info];
// 题目一 :
// Fruit *fr = [[Fruit alloc] init];
// [fr info];
// Close *cl = [[Close alloc] init] ;
// [cl info];
// Vegetable *ve = [[Vegetable alloc] init ];
// [ve info];
// Meal *me = [[Meal alloc] init ];
// [me info];
// Flower *flo = [[Flower alloc] init ] ;
// [flo info];
// Tree *tr = [[Tree alloc] init ];
// [tr info];
//题目二 :
// NSLog(@" 开班仪式正式开始 ~ ");
// Classes *cla = [[Classes alloc] init] ;
// [cla info];
// NSLog(@" 任课老师介绍");
// Teacher *tea = [[Teacher alloc]init];
// [tea info];
// NSLog(@" 刘辉老师做自我介绍 :");
// Teacher *tea2 = [Teacher alloc ] ;
// tea2 -> _name = @"刘辉";
// tea2 -> _sex = @"男";
// tea2 -> _age = 32;
// tea2 -> _classes = @"蓝欧学校" ;
// tea2 -> _class = @"c 语言";
// [tea2 info];
// NSLog(@" 学生自我介绍");
// NSLog(@" 第一名学生");
// Student *stu = [[Student alloc] init ];
// [stu info];
// NSLog( @" 第二名学生");
// Student *stu2 = [[Student alloc]init];
// stu2 -> _name = @"付金诗";
// stu2 -> _age = 24;
// stu2 -> _num = 454354;
// [stu2 info];
// NSLog( @" 第三名学生 ");
// Student *stu3 = [[Student alloc] init] ;
// stu3 -> _name = @"裴紫夷";
// stu3 -> _age = 22;
// stu3 -> _num = 456451;
// stu3 -> _sex = @" 女";
// [stu3 info];
//题目三 :
// Rectangle *r1 = [Rectangle alloc ] ;
// Rectangle *r2 = [Rectangle alloc ];
// Rectangle *r3 = [Rectangle alloc ];
// Rectangle *r4 = [Rectangle alloc ];
// r1 -> _x = 0 ;
// r1 -> _y = 0 ;
// r1 -> _width = 320 ;
// r1 -> _height = 480;
// r1 -> _centerX = (r1 -> _x + r1 ->_width )/2 ;
// r1 -> _centerY = (r1 ->_y + r1 ->_height) / 2;
// [r1 info];
// r2 -> _x = 60 ;
// r2 -> _y = 100 ;
// r2 -> _width = 70 ;
// r2 -> _height = 120;
// r2 -> _centerX = (r2 -> _x + r2 ->_width )/2 ;
// r2 -> _centerY = (r2 ->_y + r2 ->_height) / 2;
// [r2 info];
// r3 -> _x = 170 ;
// r3 -> _y = 150 ;
// r3 -> _width = 120 ;
// r3 -> _height = 50;
// r3 -> _centerX = (r3 -> _x + r3 ->_width )/2 ;
// r3 -> _centerY = (r3 ->_y + r3 ->_height) / 2;
// [r3 info];
// r4 -> _x = 180 ;
// r4 -> _y = 280 ;
// r4 -> _width = 90 ;
// r4 -> _height = 160;
// r4 -> _centerX = (r4 -> _x + r4 ->_width )/2 ;
// r4 -> _centerY = (r4 -> _y + r4 ->_height) / 2;
// [r4 info];
// 题目四 :
Number *num1 = [[Number alloc] init] ;
Number *num2 = [[Number alloc] init ];
Number *sum = [[Number alloc] init ];
Number *poor = [[Number alloc ] init ];
Number *indigestion = [[Number alloc] init ];
Number *quotient = [[Number alloc] init ] ;
num1 -> _fenmu = arc4random() ;
num2 -> _fenzi = arc4random() ;
num1->_fenzi = arc4random();
num2->_fenmu = arc4random();
[num1 sample];
[num2 sample];
NSLog(@" 这两个数字分别为 : ");
[num1 info];
[num2 info];
NSLog( @" 它们的和为 : ");
sum -> _fenmu = num1 -> _fenmu * num2 ->_fenmu ;
sum -> _fenzi = num1 -> _fenzi * num2 -> _fenmu + num2 -> _fenzi * num1 ->_fenmu;
[sum sample];
[sum info];
NSLog( @" 它们的差为 : ");
poor -> _fenmu = num1 -> _fenmu * num2 ->_fenmu ;
poor -> _fenzi = num1 -> _fenzi * num2 -> _fenmu - num2 -> _fenzi * num1 ->_fenmu;
[poor sample];
[poor info];
NSLog( @" 它们的积为 : ");
indigestion -> _fenmu = num1 -> _fenmu * num2 ->_fenmu ;
indigestion-> _fenzi = num1 -> _fenzi * num2 -> _fenzi ;
[indigestion sample];
[indigestion info];
NSLog( @" 它们的商为 : ");
quotient -> _fenmu = num1 -> _fenmu * num2 ->_fenzi ;
quotient-> _fenzi = num1 -> _fenzi * num2 -> _fenmu ;
[quotient sample];
[quotient info];