Esempio n. 1
0
int main (void) {
	int got_type = 0;

	/** first lexem in string */
	while (gettoken() != EOF) {
		/** 3. process error characters */
		if (tokentype == ERR) {
			printf("error[1]: some error symbol inserted\n");
			return -1;
		}

		if (got_type == 0) {
			if (tokentype != NAME || gettokentype() != DATATYPE) {
				printf("error: return type needed at first position\n");
				return -1;
			}
			strcpy(datatype, token);
			got_type = 1;
		}

		/** 1. added check for empty first (datatype) value */
//		if ((got_type == 0) && tokentype != NAME) {
//			printf("error: return type needed at first position\n");
//			return -1;
//		}
//		else {
//                        /** lexem type */
//			strcpy(datatype, token);
//			got_type = 1;
//		}

		out[0] = '\0';
		/** process rest of line */
		dcl();
		if (tokentype != '\n') {
			printf("syntax error in/after '%s'-token\n", token);
			return -1;
		}
		printf("%s: %s %s\n", name, out, datatype);
	}
	return 0;
}
int main()
{
    char temp[MAXLINE];
    char *tokens[MAXNUM];
    int token_num = 0;
    int type, i;
    double op2;

    printf("Reverse Polish Calculator(type 'Quit' to quit)\n");
    while (zgetline(temp, MAXLINE) > 0) {
        if (!strcmp(temp, "Quit \n"))
            break;

        token_num = 0;
        if ((tokens[token_num] = strtok(temp, "\t ")) == NULL) {
            continue;
        }
        else {
            token_num++;
        }
        while ((tokens[token_num] = strtok(NULL, "\t ")) != NULL) {
            token_num++;
        }
        for (i = 0; i < token_num; i++) {
            type = gettokentype(tokens[i]);
            if (type == QUIT) {
                printf("\t%.8g\n", pop());
                clear_stack();
                break;                
            }
            switch (type) {
            case NUMBER:
                push(atof(tokens[i]));
                break;
            case PLUS:
                push(pop()+pop());
                break;
            case MULTIPLE:
                push(pop()*pop());
                break;
            case MINUS:
                op2 = pop();
                push(pop()-op2);
                break;
            case DIVIDE:
                op2 = pop();
                if (op2 != 0.0)
                    push(pop()/op2);
                else
                    printf("error: zero divisor\n");
                break;
            case MOD:
                op2 = pop();
                push((int)pop()%(int)op2);
                break;
            case SIN:
                push(sin(pop()));
                break;
            case COS:
                push(sin(pop()));
                break;
            case EXP:
                push(exp(pop()));
                break;
            case POW:
                op2 = pop();
                push(pow(pop(), op2));
                break;
            default:
                printf("error: unknown command %s\n", tokens[i]);
                break;
            }
        }
        printf("Reverse Polish Calculator(type 'Quit' to quit)\n");
    }

    return 0;
}