2013-07-14 38 views
0

我正在寫我的第一個真正的客觀C程序,它是一本非常簡單的計算器,就像Stephen Kochan編寫的Objective-C 2.0中的一本書。簡單的Objective-C計算器程序不斷打印

無論如何,無論何時,只要我運行該程序,它只是不斷地重複打印相同的東西,而不是給我輸入其他東西的選項。代碼如下,如果有人可以幫助,我認爲這個問題是介於while循環和switch函數之間的。先謝謝你!

#import <Foundation/Foundation.h> 

@interface Calculator : NSObject { 
    double number, accumulator; 
    char operator; 
} 

    -(void) add: (double) n; 
    -(void) subtract: (double) n; 
    -(void) multiply: (double) n; 
    -(void) divide: (double) n; 


@end 

@implementation Calculator 

-(void) add: (double) n { 
    accumulator += n; 
    NSLog(@"%fl", accumulator); 
} 

-(void) subtract: (double) n { 
    accumulator -= n; 
    NSLog(@"%fl", accumulator); 
} 

-(void) multiply: (double) n { 
    accumulator *= n; 
    NSLog(@"%fl", accumulator); 
} 

-(void) divide: (double) n { 
    if (n == 0) 
     NSLog(@"Error! You can't divide by 0!"); 
    else 
     accumulator /= n; 
     NSLog(@"%fl", accumulator); 

} 

@end 






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

{ 

    @autoreleasepool { 
     double number, accumulator; 
     char operator; 

     Calculator *myCalc = [[Calculator alloc] init]; 

     NSLog(@"Begin calculations by typing a number then S"); 
     scanf("%lf, %c", &accumulator, &operator); 

     while (operator != 'E') { 
      NSLog(@"%lf", accumulator); 
      NSLog(@"What would you like to do next?"); 
      scanf("%lf, %c", &number, &operator); 

      switch (operator) { 
       case '+': 
        [myCalc add: number]; 
        break; 

       case '-': 
        [myCalc subtract: number]; 
        break; 

       case '*': 
        [myCalc multiply: number]; 
        break; 

       case '/': 
        [myCalc divide: number]; 
        break; 

       default: 
        break; 
      } 



      } 

    } 
    return 0; 
} 
+0

嘗試把這些放在你的while的末尾:NSLog(@「你下一步要做什麼?」); scanf(「%lf,%c」,&number,&operator);這將導致程序停止,一旦它到達這些,而不是經常 – user2277872

+0

我不知道我是如何錯過了!但不幸的是,它仍在繼續運行,我不知道爲什麼...... –

+0

@AshleyElisabethStallings呃,所以我不知道我以前的評論已經消失了,但讓我重複一遍:我建議你在試着從Objective-C開始(包括關於C標準庫的知識,不僅僅是語言),否則你會在以後遇到麻煩。 – 2013-07-14 05:45:02

回答

0

簡而言之:不要使用scanf()。它不符合你的想法。

我已經試着解釋什麼是錯的,但基本上它不喜歡換行和東西,它是迂腐。搜索SO爲similar questions。最簡單的辦法是真正有用的東西,例如更換scanf()作爲

char buf[0x100]; 
char *end; 

fgets(buf, sizeof buf, stdin); 
accumulator = strtod(buf, &end); 
while (isspace(*end)) 
    end++; 

operator = *end; 

而且,你的計算器邏輯是有缺陷的。 myCalc對象不會與main()函數共享具有相同名稱的accumulator變量。你的程序基本上不考慮輸入的第一個數字。另外,我沒有看到「S」型零件的用途是什麼,在代碼中絕對沒有輸入「S」的檢查,只有「E」用於結束。


在一個側面說明:我們在C(基本),但它仍然不能使用C++關鍵字作爲標識符一個好主意。假設保留newoperator。調用變量op

另外,作爲設計的改進,可以抽象掉大switch聲明計算器類,並且這樣你可以寫類似[myCalc performOp:'+' withNumber:number];

+0

非常感謝!我會嘗試所有這些,希望它能夠工作! 我同意「S」沒有意義,但我只是通過本書中的一個建議問題,這就是它希望我這樣做。哈哈,但謝謝你! –

+0

@AshleyElisabethStallings不客氣。 – 2013-07-14 05:45:22

0

scanf通常是一個壞的功能使用。將輸入行讀入字符串通常會更好,然後在字符串上使用sscanf(或其他解析器)。

但是,這種情況下的修復很簡單。 scanf返回成功分配的輸入項目的數量。你期待兩個。如果出現錯誤或達到文件結尾,它將返回少於兩個。因此:

 int rc = scanf("%lf, %c", &number, &operator); 
     if (rc < 2) { 
      break; 
     }