#import
@inte***ce fraction : nsobject
nsinteger _numerator;//分數的分子
nsinteger _denominator;//分數的分母
//構造方法的設計
- (id)initwithnumerator:(nsinteger)numerator anddenominator:(nsinteger)denominator;
//setter方法
- (void)setnumerator:(nsinteger)numerator;
- (void)setdenominator:(nsinteger)denominator;
- (void)setnumorator:(nsinteger)numerator anddenominator:(nsinteger)denominator;
//getter方法
- (nsinteger)numerator;
- (nsinteger)denominator;
//分數的加減乘除運算
- (void)addfraction:(fraction *)afraction;
- (void)subfraction:(fraction *)afraction;
- (void)mulfraction:(fraction *)afraction;
- (void)divfraction:(fraction *)afraction;
//實現約分
- (void)reduce;
//列印分數
- (void)printfraction;
@end
@implementationfraction
//init方法實現
- (id)initwithnumerator:(nsinteger)numerator anddenominator:(nsinteger)denominator
if (self = [super init]) {
_numerator = numerator;
_denominator = denominator;
returnself;
//srtter方法實現
- (void)setnumerator:(nsinteger)numerator
_numerator = numerator;
- (void)setdenominator:(nsinteger)denominator
_denominator = denominator;
- (void)setnumerator:(nsinteger)numerator anddenominator:(nsinteger)denominator
_numerator = numerator;
_denominator = denominator;
//getter方法
- (nsinteger)numerator
return _numerator;
- (nsinteger)denominator
return _denominator;
//分數的加法運算
- (void)addfraction:(fraction *)afraction
_numerator = [self numerator] * [afraction denominator] + [afraction numerator] * [self denominator];
_denominator = [self denominator] * [afraction denominator];
//分數的減法運算
- (void)sbufraction:(fraction *)afraction
_numerator = [self numerator] * [afraction denominator] - [afraction numerator] * [self denominator];
_denominator = [self denominator] * [afraction denominator];
//分數的懲罰運算
- (void)mulfraction:(fraction *)afraction
_numerator = [self numerator] * [afraction numerator];
_denominator = [self denominator] * [afraction denominator];
//分數的除法運算
- (void)divfraction:(fraction *)afraction
_numerator = [self numerator] * [afraction denominator];
_denominator = [self denominator] * [afraction numerator];
//分數的約分
- (void)reduce
nsinteger n = _numerator;
nsinteger d = _denominator;
while (d) {
nsinteger temp = n%d;
n = d;
d = temp;
_numerator /= n;
_denominator /= n;
- (void)printfraction
nslog(@"fraction = %li/%li", _numerator, _denominator);
@end
int main(int argc, const
char * argv) {
@autoreleasepool{
fraction *f1 = [[fraction alloc] initwithnumerator:1 anddenominator:3];
fraction *f2 = [[fraction alloc] initwithnumerator:2 anddenominator:5];
// [f1 addfraction:f2];
// [f1 printfraction];
[f1 mulfraction:f2];
[f1 printfraction];
fraction *f3 = [[fraction alloc] initwithnumerator:1 anddenominator:3];
fraction *f4 = [[fraction alloc] initwithnumerator:4 anddenominator:15];
[f3 addfraction:f4];
[f3 reduce];
[f3 printfraction];
return0;
分數類的實現
實現乙個分數類,並過載其加減運算 include include using namespace std class fraction 無參建構函式 fraction int,int void set int aa,int bb 設定分子分母 void show 顯示分數 fraction add ...
oc類的實現
oc方法的簽名 方法簽名由多部分組成,每一部分簽名都說明引數含義,所以方法的簽名具有自說明性,例如 void setname nsstring aname age int aage方法簽名為setname age 說明這個方法是用來設定姓名和年齡的,具有兩個引數 void setname nsstr...
實現分數類的過載
程式頭部注釋開始 程式的版權和版本宣告部分 檔名稱 作 者 苗影 完成日期 2012 年 4 月 10 日 版 本 號 對任務及求解方法的描述部分 輸入描述 問題描述 程式輸出 程式頭部的注釋結束 includeusing namespace std class cfraction void sim...