关于stack postfix calculator 请大师指导啊
首先是先要读取文件, 文件里面是些数字与预算符号。然后要把读取的文件让它自动运算!然后我可以单独读取文件,但是不清楚怎么把他加入进去。这个我写的程序是可以自己输入数字自己运算的, 但是还有个就是 我输入4,8,/,它得的是2,按道理是0.5! 它是用8/4做的 !求指导呀 #include <stdio.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#define STACKSIZE 6
int stack_pointer = 0;
int STACKDISPLAY = 0;
long double stack[STACKSIZE] = {0.0};
int string_equal(const char* first, const char *second)
{
return (strcmp(first, second) == 0);
}
void show_stack(char message[])
{
int i;
if (!STACKDISPLAY)
{
return;
}
printf("Displaying stack at end of function %s\nStack contents:\n\r", message);
for (i = stack_pointer; i >= 0; i--)
printf("stack[%d] = %f\n\r", i, stack[i]);
printf("\n\n");
}
double pop_off_stack(void)
{
double return_value;
return_value = stack[stack_pointer];
if (stack_pointer > 0)
{
stack_pointer--;
}
show_stack("pop_off_stack");
return return_value;
}
double top_of_stack_value(void)
{
return stack[stack_pointer];
}
double push_onto_stack(double value)
{
int i;
stack[++stack_pointer] = value;
assert(stack_pointer < (STACKSIZE + 1));
if (stack_pointer == STACKSIZE)
{
for (i = 0; i < STACKSIZE; i++)
stack[i] = stack[i + 1];
stack_pointer = STACKSIZE - 2;
}
show_stack("push_onto_stack");
return value;
}
int main(int argc, char **argv)
{
char c, instr[80];
char input[80]; memset(input, '\0', 80);
double aDouble;
FILE*pRead;
while (1)
{
scanf("%s", &input);
long double numericalInput = atof(input);
if(numericalInput == 0.0)
{
// then try parsing the commands, like "chs", "abs", "sin", etc.
// use the stringEquals() method.
if(string_equal(input, "+"))
{
// addition
printf(" The sum is %f\n", push_onto_stack(pop_off_stack() + pop_off_stack()));
} else if (string_equal(input, "-")) {
// subtraction goes here
printf(" The sum is %f\n", push_onto_stack(pop_off_stack() - pop_off_stack()));
} else if (string_equal(input, "*")) {
//multiplcation here
printf(" The sum is %f\n", push_onto_stack(pop_off_stack() * pop_off_stack()));
} else if (string_equal(input, "/")) {
//division here
printf( "The sum is %f\n", push_onto_stack(pop_off_stack() / pop_off_stack()));
} else if (string_equal(input, "chs")) {
//change signs
printf( "The number is %f\n", push_onto_stack(pop_off_stack()*(-1)));
} else if (string_equal(input, "abs")) {
//absolute value
printf("the result is: %f\n", push_onto_stack(abs((double)pop_off_stack())));
} else if (string_equal(input, "sqrt")) {
//square root here
printf("the result is: %f\n", push_onto_stack(sqrt((double)pop_off_stack())));
} else if (string_equal(input, "sin")) {
//sin function here
printf("the result is: %f\n", push_onto_stack(sin((double)pop_off_stack())));
} else if (string_equal(input, "cos")) {
//cos function here
printf("the result is: %f\n", push_onto_stack(cos((double)pop_off_stack())));
} else if (string_equal(input, "tan")) {
//tan function here
printf("the result is: %f\n", push_onto_stack(tan((double)pop_off_stack())));
} else if (string_equal(input, "q") || string_equal(input, "Q")) {
//quitting program
printf("Quitting Good Bye \n");
exit(0);
}
} else {
push_onto_stack (numericalInput);
}
}
return 0;
}