#include<stdio.h>
int ask(int);
double run(char opcode, double operand1, double opernad2);
int main(void)
{
while(1)
{
double operand1, operand2, result;
int count = 0;
char opcode,quit;
count = ask(count);
scanf("%lf", &operand1);
count = ask(count);
scanf("%lf", &operand2);
printf("원하시는 연산기호를 입력하십시오 ");
getchar();
opcode = getchar();
while(1)
{
if(opcode =='+'|| opcode == '-'|| opcode == '*'|| opcode == '/')
{
result = run(opcode, operand1, operand2);
break;
}
else
{
printf("잘못된 연산기호 입니다. ");
printf("다시 연산기호를 입력해 주십시오 ");
getchar();
opcode = getchar();
}
}
printf(" %lf %c %lf = %lf ", operand1, opcode, operand2, result);
printf(" 다시 하시겠습니까? (Y/n) ");
getchar();
quit = getchar();
if(quit == 'N' || quit == 'n')
break;
}
printf("Bye :) ");
return 0;
}
int ask(int count)
{
printf("%d번째 숫자를 입력해주십시오. ", ++count);
return count;
}
double run(char opcode, double operand1, double operand2)
{
switch(opcode)
{
case('+'):
return operand1 += operand2;
break;
case('-'):
return operand1 -= operand2;
break;
case('*'):
return operand1 *= operand2;
break;
case('/'):
return operand1 /= operand2;
}
}
뭔가가 더 잘못된 느낌인데
아까보단 나아 ㅋㄷㅋㄷ
ask 를 좀 더 똘똘하게 만들어 보자.
double ask(int param){ double input; printf("%d ... ", param); scanf("%lf", &input;); return input);
operand1 = ask(++count); operand2 = ask(++count);
위에 위에 끝부분 return 앞에 getchar(); 하나 박아버리고 return input; }
그리고 c++ 이면 다형성 지원하니까 (같은 이름에 인자만 다른 여러 함수 선언)
int ask(){ int input; printf("......."); input = getchar(); return input; } 도 만들던지 연산자 입력용으로
opcode = ask(); 가 되겠지.
if (strchr("+-*/", opcode)) 를 이용하면 if 문을 간소화 할 수 있어.
대신 string.h 를 include 해야해.
opcode = ask(); 가 while 안에 있으면, 아래 else 문의 두줄은 사라지겠지? ㅋㄷ
ask 를 printf 처럼 가변인자를 처리할 수 있게 만들면 범용성이 높아질텐데 그러면 난이도가 좀 올라가겠구나 ㅋㄷㅋㄷ
void ask(const char* msg, const char* format_string, ...); 처럼 만들어서 말야.
const char* string_format(const char* format_string, ...); 도 만들어서 메시지도 만들어주게
아씨 댓글론 안써지네 ㅡ,.ㅡ 뭐 여튼.
http://dblack.tk
커뮤니티 사이트 입니다 많은 이용 부탁 드립니다.