下笔春蚕食叶声。

表达式求值+-*/()

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls(x) ((x)<<1)
#define rs(x) ((x)<<1|1)
#define mkp make_pair
#define fi first
#define se second
#define PII pair<int, int>
char ch;
float factor();
float term();
float expression();
float factor() {
	float num = 0;
	if(ch == '(') {
		ch = getchar();
		num = expression();
		ch = getchar();
		return num;
	} else {
		while(ch >= '0' && ch <= '9'){
			num = num * 10 + ch - '0';
			ch = getchar();
		}
		return num;
	}
}
float term() {
	float x, y;
	x = factor();
	if(ch == '*' || ch == '/') {
		char op = ch;
		ch = getchar();
		y = term();
		if(op == '*') return x * y;
		else if(op == '/') return x / y;
	} else return x;
}
float expression() {
	float x, y;
	x = term();
	if(ch == '+' || ch == '-') {
		char op = ch;
		ch = getchar();
		y = expression();
		if(op == '+') return x + y;
		else return x - y;
	} else return x;
}                                                        
int main() {
	ch = getchar();
	printf("%f\n", expression()); 
    return 0;
}
/*
(3*5)+12
*/


posted @ 2024-11-28 11:38  ACwisher  阅读(3)  评论(0编辑  收藏  举报