目标C:无法理解循环的逻辑

时间:2011-04-14 12:43:55

标签: objective-c

我是Objective C的新手。目前,我正在尝试目标C中的一些示例。我没有得到正确的输出。我无法理解我在这里包含的正确输出背后的逻辑。

的main.m

#import <Foundation/Foundation.h>
#import "BudObj.h"
#import "Transaction.h"

int main(int argc, char *argv[]) {

        Budget* europeBudget=[Budget new];
        NSMutableArray *transactions=[[NSMutableArray alloc] initWithCapacity:10];

        [europeBudget createBudget:1000.00 withExchangeRate:1.2500];

        Transaction* aTransaction;
        aTransaction = [Transaction new];
        for(int n=1;n<2;n++){

                [aTransaction createTransaction:n*100 ofType:credit];
                [transactions addObject:aTransaction];

        }

        int n=1;
        while (n<3) {

                [aTransaction createTransaction:n*100 ofType:cash];
                [transactions addObject:aTransaction];
                n++;
        }

        do{
                [aTransaction createTransaction:n*100 ofType:credit];
                [transactions addObject:aTransaction];
                n++;
        }while (n<=3);



        for(Transaction *aaTransaction in transactions){
         switch ([aTransaction returnType]) {
                case cash:
                    [europeBudget spendDollars:[aaTransaction returnAmount]];
                    break;
                case credit:
                    [europeBudget changeForeignCurrency:[aaTransaction returnAmount]];
                    break;
                default:
                    break;
         }
        }


        return 0;
}

BudObj.m

#import <Foundation/Foundation.h>
#import "BudObj.h"
#import "Transaction.h"

@implementation Budget

- (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate{
        budget = aBudget;
        exchangeRate = anExchangeRate;
}


- (void) spendDollars:(double)dollars{
    budget = budget - dollars;
        NSLog(@"Converting %0.2f US Dollars into Foreign Currency leaves $%0.2f",dollars,budget);
}

- (void) changeForeignCurrency:(double)foreignCurrency{
    exchangeTransaction = foreignCurrency * exchangeRate;
        budget = budget - exchangeTransaction;
        NSLog(@"Charging %0.2f in Foreign Currency leaves $%0.2f",foreignCurrency,budget);
}

@end

BudObj.h

#import <Foundation/Foundation.h>

@interface 
Budget : NSObject {

        float  exchangeRate;
        double budget;
        double exchangeTransaction;

}

- (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate;
- (void) spendDollars: (double) dollars;
- (void) changeForeignCurrency: (double) foreignCurrency;

@end

Transaction.h

#import <Cocoa/Cocoa.h>

typedef enum{cash,credit} transactionType;

@interface Transaction : NSObject {

        transactionType type;
        double amount;

}

-(void)createTransaction:(double)theAmount ofType:(transactionType)theType;
-(double)returnAmount;
-(transactionType)returnType;

@end

Transaction.m

#import "Transaction.h"

@implementation Transaction

-(void)createTransaction:(double)theAmount ofType:(transactionType)theType{

        type=theType;
        amount=theAmount;

}

-(double)returnAmount{

        return amount;

}

-(transactionType)returnType{

        return type;

}

@end

输出:

2011-04-10 17:31:28.717 BudObj.m[3751:a0f] Charging 300.00 in Foreign Currency leaves $625.00
2011-04-10 17:31:28.719 BudObj.m[3751:a0f] Charging 300.00 in Foreign Currency leaves $250.00
2011-04-10 17:31:28.720 BudObj.m[3751:a0f] Charging 300.00 in Foreign Currency leaves $-125.00
2011-04-10 17:31:28.720 BudObj.m[3751:a0f] Charging 300.00 in Foreign Currency leaves $-500.00

但是预期的输出是

Converting 100.00 US dollars into foreign currency leaves $900.00
Charging 100.00 in foreign currency leaves $775.00
Charging 200.00 in foreign currency leaves $525.00
Charging 300.00 in foreign currency leaves $150.00
Converting 100.00 US dollars into foreign currency leaves $1900.00
Charging 100.00 in foreign currency leaves $1750.00

1 个答案:

答案 0 :(得分:1)

您的最终问题是您使用的是同一个交易对象。您在此行上设置了一次事务处理,并且从未在其他任何地方设置它。我确实从你的评论中看到它曾经是正确的。

Transaction* aTransaction;
aTransaction = [Transaction new];

当您将事务添加到数组时,所有这些事务都指向同一个对象,并且您所做的事情的改变是1 aTransaction真正改变它们。因此,当需要打印时,您可以多次引用同一笔交易所有类型信用,并且您将继续发送相同的退货金额。

另请注意,索引通常为0,int n = 1;可能无法为您提供正确的数字元素。

要尽可能接近您的原始代码来实现您的预​​期输出。 (可能需要一些调整)

#import <Foundation/Foundation.h>
#import "BudObj.h"
#import "Transaction.h"

int main(int argc, char *argv[]) {
        Budget* europeBudget=[[Budget alloc] init];

        [europeBudget createBudget:1000.00 withExchangeRate:1.2500];

        Transaction* aTransaction;
        for(int n=1;n<2;n++){

                aTransaction = [[Transaction alloc] init];
                [aTransaction createTransaction:n*100 ofType:cash];
                [transactions addObject:aTransaction];
                [aTransaction release];
        }

        int n=0;
        while (n<3) {
                aTransaction = [[Transaction alloc] init];
                [aTransaction createTransaction:n*100 ofType:credit];
                [transactions addObject:aTransaction];
                [aTransaction release];
                n++;
        }
        //n = 3 at this point
        do{
                aTransaction = [[Transaction alloc] init];
                [aTransaction createTransaction:n*100 ofType:cash];
                [transactions addObject:aTransaction];
                [aTransaction release];
                n++;
        }while (n<=3);

        //n = 4 at this point
        aTransaction = [[Transaction alloc] init];
        [aTransaction createTransaction:n*100 ofType:credit];
        [transactions addObject:aTransaction];
        [aTransaction release];

        for(Transaction *aaTransaction in transactions){
         switch ([aTransaction returnType]) {
                case cash:
                        [europeBudget spendDollars:[aaTransaction returnAmount]];
                        break;
                case credit:
                        [europeBudget changeForeignCurrency:[aaTransaction returnAmount]];
                        break;
                default:
                        break;
         }
        }
        [transactions release];
        [europeBudget release];
        return 0;
}

现在我没有检查您的美元金额是否正确但是这应该打印出正确的转换/充电顺序。我也没有假设有垃圾收集所以我只是添加了所有版本。