Exemplo n.º 1
0
void RecreatorImpl::prependKeyword(CommandCode command_code)
{
    std::string string = command_code.getKeyword();
    swapTop(string);
    append(' ');
    append(string);
}
Exemplo n.º 2
0
int main(int argc, char const *argv[])
{
    puts("(calculator v0.2)");
    puts("enter the expression:");
	int type;
	double op2;
	char s[MAXOP];
	extern int sp;
 	while ((type = getop(s)) != EOF)
	{
		if (sp >= 2) {
			switch (type) {
                case '%' :
                    op2 = pop();
                    if(op2) {
                        push(fmod(pop(), op2)) ;
                    }else 
					printf("\nError: Division by zero!");
                    continue;
                case '+' :
                    push (pop() + pop ());
                    continue;
                case '-' :
                    op2 = pop();
                    push (pop() - op2);
                    continue;
                case '*':
                    push (pop() * pop());
                    continue;
                case '/':
                    op2 = pop ();
                    if (op2 != 0.0)
                    {
                        push (pop() / op2);
                    }
                    else {
                        printf("error : zero divisor\n");
                    }
                    continue;
                case '~':
                    swapTop();
                    continue;
			}
		}else if (sp == 1){
			if (type == '-'){
				push(-pop());
                continue;
			}else if (type == '+' ||
                      type == '*' ||
                      type == '/'){
				printf("error : no enough number to calculate\n");
                continue;
			}
		}
        switch (type){
            case NUMBER :
                push (atof (s));
                continue;
            case IDENTIFIER :
                exFunction(s);
                continue;
            case '@':
                printAll();
                continue;
            case '\n':
                printTop();
                continue;
            case '$':
                flush();
                fprintf(stderr, "unknown operand and the stack is flushed, try again \n");
                continue;
            case '_':
                printf("%lf", copyTop());
                continue;
            default:
                fprintf(stderr, "no such command: %c\n", type);
        }
	}
	return 0;
}