1

我正在开发一个程序,它可以让你使用毕达哥拉斯方法计算三角形的边。我最近发布了我的第一个问题。我如何对其进行编码,以便您也可以计算出 B 或 A。到目前为止,这是我的代码:

毕达哥拉斯计算器

  #import "PythagorasCalc.h"

  @implementation PythagorasCalc

 - (double)calculatePythagorasValue {
   return sqrt(A*A+B*B);
 }

//Access Code

- (double)A {
   return A;
}

- (void)setA: (double)value {
    if(A != value) {
       A = value;
    }
}

- (double)B {
  return B;
}

- (void)setB: (double)value {
   if(B != value) {
       B = value;
    }
}

- (double)C {
    return C;
}

- (void)setC: (double)value {
   if(C != value) {
       C = value;
    }
 }

@end

毕达哥拉斯计算器

    #import <Cocoa/Cocoa.h>

@interface PythagorasCalc : NSObject {
    @private
    int A;
    int B;
    int C;
}

@property (nonatomic) double A;
@property (nonatomic) double B;
@property (nonatomic) double C;

- (double)calculatePythagorasValue;

@end

AppDelegate.m

#import "PythagorasCalculatorAppDelegate.h"

@implementation PythagorasCalculatorAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    calculator = [[PythagorasCalc alloc] init];
}

- (IBAction)calculateClicked:(id)sender {

    double A = _ATextField.doubleValue;
    double B = _BTextField.doubleValue;
    double C = _CTextField.doubleValue;

    calculator.A = A;
    calculator.B = B;
    calculator.C = C;

    _CTextField.doubleValue = [calculator calculatePythagorasValue];  
}
@end

AppDelegate.h

#import <Cocoa/Cocoa.h>
#import "PythagorasCalc.h"

@interface PythagorasCalculatorAppDelegate : NSObject <NSApplicationDelegate> {
    //Public Calculator Object
    PythagorasCalc *calculator;
}

@property (assign) IBOutlet NSWindow *window;
- (IBAction)calculateClicked:(id)sender;
@property (weak) IBOutlet NSTextField *ATextField;
@property (weak) IBOutlet NSTextField *BTextField;
@property (weak) IBOutlet NSTextField *CTextField;

@end
4

0 回答 0