2021-04-07

201912-3 化学方程式

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <string>
using namespace std;

void analyse(string s, vector<string>& left, vector<string>& right) {
	int l = s.length();
	int start = 0, end = 0, flag = 0;
	while (end <= l) {
		if (s[end] == '+') {
			if (flag == 0) {
				left.push_back(s.substr(start, end - start));
				start = end + 1;
			}
			else if (flag == 1) {
				right.push_back(s.substr(start, end - start));
				start = end + 1;
			}
		}
		else if (s[end] == '=') {
			left.push_back(s.substr(start, end - start));
			start = end + 1;
			flag = 1;

		}
		else if (end == l) {
			right.push_back(s.substr(start, end - start));
		}
		end++;
	}
}



void solve(string s, vector<pair<string, int> >& va) {
	map<string, int> m;
	stack<pair <string, int> > st;
	int coef = 1;  //总系数
	int start = 0, end = 0, l = s.length();
	if (s[0] >= '0' && s[0] <= '9') {
		while (s[end] >= '0' && s[end] <= '9') {
			end++;
		}
		coef = stoi(s.substr(start, end - start));
	}
	start = end;
	while (end < l) {
		if (s[end] >= 'a' && s[end] <= 'z') {
			end++;
			if (end == l) {
				st.push(pair<string, int>(s.substr(start, end - start), 1));
			}
		}
		else if (s[end] >= '0' && s[end] <= '9') {
			string temp = s.substr(start, end - start);
			start = end;
			while (s[end] >= '0' && s[end] <= '9') {
				end++;
				if (end == l) break;
			}
			int subcoef = stoi(s.substr(start, end - start)); //Ba2   元素系数
			st.push(pair<string, int>(temp, subcoef));
			start = end;
		}
		else if (s[end] >= 'A' && s[end] <= 'Z' && end > start) {
			st.push(pair<string, int>(s.substr(start, end - start), 1));
			start = end;
		}
		else if (s[end] == '(') {
			if (end > 0 && (s[end - 1] > '9' || s[end - 1] < '0') && s[end - 1] != '(' && s[end - 1] != ')') {
				st.push(pair<string, int>(s.substr(start, end - start), 1));
			}
			st.push(pair<string, int>("(", 1));
			end++;
			start = end;
		}
		else if (s[end] == ')') {
			if (end > 0 && (s[end - 1] > '9' || s[end - 1] < '0') && s[end - 1] != '(' && s[end - 1] != ')') {
				st.push(pair<string, int>(s.substr(start, end - start), 1));
			}
			end++;
			start = end;
			int subcoef = 1;
			if (end != l && s[end] >= '0' && s[end] <= '9') {
				while (end != l && s[end] >= '0' && s[end] <= '9') {
					end++;
				}
				subcoef = stoi(s.substr(start, end - start));
				start = end;
			}
			vector<pair<string, int> > v;
			while (st.top().first != "(") {
				pair<string, int> temp = st.top();
				st.pop();
				temp.second *= subcoef;
				v.push_back(temp);
			}
			st.pop();
			for (int i = 0; i < v.size(); i++) {
				st.push(v[i]);
			}
		}
		else {
			end++;
			if (end == l) {
				st.push(pair<string, int>(s.substr(start, end - start), 1));
			}
		}
	}
	
	//vector<pair<string, int> > va;
	while (!st.empty()) {
		pair<string, int> temp = st.top();
		st.pop();
		temp.second *= coef;
		va.push_back(temp);
	}
	
	cout << s << ":" << endl;
	for (int i = 0; i < va.size(); i++) {
		cout << va[i].first << " " << va[i].second << endl;
	}
	
}


int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		vector<string> left, right;
		map<string, int> l, r;
		string s;
		cin >> s;
		analyse(s, left, right);
		cout << "leftside:" << endl;
		for (int i = 0; i < left.size(); i++) {
			vector<pair<string, int> > va;
			solve(left[i], va);
			for (int j = 0; j < va.size(); j++) {
				if (l.count(va[j].first)) l[va[j].first] += va[j].second;
				else l.insert(va[j]);
			}
		}
		cout << endl << endl << endl << "rightside:" << endl;
		for (int i = 0; i < right.size(); i++) {
			vector<pair<string, int> > va;
			solve(right[i], va);
			for (int j = 0; j < va.size(); j++) {
				if (r.count(va[j].first)) r[va[j].first] += va[j].second;
				else r.insert(va[j]);
			}
		} 

		bool isEqual = true;
		for (map<string, int>::iterator p = l.begin(); p != l.end(); p++) {
			if (!r.count(p->first)) {
				//不存在
				isEqual = false;
				break;
			}
			else {
				if (r[p->first] != p->second) {
					isEqual = false;
					break;
				}
			}
			if (isEqual == false) break;
		}

		if (isEqual) {
			for (map<string, int>::iterator p = r.begin(); p != r.end(); p++) {
				if (!l.count(p->first)) {
					//不存在
					isEqual = false;
					break;
				}
				else {
					if (l[p->first] != p->second) {
						isEqual = false;
						break;
					}
				}
				if (isEqual == false) break;
			}
		}
		if (isEqual) cout << 'Y' << endl;
		else cout << 'N' << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值