#include<stdio.h>
double ask(int param);
char ask();
char query();
double run(char opcode, double operand1, double opernad2);
int main(void)
{
double operand1, operand2, result;
int plus = 0;
int count = 0;
char opcode, quit;
while(1)
{
switch(plus)
{
case 0:
operand1 = ask(++count);
case 1:
operand2 = ask(++count);
opcode = ask();
while(1)
{
if(opcode =='+'|| opcode == '-'|| opcode == '*'|| opcode == '/')
{
result = run(opcode, operand1, operand2);
break;
}
else
{
printf("잘못된 연산기호 입니다. ");
opcode = getchar();
}
}
printf(" %lf %c %lf = %lf ", operand1, opcode, operand2, result);
printf("추가 계산을하시겠습니까?(Y/n) ");
getchar();
plus = getchar();
while(1)
{
if(plus == 'Y' || plus == 'y')
{
operand1 = result;
plus = 1;
break;
}
else if(plus == 'N' || plus == 'n')
{
printf(" 다시 하시겠습니까? (Y/n) ");
getchar();
quit = getchar();
while(1)
{
if(quit == 'N' || quit == 'n')
{
break;
}
else if(quit == 'Y' || quit == 'y')
{
operand1 = operand2 = result = 0;
plus = count = 0;
opcode = quit = 0;
break;
}
else
{
quit = query();
}
}
break;
}
else
{
plus = query();
}
}
}
break;
}
printf("Bye :) ");
return 0;
}
double ask(int param)
{
double input;
printf("%d번째 숫자를 입력해주십시오. ", param);
scanf("%lf", &input);
getchar();
return input;
}
char ask()
{
char input;
printf("연산기호를 입력해 주십시오. ");
input = getchar();
return input;
}
char query()
{
char input;
printf("Y or N ");
printf("다시 입력해 주십시오. ");
input = getchar();
return input;
}
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;
}
}
근데 만약 y/n 같은 질문할때 2번씩뜨는데 이유를 모르겠어
즉
y/n 형에서 숫자 3을입력하면 보통 ("잘못된 입력입니다 ")이런식으로 한번떠야되는데
("잘못된 입력입니다 ")
("잘못된 입력입니다 ")
이런식으로나와
printf에 %lf 쓰는 거 비표준이니깐 %f로 죄다 고쳐라. (scanf는 double에 %lf 쓰는 게 맞는데 printf는 float이든 double이든 둘 다 l을 안 붙이고 %f를 써야지 표준)
ㄴ그뭐지 별로 상관없다길래 단일화 시켰는데 f 로 써주는게 편하려나 ?
아니 편하려나 랜다 좋으려나 ?
누가 상관없다고 함? 멍청하네.
fflush(stdin); 이랑 동급의 극혐 코드야.
그렇구만 잘못알고있을뻔했네 고마워!
얘 코딩시작한지 이틀쯤 된 늅늅이라고 보면 돼.
기능추가가 어느정도 됐으면 줄여봐 ㅋㅋ 기네.
한번 줄여볼게 잠시만
http://dblack.tk
커뮤니티 사이트 입니다 많은 이용 부탁 드립니다.