ccf化学方程式配平检验

样例输入:
11
H2+O2=H2O
2H2+O2=2H2O
H2+Cl2=2NaCl
H2+Cl2=2HCl
CH4+2O2=CO2+2H2O
CaCl2+2AgNO3=Ca(NO3)2+2AgCl
3Ba(OH)2+2H3PO4=6H2O+Ba3(PO4)2
3Ba(OH)2+2H3PO4=Ba3(PO4)2+6H2O
4Zn+10HNO3=4Zn(NO3)2+NH4NO3+3H2O
4Au+8NaCN+2H2O+O2=4Na(Au(CN)2)+4NaOH
Cu+As=Cs+Au

样例输出:
N
Y
N
Y
Y
Y
Y
Y
Y
Y
N

代码:

#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <stack>
#include <ctype.h>
using namespace std;

int Get_Factor(string& s)
{
	int i = 0;
	int result = 0;
	if (!isdigit(s[i]))
	{
		return 1;
	}
	while (isdigit(s[i]))
	{
		int x = s[i] - '0';
		result = result * 10 + x;
		i++;
	}
	s = s.substr(i, s.size() - i);
	return result;
}

//处理的思路是从后往前处理
//最后一个字符有四种情况
//反括号,数字,大写字母,小写字母
//Na(Au(CN)2),Ba(OH)2, NaOH, NaCl 
map<string, int> Cal(string s)
{
	stringstream str(s);
	string temp;
	stack<int> num;
	int count;
	map<string, int> result;
	while (getline(str, temp, '+'))//得到一个化学式储存在temp当中,根据加号分隔开 
	{
		int factor = Get_Factor(temp);//得到首系数,并且可以做到把系数从字符串中剥离
		//接下来可以开始从最后一个字符分五种情况讨论
		count = 1;
		int i = temp.size() - 1;
		for (; i >= 0; i--)
		{
			if (temp[i] == ')') continue;//右括号 
			else if (temp[i] == '(')//左括号 
			{
				if (!num.empty())
				{
					int x = num.top();
					count /= x;
					num.pop();
				}
				else
				{
					continue;
				}
				
			}
			else if (islower(temp[i]))//小写字母 
			{
				
				string str = temp.substr(i - 1, 2);
				
				result[str] += count*factor;
				i--;//把计算过后的大写字母跳过去 
			}
			else if (isupper(temp[i]))
			{
				
				string str1 = temp.substr(i, 1);
				
				result[str1] += count*factor;
			}
			else if (isdigit(temp[i]) && temp[i - 1] == ')')
			{
				
				num.push(temp[i] - '0');
				count *= temp[i] - '0';
			}
			else if (isdigit(temp[i]) && isupper(temp[i - 1]))
			{
				
				string str2 = temp.substr(i - 1, 1);
				
				result[str2] += factor*count * (temp[i] - '0');
				
				i--;
			}
			else if (isdigit(temp[i]) && islower(temp[i - 1]))
			{
				
				string str3 = temp.substr(i - 2, 2);
				result[str3] += factor*count * (temp[i] - '0');
				
				i -= 2;
			}
			else
			{
				cout << "存在情况没有考虑" << endl;
			}
		}
		


	}

	return result;
}

int Check(string s)//利用stringstream将字符串根据“= ”分为两部分,分别利用底层函数得到各个元素的对应个数 
{
	stringstream str(s);
	string left,right;
	getline(str,left,'=');
	getline(str,right);
	map<string,int> left_part=Cal(left);
	map<string,int> right_part=Cal(right);
	if(left_part.size()!=right_part.size())
	{
		return 0;
	}
	else
	{
		for(map<string,int>::iterator it=left_part.begin();it!=left_part.end();it++)
		{
			if(right_part[it->first]!=it->second) return 0;
		}
		return 1;
	}
} 


int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		string temp;
		cin>>temp;
		int x=Check(temp);//配平返回1,没有配平返回0 
		if(x)
		{
			cout<<"Y";
		}
		else
		{
			cout<<"N";
		}
	}
}

用到的少许技巧:
1.利用stringstream和getline来对字符串进行分割
2.利用栈来储存相乘的系数(不包括首系数)
3.利用map来给出元素以及个数的二维表格对应关系

需要注意的:
1.栈可能为空,需要判断,否则有的化学式会出问题
2.首系数不能最后乘(容易错),跟普通系数一起乘就行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值