0% found this document useful (0 votes)
4 views2 pages

新文件1

Uploaded by

poo94121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

新文件1

Uploaded by

poo94121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

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��

char *input; // �����ܼƥΨӫ�V��J�r��

// Ū��ê�^�U�@�Ӧr��
char next_char() {
while (*input == ' ') input++; // ��L��
return *input;
}

// ���ʨ�U�@�Ӧr��
void eat_char() {
input++;
}

// �ˬd�O�_���Ʀr�r�� ('0' ~ '9')


int is_digit(char ch) {
return ch >= '0' && ch <= '9';
}

// �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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy