表达式题 : poj 2269 && SGU 182

表达式资料

http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm

学过编译原理的话对这种表达式怎么算还是小儿科的吧

但是,手算会算,不代表代码可以写的好。。。。

这种题没怎么写,调试了n久


SGU 182

/*
表达式计算,我用的是栈版本
 状态压缩枚举每个字母是0或1,判断原来表达式的值是否为true , 为true就输出这个状态
一些有关题意重要的信息请看forum
*/
#include<cstdio>
#include<cstring>
const int N = 3010;
int mp[256];
template<typename T>  
struct Exp
{
    bool error;
    int tot,top;
    T num[N];
    char op[N];
    void init()
    {
        error=false;
        tot=0;
        top=0;
        op[1]='(';
    }
    bool prior(char a,char b)
    {
        if(b=='|' || b=='<' || b=='=' || b=='#')
        {
            return a!='(';
        }
        if(b=='&')  return a=='!' || a=='&';
        return false;
    }
    T calc(char c,T a, T b)
    {
        if(c=='&') return a && b;
        if(c=='|') return a || b;
        if(c=='<') return a == b;
        if(c=='=') return!a || b;
        if(c=='#') return a ^ b;
    }
    T solve(char *s,int state)
    {
        int len = strlen(s);
        s[len++]=')';
        for(int i=0; i<len; i++)
        {
            if(s[i]=='(') op[++top]=s[i];
            else if(s[i]==')')
            {
                while(top>0&&op[top]!='(')
                {
                    if(op[top]=='!')
                    {
                        num[tot]=!num[tot];
                        top--;
                    }
                    else
                    {
                        num[tot-1] = calc(op[top],num[tot-1],num[tot]);
                        tot--;
                        top--;
                    }
                }
                top--;
            }
            else if(s[i] >= 'a' && s[i] <= 'j')
            {
                int x = mp[s[i]];
                num[++tot] =( ((1<<x)&state) > 0 );
            }
            else
            {
                while(top>0&&prior(op[top],s[i]))
                {
                    if(op[top]=='!')
					{
                        num[tot]=!num[tot];
                        top--;
                    }
                    else {
                         num[tot-1] = calc(op[top],num[tot-1],num[tot]);
                         tot--;
                         top--;
                    }
                }
                op[++top]=s[i];
            }
            if(s[i]=='|' || s[i]=='=') i++;
            else if(s[i]=='<') i+=2;
        }
        return num[1];
    }
};
Exp<bool> E;
char s[N];
int main()
{
    while(gets(s))
	{
		memset(mp,-1,sizeof(mp));
		int m = 0 , len = strlen(s);
		char rec[15];
		for(int i = 0; i < len; i++)
		{
			if(s[i] >= 'a' && s[i] <= 'j' && mp[s[i]] < 0)
			{
				rec[m] = s[i];
				mp[s[i]] = m++;
			}
		}
		bool has = false;
		for(int i = 0; i < (1<<m); i++)
		{
			E.init();
			if(E.solve(s,i))
			{
				if(has) printf("||");
				else has = true;
				for(int j = 0; j < m; j++)
				{
					if(j) printf("&");
					if(!(i&(1<<j))) printf("!");
					printf("%c",rec[j]);
				}
			}
		}
		if(!has) printf("a&!a");
		printf("\n");
	}
    return 0;
}


poj 2269

#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<iostream>
using namespace std;
const int N = 500;
template<typename T>  
struct Exp
{
    bool error;
    int tot,top;
    T num[N];
    char op[N];
    void init()
    {
        error=false;
        tot=0;
        top=1;
        op[1]='(';
    }
    bool prior(char a,char b)
    {
      if(b=='+' || b=='-') return a != '(';
	  return a=='*';
    }
    T calc(char c,T a, T b)
    {
	   string ans="";
	   set<char> st;
       if(c=='+')
	   {
		   for(int i = 0; i < a.length(); i++) st.insert(a[i]);
		   for(int i = 0; i < b.length(); i++) st.insert(b[i]);
		   for(set<char>::iterator it = st.begin();it!=st.end();it++)
		   {
			   ans += *it;
		   }
		   return ans;
	   }
	   if(c=='-')
	   {
		   for(int i = 0; i < a.length(); i++) st.insert(a[i]);
		   for(int i = 0; i < b.length(); i++) if(st.find(b[i])!=st.end()) st.erase(b[i]);
		   for(set<char>::iterator it = st.begin();it!=st.end();it++)
		   {
			   ans += *it;
		   }
		   return ans;
	   }
	   if(c=='*')
	   {
		   for(int i = 0; i < a.length(); i++) st.insert(a[i]);
		   for(int i = 0; i < b.length(); i++) if(st.find(b[i])!=st.end()) ans+=b[i];
		   return ans;
	   }
    }
    T solve(char *s)
    {
        int len = strlen(s);
        s[len++]=')';
        for(int i=0; i<len; i++)
        {
            if(s[i]=='(') op[++top]=s[i];
            else if(s[i]==')')
            {
                while(top>0&&op[top]!='(')
                {
                        num[tot-1] = calc(op[top],num[tot-1],num[tot]);
                        tot--;
                        top--;
                }
                top--;
            }
            else if(s[i] == '{')
            {
				 i++;
				 if(s[i]== '}') 
				 {
					 num[++tot]="";
					 continue;
				 }
				 string tmp = "";
                 while(i<len && s[i] >= 'A' && s[i] <= 'Z')
				 {
				//	 printf("i=%d\n",i);
					 tmp += s[i];
					 i++;
				 }
				 
				 num[++tot] = tmp;
				// printf("tot=%d\n",tot);
				// cout<<"dd"<<tmp<<endl;
            }
            else
            {
                while(top>0&&prior(op[top],s[i]))
                {
                         num[tot-1] = calc(op[top],num[tot-1],num[tot]);
                         tot--;
                         top--;
                }
                op[++top]=s[i];
            }
        }
        return num[tot];
    }
};
Exp<string> E;
char s[N];
int main()
{
    while(gets(s))
	{
		E.init();
		cout<<"{"<<E.solve(s)<<"}"<<endl;
	}
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值