내 rpn- 계산기가 작동하지만 문제가 있습니다. 문제는 그 과정에서 발생하는 모든 연속적인 계산을 인쇄한다는 것입니다.내 rpn 계산기에서 다중 출력을 피하기 위해 printf를 둘 곳은 어디입니까?
나는 이것을 고치기 위해 여러 가지 방법을 시도했다. 최신 계산은 모든 계산에 의해 올라가는 정수를 더하고,이 정수가 0보다 크고 스택에 하나의 숫자 만 인쇄되면 printf가 인쇄된다.
그러나 계산이 여러 번 진행될 때 (예 : 5 5 + 10 5 * * 쓰기) 문제가 발생하면 첫 번째 계산 후에 스택에 항목이 하나만 있기 때문에 10 500이 인쇄됩니다 .
어떻게 해결할 수 있습니까?
#define MAX_STACK_SIZE 100
#define MAX_BUFFER_SIZE 100
char buff[MAX_BUFFER_SIZE];
double x, stack[MAX_STACK_SIZE];
double t;
int i, k=0, num_operand=0;
int main(void) {
while(x != 'q') {
if(scanf("%s", buff) < 1)
{ return 0;
}
if(isdigit(buff[0]) || isdigit(buff[1])) {
sscanf(buff, "%lf", &x);
if (num_operand < MAX_STACK_SIZE)
{
stack[num_operand]=x;
num_operand ++;
} else { printf("Make stack bigger\n");}
} else {
switch(buff[0]) {
case '+': stack[num_operand - 2] = stack[num_operand - 1] + stack[num_operand - 2];
num_operand --;
num_operand --;
t = stack[num_operand];
k++;
num_operand ++;
break;
case '-': stack[num_operand - 2] = stack[num_operand - 2] - stack[num_operand - 1];
num_operand --;
num_operand --;
t = stack[num_operand];
k++;
num_operand ++;
break;
case '/': stack[num_operand - 2] = stack[num_operand - 2]/stack[num_operand - 1];
num_operand --;
num_operand --;
t = stack[num_operand];
k++;
num_operand ++;
break;
case '*': stack[num_operand - 2] = stack[num_operand - 1] * stack[num_operand - 2];
num_operand --;
num_operand --;
t = stack[num_operand];
k++;
num_operand ++;
break;
} }
if (num_operand == 1 && k !=0) {
k = 0;
printf("%lf \n", t); }
}
}
'x! = 'q'' :'x'의 형식이 double입니다. – BLUEPIXY
읽는 동안 표현식을 비트 단위로 평가하는 대신 전체 표현식을 읽고 평가하십시오. – molbdnilo
참고 :'printf ("% lf \ n", t);'는 "10 500"이 인쇄되지 않지만 "10.000000 \ n500.000000 \ n"'이됩니다. 세부 사항은 중요합니다. – chux