新文件1
新文件1
h>
// �e�V�ŧi�A�B�z�[����άA��
int expression(); // �B�z�[��B��
int term(); // �B�z�����l�B��
int factor(); // �B�z�Ʀr�M�A�������B��
// Ū��ê�^�U�@�Ӧr��
char next_char() {
while (*input == ' ') input++; // ��L��
return *input;
}
// ���ʨ�U�@�Ӧr��
void eat_char() {
input++;
}
// �N�Ʀr�r��������
int to_digit(char ch) {
return ch - '0';
}
// �ѪR�ê�^�@�Ӿ��
int number() {
int result = 0;
while (is_digit(next_char())) {
result = result * 10 + to_digit(next_char());
eat_char();
}
return result;
}
// �B�z�[��B��
int expression() {
int result = term();
while (next_char() == '+' || next_char() == '-') {
if (next_char() == '+') {
eat_char();
result += term();
} else if (next_char() == '-') {
eat_char();
result -= term();
}
}
return result;
}
// �B�z�����ξl�ƹB��
int term() {
int result = factor();
while (next_char() == '*' || next_char() == '/' || next_char() == '%') {
if (next_char() == '*') {
eat_char();
result *= factor();
} else if (next_char() == '/') {
eat_char();
result /= factor(); // ��ư��k
} else if (next_char() == '%') {
eat_char();
result %= factor();
}
}
return result;
}
// �B�z�Ʀr�άA��������F��
int factor() {
if (next_char() == '(') {
eat_char(); // ��L '('
int result = expression();
eat_char(); // ��L ')'
return result;
} else {
return number();
}
}
int main() {
char line[256];
// ��C�@���J���� EOF
while (fgets(line, sizeof(line), stdin) != NULL) {
input = line; // �N��J�]�������ܼƶi��ѪR
int result = expression(); // �p��B����G
printf("%d\n", result);
}
return 0;
}