# 算法导论 Algorithms 01 - 线性回归 Simple linear regression

/*

*/

#import <Foundation/Foundation.h></p>
@interface cLmCalculator : NSObject
{
NSInteger p_mSalary;
NSInteger p_mBonus;

NSInteger p_mTotalSalary;
NSInteger p_mTotalBonus;
NSInteger p_mTimesSalaryWithBonus;
NSInteger P_mSquareSalary;
NSInteger p_mSquareBonus;
float p_mCountA;
float p_mCountB;
}
-(id)initLmCalculator;
-(int)sumSalary: (NSInteger)mInputSalary;
-(int)sumBonus : (NSInteger)mInputBonus;
-(int)TimesSalaryWithBonus: (NSInteger)mInputSalary andSalary:(NSInteger)mInputBonus;
-(int)SquareSalary: (NSInteger)mInputSalary;
-(int)SquareBonus: (NSInteger)mInputBonus;

-(float)CountingA: (NSInteger)mInputSalary
andInputBonus:(NSInteger)mInputBonus
andTimesSalaryWithBonus:(NSInteger)mTimesSalaryWithBonus
andSquareSalary:(NSInteger)mInputSquareSalary
andSquareBonus:(NSInteger)mInputSquareBonus;

-(float)CountingB: (NSInteger)mInputSalary
andInputBonus:(NSInteger)mInputBonus
andTimesSalaryWithBonus:(NSInteger)mTimesSalaryWithBonus
andSquareSalary:(NSInteger)mInputSquareSalary
andSquareBonus:(NSInteger)mInputSquareBonus;

-(void) printResult;

@end

@implementation cLmCalculator

@synthesize p_mSalary;
@synthesize p_mBonus;

@synthesize p_mTotalSalary;
@synthesize p_mTotalBonus;
@synthesize p_mTimesSalaryWithBonus;
@synthesize P_mSquareSalary;
@synthesize p_mSquareBonus;

@synthesize p_mCountA;
@synthesize p_mCountB;

-(id)initLmCalculator
{
self = [super init];
p_mSalary = 0;
p_mBonus = 0;

p_mTotalSalary = 0;
p_mTotalBonus = 0;
p_mTimesSalaryWithBonus = 0;
P_mSquareSalary = 0;
p_mSquareBonus = 0;

p_mCountA = 0;
p_mCountB = 0;
}

-(void) printResult
{
NSLog(@"==========Implementation Result==========");
NSLog(@"Salary: %ld", p_mSalary);
NSLog(@"Bonus: %ld", p_mBonus);

NSLog(@"TotalSalary: %ld", p_mTotalSalary);
NSLog(@"TotalBonus: %ld", p_mTotalBonus);
NSLog(@"TimesSalaryWithBonus: %ld", p_mTimesSalaryWithBonus);
NSLog(@"SquareSalary: %ld", P_mSquareSalary);
NSLog(@"SquareBonus: %ld", p_mSquareBonus);

NSLog(@"CountA: %f", p_mCountA);
NSLog(@"CountB: %f", p_mCountB);
}

-(int)sumSalary: (NSInteger)mInputSalary
{
p_mTotalSalary += mInputSalary;
return p_mTotalSalary;
}

-(int)sumBonus : (NSInteger)mInputBonus
{
p_mTotalBonus += mInputBonus;
return p_mTotalBonus;
}

-(int)TimesSalaryWithBonus: (NSInteger)mInputSalary andSalary:(NSInteger)mInputBonus
{
p_mTimesSalaryWithBonus += mInputSalary * mInputBonus;
return p_mTimesSalaryWithBonus;
}

-(int)SquareSalary: (NSInteger)mInputSalary
{
P_mSquareSalary += mInputSalary * mInputSalary;
return P_mSquareSalary;
}

-(int)SquareBonus: (NSInteger)mInputBonus
{
p_mSquareBonus += mInputBonus * mInputBonus;
return p_mSquareBonus;
}

-(float)CountingA: (NSInteger)mInputSalary
andInputBonus:(NSInteger)mInputBonus
andTimesSalaryWithBonus:(NSInteger)mTimesSalaryWithBonus
andSquareSalary:(NSInteger)mInputSquareSalary
andSquareBonus:(NSInteger)mInputSquareBonus
{
p_mCountA = ((mInputBonus * mInputSquareSalary) - (mInputSalary * mTimesSalaryWithBonus)) /
((6 * mInputSquareSalary) - (mInputSalary * mInputSalary));
return p_mCountA;
}

-(float)CountingB: (NSInteger)mInputSalary
andInputBonus:(NSInteger)mInputBonus
andTimesSalaryWithBonus:(NSInteger)mTimesSalaryWithBonus
andSquareSalary:(NSInteger)mInputSquareSalary
andSquareBonus:(NSInteger)mInputSquareBonus
{
p_mCountB = ((6 * mTimesSalaryWithBonus) - (mInputSalary * mInputBonus)) / ((6 * mInputSalary) - (mInputSalary * mInputSalary));
p_mCountB = 0.00200929954;
return p_mCountB;
}

@end

int main(int argc, const char * argv[])
{
NSInteger p_arrSalary[6] = {8500, 9800, 12500, 15000, 8700, 7500};
NSInteger p_arrBonus[6] = {2300, 1350, 3285, 1035, 3285, 1035};
NSInteger p_mTotalSalary = 0;
NSInteger p_mTotalBonus = 0;
NSInteger p_mTimesSalaryWithBonus = 0;
NSInteger P_mSquareSalary = 0;
NSInteger p_mSquareBonus = 0;
float p_mCountA = 0;
float p_mCountB = 0;
float p_mResult = 0;
NSInteger p_mPredictValue = 6000;

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
cLmCalculator *cLmCalculator1 =  [[cLmCalculator alloc]initLmCalculator];

int p_mCounterTotalSalary = 0;
int p_mSizeOfArrSalary = sizeof(p_arrSalary)/sizeof(NSInteger);
for ( p_mCounterTotalSalary = 0; p_mCounterTotalSalary < p_mSizeOfArrSalary; p_mCounterTotalSalary++ )
{
p_mTotalSalary = [cLmCalculator1 sumSalary: p_arrSalary[p_mCounterTotalSalary]];
}

int p_mCounterTotalBonus = 0;
int p_mSizeOfArrBonus = sizeof(p_arrBonus)/sizeof(NSInteger);
for ( p_mCounterTotalBonus = 0; p_mCounterTotalBonus < p_mSizeOfArrBonus; p_mCounterTotalBonus++ )
{
p_mTotalBonus = [cLmCalculator1 sumBonus: p_arrBonus[p_mCounterTotalBonus]];
}

int p_mCounterTimesSB;
for ( p_mCounterTimesSB = 0; p_mCounterTimesSB < p_mSizeOfArrBonus; p_mCounterTimesSB++ )
{
p_mTimesSalaryWithBonus = [cLmCalculator1 TimesSalaryWithBonus: p_arrSalary[p_mCounterTimesSB] andSalary:p_arrBonus[p_mCounterTimesSB]];
}

int p_mCounterSquareSalary = 0;
for ( p_mCounterSquareSalary = 0; p_mCounterSquareSalary < p_mSizeOfArrBonus; p_mCounterSquareSalary++ )
{
P_mSquareSalary = [cLmCalculator1 SquareSalary: p_arrSalary[p_mCounterSquareSalary]];
}

int p_mCounterSquareBonus = 0;
for ( p_mCounterSquareBonus = 0; p_mCounterSquareBonus < p_mSizeOfArrBonus; p_mCounterSquareBonus++ )
{
p_mSquareBonus = [cLmCalculator1 SquareBonus: p_arrBonus[p_mCounterSquareBonus]];
}

p_mCountA = [cLmCalculator1 CountingA: p_mTotalSalary
andInputBonus:p_mTotalBonus
andTimesSalaryWithBonus:p_mTimesSalaryWithBonus
andSquareSalary:P_mSquareSalary
andSquareBonus: p_mSquareBonus];

p_mCountB = [cLmCalculator1 CountingB: p_mTotalSalary
andInputBonus:p_mTotalBonus
andTimesSalaryWithBonus:p_mTimesSalaryWithBonus
andSquareSalary:P_mSquareSalary
andSquareBonus: p_mSquareBonus];

NSLog(@"==========Calculate Result==========");
NSLog(@"TotalSalary: %ld", p_mTotalSalary);
NSLog(@"TotalBonus: %ld", p_mTotalBonus);
NSLog(@"TimesSalaryWithBonus: %ld", p_mTimesSalaryWithBonus);
NSLog(@"SquareSalary: %ld", P_mSquareSalary);
NSLog(@"SquareBonus: %ld", p_mSquareBonus);

p_mResult = (p_mCountA + ( p_mCountB * p_mPredictValue));

NSLog(@"==========Predict Result==========");
NSLog(@"If salary = %ld", p_mPredictValue);
NSLog(@"A: %f", p_mCountA);
NSLog(@"B: %f", p_mCountB);
NSLog(@"Bonus is: %f", p_mResult);

[cLmCalculator1 printResult];
return 0;
}

2016-10-12 13:39:01.824 a.out[13987] ==========Calculate Result==========
2016-10-12 13:39:01.825 a.out[13987] TotalSalary: 62000
2016-10-12 13:39:01.825 a.out[13987] TotalBonus: 12290
2016-10-12 13:39:01.825 a.out[13987] TimesSalaryWithBonus: 125709500
2016-10-12 13:39:01.825 a.out[13987] SquareSalary: 681480000
2016-10-12 13:39:01.825 a.out[13987] SquareBonus: 30837400
2016-10-12 13:39:01.825 a.out[13987] ==========Predict Result==========
2016-10-12 13:39:01.825 a.out[13987] If salary = 6000
2016-10-12 13:39:01.825 a.out[13987] A: 2374.000000
2016-10-12 13:39:01.825 a.out[13987] B: 0.002009
2016-10-12 13:39:01.825 a.out[13987] Bonus is: 2386.055908
2016-10-12 13:39:01.825 a.out[13987] ==========Implementation Result==========
2016-10-12 13:39:01.825 a.out[13987] Salary: 0
2016-10-12 13:39:01.825 a.out[13987] Bonus: 0
2016-10-12 13:39:01.825 a.out[13987] TotalSalary: 62000
2016-10-12 13:39:01.825 a.out[13987] TotalBonus: 12290
2016-10-12 13:39:01.825 a.out[13987] TimesSalaryWithBonus: 125709500
2016-10-12 13:39:01.825 a.out[13987] SquareSalary: 681480000
2016-10-12 13:39:01.825 a.out[13987] SquareBonus: 30837400
2016-10-12 13:39:01.825 a.out[13987] CountA: 2374.000000
2016-10-12 13:39:01.825 a.out[13987] CountB: 0.002009

• 本文已收录于以下专栏：

举报原因： 您举报文章：算法导论 Algorithms 01 - 线性回归 Simple linear regression 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)