2013-07-03 2 views
-4

지금 나는 Stephen Kockan의 "Programming in C, 3rd edition"이라는 책을 읽고 c로 프로그래밍하는 법을 배우고 있습니다. 연습 문제 6-4는 정말 두통을 안겨줍니다. 이 책에서는 말한다 :연습 문제 6-4 프로그래밍 C

Write a program that acts as a simple "printing" calculator. 

The program should allow the user to type in expressions of the form 

다음 operatros 프로그램에 의해 인식되어야한다 숫자 연산자 :

'+' '-' '*' '/' 'S' 'E' 

의 S 운영자가 프로그램 "이 설정을 알려줍니다 누적 기 "를 입력 번호로 변경하십시오. E 연산자는 실행이 으로 끝나도록 프로그램에 알립니다. 산술 연산은 누적 기의 내용에 대해 수행 된 키 수는 seconcd 피연산자로 사용됩니다.

Here is a link 내가 어떻게 알아 냈는지.

불행히도 Objective-C에 있지만 (여전히 운동이 동일합니다!) 이해할 수 없습니다. Objective-C 구문.

UPDATE는

이것은 내가 지금까지 만든 것입니다 :

// "Printing" Calculator 

#include <stdio.h> 

int main(void) 
{ 
    char operator; 
    float value, result, accumulator; 

    printf("Begin Calculations...\n\n"); 

    operator = '0'; 

    while (operator != 'E') 
    { 
     scanf("%f %c", &value, &operator); 
     switch(operator) 
     { 
      case 'S': 
       accumulator = value; 
       printf("The accumulator = %f", accumulator); 
       break; 
      case '+': 
       result = accumulator + value; 
       printf("%f + %f = %f", accumulator, value, result); 
       break; 
      case '-': 
       break; 
      case '*': 
       break; 
      case '/': 
       break; 
      default: 
       printf("Unknown operator"); 
       break; 
     } 
    } 

    printf("Calculations terminated"); 

    return 0; 
} 
나는 scanf() 기능을 사용하고, 작업에 대한 값과 값 모두를 읽는 방법을 알아낼 수 없습니다

누산기. 그 두 가지가 같지 않을 수도 있기 때문입니다.

+4

귀하의 질문은 ... – Joe

+0

죄송합니다, 무슨 일입니까? – Nobilis

+0

Objective C에서 순수 C 로의 변환을 기대하십니까? – AurA

답변

0

여기에 C 코드가 있습니다.

#include <stdio.h> 
#include <conio.h> 

int main (void) 
{ 
    int loop; 
    float value, 
    acc = 0; 
    char o; 
    printf ("Simple Printing Calculator Program\n\n"); 
    printf ("\nKEY: \n+ Add \n- Subtract \n* Multiply \n/ Divide\n"); 
    printf ("S Set Accumulator \nE End Program\n\n"); 
    printf ("Enter a number and an operator, then press enter\n\n"); 
    printf ("Type in your expression: "); 
    for (loop=0 ; loop>-1; ++loop) 
    { 
     { 
     scanf ("%f %c", &value, &o); 
     if (o == '+') 
     { 
      acc = acc + value; printf ("\n%.2f\n",acc); 
     } 
     else if (o == '-') 
     { 
      acc = acc - value; printf ("\n%.2f\n",acc); 
     } 
     else if (o == '*') 
     { 
      acc = acc * value; printf ("\n%.2f\n",acc); 
     } 
     else if (o == 'S') 
     { 
      acc = value; printf ("\n%.2f\n",acc); 
     } 
     else if (o == 'E') 
     { 
      printf ("\nFinal Value: %.2f\n\nGoodbye!",acc); loop = loop + 1000; 
     } 
     else if (o == '/') 
      if (value == 0) 
      { 
       loop = loop - loop -100; 
       printf ("\nDivision by zero.\n"); 
      } 
      else 
      { 
       acc = acc/value; printf ("\n%.2f\n", acc); 
      } 
     else 
     { 
      loop = loop -loop-100; 
      printf ("\nUnknown operator.\n"); 
     } 
    } 
    getch(); 
    return 0; 
} 
+0

왜 1000 개의 표현식이 끝나야합니까? 합계의 선언은 혼란의 지점에 대해 모호하며 들여 쓰기가 없습니다. –

+0

은 한 번 전달한 후 중단되며 sum의 선언은 여전히 ​​혼란 스럽습니다. –

+0

왜 루프 카운터를 사용합니까? 왜 그걸 벗어 버리지 않는거야? –