Ejemplo n.º 1
0
int main() {

    char   operator[MAXOP] = ""; // Operator
    char   optype;               // Operator type
    double operand;              // Temporary operand
    char   lastop = '\0';        // Last operand

    printf("RPN Calculator (use 'Ctrl-D' or enter \"quit\" to exit)\n\n");
    printf("> ");
    while ((optype = getop(operator)) != (char)EOF) {
        switch(optype) {
            case NUMBER:
                dapush(atof(operator));
                break;
            case NAME:
                mathfun(operator);
                break;
            case '+':
                if (dasize() < 2)
                    printf("Less than two items are on the stack\n");
                else
                    dapush(dapop() + dapop());
                break;
            case '-':
                if (dasize() < 1)
                    printf("Empty stack\n");
                else if (dasize() < 2)
                    dapush(-dapop());
                else {
                    operand = dapop();
                    dapush(dapop() - operand);
                }
                break;
            case '*':
                if (dasize() < 2)
                    printf("Less than two items are on the stack\n");
                else
                    dapush(dapop() * dapop());
                break;
            case '/':
                if (dasize() < 2)
                    printf("Less than two items are on the stack\n");
                else {
                    //TODO: Check floating-point library state
                    if (fabs(dapeek() - 0) < DBL_EPSILON)
                        printf("Error: Attempted to divide by zero.\n");
                    else {
                        operand = dapop();
                        dapush(dapop() / operand);
                    }
                }
                break;
            case '%':
                if (dasize() < 2)
                    printf("Less than two items are on the stack\n");
                else {
                    //TODO: Check floating-point library state
                    if (fabs(dapeek() - 0) < DBL_EPSILON)
                        printf("Error: Attempted to divide by zero.\n");
                    else {
                        operand = dapop();
                        dapush(fmod(dapop(), operand));
                    }
                }
                break;
            case 'e':
                dapush(2.71828183L);
                break;
            case 'P':
                if (dasize() > 0)
                    printf("%.8g\n", dapeek());
                break;
            case 'D':
                dadup();
                break;
            case 'S':
                daswap();
                break;
            case 'L':
                dalist();
                break;
            case 'C':
                daclear();
                break;
            case '\n':
                if (lastop != 'P' && lastop != 'L' && dasize() > 0)
                    printf("%.8g\n", dapeek());
                printf("> ");
                break;
            default:
                printf("Error: Unknown command '%s'\n", operator);
        } // switch
        lastop = optype;
    } // while

    return 0;
}
Ejemplo n.º 2
0
Archivo: test.c Proyecto: erc1IC/test
void main()
{
	unsigned char point_area, temp, math_key, result[9] = {0, -1, -1, -1, -1, -1, -1, -1, -1};
	unsigned char flag = 0,i = 0, j = 0;
	float count[2] = {0};
	DS1302=0; //?DS1302??,??????
    SD=0;//?SD???,??????
	cs88=0;		//???????????????????
	led_en=0;
    cs88=0;	//????
	dula=0;
	wela=0;
	while(1){
		temp = keyscan();
		switch(temp)
		{
			case -1: break;
			case '+':
			case '-':
			case '*':
			case '/':
				if(i == 0)
				{
					math_key = temp;
					flag = 0;
					i++;
				}else{
					break;
				}
				j = 0; reset(result);
				break;
				
			case '.':                        
				if(flag == 0 && j != 0)
				{
				result[--j] += 10;
				point_area = j++;
				flag = 1;
				}else{
					result[j] += 10;
					point_area = j++;
					flag = 1;
				}
				break;
				
			case '=':
				reset(result);
				mathfun(count, math_key, result);
				i = 0; j = 0; flag = 0; count[0] = 0; count[1] = 0;
				break;
				
			default:
				if(result[0] == 0 && temp == 0)break;
				if(flag == 0)
				{
					count[i] = count[i]*10 + temp;
				}else{
					count[i] = count[i] + temp/pow(10, j - point_area);
				}
				result[j++] = temp;
		}
		display(result);
	}
	
}