记录编号 18614 评测结果 AAAAAAAAAA
题目名称 表达式转换 最终得分 100
用户昵称 Gravatarwangwangdog 是否通过 通过
代码语言 C++ 运行时间 0.004 s
提交时间 2010-09-16 20:55:28 内存使用 0.25 MiB
显示代码纯文本
#include<stdio.h>
#include<string.h>
int i,t,k,comp[9][9],l,data[100],j;
char a[100],str[100],b[100];
FILE *fin,*fout;
int num(char chr)
{
	if(chr=='+')return 1;
	if(chr=='-')return 2;
	if(chr=='*')return 3;
	if(chr=='/')return 4;
	if(chr=='^')return 5;
	if(chr=='#')return 6;
	if(chr=='+')return 7;
}
void panduan()
{
	if(str[i]==')')
	{
	    while(b[k]!='(')
	    {
		    t++;
			a[t]=b[k];
			k--;
		}
		k--;
	}
	else
	{
		if(str[i]!='(')
		{
			int aa=num(str[i]),bb=num(b[k]);
			while(comp[aa][bb]==0&&b[k]!='('&&k>0)
			{
				t++;
				a[t]=b[k];
				k--;
				bb=num(b[k]);
			}
			k++;
			b[k]=str[i];
		}
		if(str[i]=='('){k++;b[k]=str[i];}
	}
}
int main()
{
	for(i=1;i<=6;i++)
		comp[1][i]=0;
	comp[1][6]=1;
	for(i=1;i<=6;i++)
		comp[2][i]=0;
	comp[2][6]=1;
	for(i=1;i<=6;i++)
	{comp[3][i]=0;comp[4][i]=0;comp[5][i]=1;comp[6][i]=0;}
	comp[3][1]=1;
	comp[3][2]=1;
	comp[3][6]=1;
	comp[4][1]=1;
	comp[4][2]=1;
	comp[4][6]=1;
	comp[5][5]=0;
	fin=fopen("express.in","rb");
	fout=fopen("express.out","wb");
	fscanf(fin,"%s",&str[1]);
	l=strlen(&str[1]);
	l++;
	str[l]='#';
	t=0;
	k=0;
	for(i=1;i<=l;i++)
	{
		int kind=str[i];
		if(kind-'0'>=0&&kind-'0'<=9)
		{
			t++;
			a[t]=str[i];
		}
		else
		{
			panduan();
		}
	}
	for(i=1;i<=t;i++)
		fprintf(fout,"%c ",a[i]);
	fprintf(fout,"\r\n");
	int ll=t;
	for(i=1;i<=ll;i++)
	{
		if(a[i]-'0'>=0&&a[i]-'0'<=9)
		{
			data[i]=a[i]-'0';
			a[i]='>';
		}
	}
	while(ll!=1)
	{
		int y=1;
		while(a[y]=='>'||a[y]=='<')
		{
			y++;
		}
		int x1=y-1;
		while(a[x1]=='<')
		{
			x1--;
		}
		int x2=x1-1;
		while(a[x2]=='<')
			x2--;
		if(a[y]=='+')
		{
			data[y]=data[x1]+data[x2];
			data[x1]=0;
			data[x2]=0;
			a[y]='>';
			a[x1]='<';
			a[x2]='<';
		}
		if(a[y]=='-')
			{
			data[y]=data[x2]-data[x1];
			a[y]='>';data[x1]=0;
			data[x2]=0;
			a[x1]='<';
			a[x2]='<';
		}
		if(a[y]=='*')
			{
			int a1=data[x1],a2=data[x2];
			data[y]=a1*a2;
			a[y]='>';data[x1]=0;
			data[x2]=0;
			a[x1]='<';
			a[x2]='<';
		}
		if(a[y]=='/')
			{
			data[y]=data[x2]/data[x1];
			a[y]='>';
			a[x1]='<';data[x1]=0;
			data[x2]=0;
			a[x2]='<';
		}
		if(a[y]=='^')
		{
			data[y]=1;
			for(j=1;j<=data[x1];j++)
				data[y]=data[y]*data[x2];
			data[x1]=0;
			data[x2]=0;
			a[x1]='<';
			a[x2]='<';
			a[y]='>';
		}
		ll=ll-2;
		for(i=1;i<=t;i++)
			if(a[i]!='<')
			{
				if(a[i]=='>')fprintf(fout,"%d ",data[i]);
				else fprintf(fout,"%c ",a[i]);
			}
		fprintf(fout,"\r\n");
	}
	fclose(fin);
	fclose(fout);
	return 0;
}