#include <stdio.h>
#include <math.h>
void scan_data(char *op, float *operand);
float do_next_op(char op, float operand, float *accumulator);
int main(void)
{
char op;
float operand;
float accumulator = 0;
do
{
printf(" + add\n - subtract\n * multiply\n / divide\n ^ power\n q quit\n\n");
scan_data(&op, &operand);
if(op == 'q' || op == 'Q')
{
printf("Final result is %.1f\n", do_next_op(op, operand, &accumulator));
break;
}
else
{
printf("result so far is %.1f\n", do_next_op(op, operand, &accumulator));
}
}while(op != 'q' || op == 'Q');
}
void scan_data(char *op, float *operand)
{
scanf("%c%f",op, operand);
}
float do_next_op(char op, float operand, float *accumulator)
{
switch(op)
{
case('+'):
*accumulator = *accumulator + operand;
break;
case('-'):
*accumulator = *accumulator - operand;
break;
case('*'):
*accumulator = *accumulator * operand;
break;
case('/'):
*accumulator = *accumulator / operand;
break;
case('^'):
*accumulator = pow(*accumulator,operand);
break;
}
return *accumulator;
}
Я пытаюсь кодировать «простой» калькулятор, где, если я наберу
+ 5.0, результат пока равен 5,0
^ 2, пока результат равен 25,0
/ 2.0 результат до сих пор равен 12,5
Q 0, конечный результат равен 12,5
Проблема в том, что код будет "правильно" выводить первую операцию, но если я добавлю больше операции после этого Я не обновляюсь до нового значения. Как я могу исправить код, чтобы сделать то, что я собираюсь сделать?
Извините, если у меня нет формулировки и формата моего вопроса, я не знаю, как правильно его задать.