Logo C++ Online Judge

C++

TA:
统计

Use an AI coding agent with a checkout of TinyExpr, a C library that evaluates expressions such as sqrt(9) + 2^3 (^ means exponentiation here). Your agent may run locally or in the cloud. Let it help you obtain the project, inspect it, and run a small check.

Answer briefly and point to the relevant code. You may rely on your agent throughout; no setup report or transcript is required.

For question 2, consider a program that registers its own zero-argument function, read_measurement(), with TinyExpr. Each call returns the latest measurement, which can change over time. The program compiles the expression once, then repeatedly evaluates that same compiled expression. Each evaluation should return the current measurement, but with the code below it keeps returning an old value even after the measurement changes. The relevant calls are:

te_variable functions[] = {
    {"read_measurement", read_measurement,
     TE_FUNCTION0 | TE_FLAG_PURE, NULL}
};
int error;
te_expr *expression = te_compile("read_measurement()", functions, 1, &error);
// Compilation succeeds. Later, whenever a result is needed:
double result = te_eval(expression);

The entry in functions connects the expression name to the C function; its third field is a set of flags. te_compile prepares an expression for reuse; te_eval evaluates the prepared expression. Storage remains valid throughout. Your agent can explain unfamiliar API syntax.

提交

Written answers receive AI rubric assessment.

Up to 3 submission attempts per student. Sign in to see your remaining attempts.

Each submission includes all questions. Review your answers before submitting.

Short explanations are welcome. You may leave answers blank; unanswered questions receive zero credit.

1. Which code decides how 2^3^2 is grouped? What rule does it use, and how could that rule be changed? (40 points)
2. What is the smallest change that makes each evaluation return the current measurement, while still compiling the expression only once? Trace the relevant path through TinyExpr to explain the bug and your fix, and briefly state what your check showed. (60 points)
Drafts are saved in this browser every 30 seconds.