Problem D: ZZ的计算器

Problem D: ZZ的计算器

前两个是我的队友的,最后一个是一位学长的,看了ztw的我终于知道什么叫做逻辑性,本来我跟他说这题用模拟人脑的方式做,而且将思路大致讲了一遍,但是我自己敲代码时发现思维很混乱,但是他写出来。。。。我都不知道说什么了,呵呵,不过还好他是我的队友,而且是我们小组的队友,跟他待一起组队,我相信我的思维逻辑一定会越来越好的,一起加油吧,骚年

ztw

#include<stdio.h>
#include<string.h>
#define LL long long

int main()
{
	LL d[10000] , temp , count;
	LL i , j;
	char ch[200];
	while(gets(ch))
	{
		count=temp=0;
		for(j=0 ; j<strlen(ch) ; j++)
		{
			if(ch[j]>='0'&&ch[j]<='9')
				temp = temp*10+ch[j]-'0';
			else
				break;
		}
		d[count++]=temp;
		for(i=j ; i<strlen(ch) ; i++)
		{
			temp=0;
			for(j=i+1 ; ch[j]>='0'&&ch[j]<='9' ; j++)
				temp = temp*10+ch[j]-'0';
			if(ch[i]=='*')
				d[count-1] *= temp;
			else if(ch[i]=='/')
				d[count-1] /= temp;
			else 
				if(ch[i]=='+')
					d[count++] = temp;
				else
					d[count++] = (-1)*temp;
				i=j-1;
			}
			for(i=1 ; i<count ; i++)
				d[0]+=d[i];
			printf("%lld\n",d[0]);
		}
	return 0;
} 


darkdream

// File Name: 表达式树.c
// Author: darkdream
// Created Time: 2013年04月18日 星期四 19时37分36秒

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
int num[50];
const int maxn = 1000;
int lch[maxn] , rch[maxn];
char op[maxn];
int nc = 0;
int ok = 1;
int build_tree(char *s,int x,int y)
{   
	//printf("%d****%d\n",x,y);
    int i , c1 =-1, c2 = -1, p =0;
	int u ;
	if(y - x == 1)
	{
	  u = ++nc;
	  lch[u] = rch[u] = 0 ;
	  op[u] = s[x];
	  return u ;
	}
	for(int i = x; i < y ; i++)
	{ 
	   switch(s[i])
	   {
	     case '(' :p++;break;
		 case ')' :p--;break;
		 case '+':case '-':if(!p) c1 = i ; break;
		 case '*':case '/':if(!p) c2 = i ;break;
	   }
	}
	if(c1 < 0) c1 = c2;
	if(c1 < 0) return build_tree(s,x+1,y-1);
	u = ++nc;
	lch[u] = build_tree(s,x,c1);
	rch[u] = build_tree(s,c1+1,y);
	op[u] = s[c1];
	return u ;
}
long long  find(int i)
{  
	if(lch[i] == 0)
	{
		return num[op[i]-'a'];
	}
   if (op[i] == '+')
	   return find(lch[i])+find(rch[i]);
   if (op[i] == '-')
	   return find(lch[i])-find(rch[i]);
   if (op[i] == '*')
	   return find(lch[i])*find(rch[i]);
   if (op[i] == '/')
   {
	   if(find(rch[i]) == 0)
	   {
		ok = 0;
	    return find(lch[i])/1;

	   }
	   else
		  return find(lch[i])/find(rch[i]);
   }
}
int main(){

   //freopen("/home/plac/problem/input.txt","r",stdin);
   //freopen("/home/plac/problem/output.txt","w",stdout);
   char s[1000];
   while(scanf("%s",s) != EOF)
   {
     memset(lch,0,sizeof(lch));
	 memset(rch,0,sizeof(lch));
	 memset(op,0,sizeof(op));
	 memset(num,0,sizeof(num));
     nc = 0; 
	 ok = 1;
	 char c = 'a';
	  char str[56] ={0};
	  int k = -1;
	  int temp = 0;
      for(int i = 0;i < strlen(s);i ++)
	  {
	     if(s[i] <= '9' && s[i] >= '0')
		 {
		   temp = temp*10 +(s[i]-'0');
		 }
		 else
		 {
		    str[++k] = c;
			num[c-'a'] = temp;
			c++;
			str[++k] = s[i];
			temp = 0;
		 }
		 if(i == strlen(s)-1)
		 {
		   str[++k] = c;
		   num[c-'a'] = temp;
		
		 }
	  }
	 
	 
	 build_tree(str,0,strlen(str));
	 int ans = find(1);
     if(!ok)
		 printf("impossible\n");
	 else
       printf("%lld\n",find(1));
   }
return 0 ;
}




kuangbing

#include <iostream>
#include <string.h>
#include <algorithm>
#include <map>
#include <stdio.h>
using namespace std;

char str[100];
int n;
int cnt;
long long getnum()
{
	long long ret=0;
	while(cnt<n && str[cnt]>='0'&&str[cnt]<='9')
	{
		ret*=10;
		ret+=str[cnt]-'0';
		cnt++;
	}	
	return ret;
}
map<char,int>mp;
long long calc()
{
	cnt=0;
	n=strlen(str);
	char op1,op2;
	mp['+']=1;
	mp['-']=1;
	mp['*']=2;
	mp['/']=2;
	long long ans=getnum();
	if(cnt>=n)return ans;
	long long t1,t2;
	op1=str[cnt++];
	t1=getnum();

//cout<<ans<<" "<<t1<<endl;
//cout<<cnt<<endl;

	while(cnt<n)
	{
	op2=str[cnt++];
	if(mp[op2]>mp[op1])
	{
		t2=getnum();
//cout<<t2<<endl;
		if(op2=='*')
			t1*=t2;
		else 
			if(op2=='/')
			{
				if(t2==0)
					return -100000000;
				t1/=t2;
			}
	}
	else
	{
			t2=getnum();
			if(op1=='+')
				ans+=t1;
			else
				if(op1=='-')
					ans-=t1;
			else 
				if(op1=='*')
					ans*=t1;
			else
			{
				if(t1==0)
					return -100000000;
				ans/=t1;
			}
			t1=t2;
			op1=op2;
		}
//cout<<cnt<<endl;
//cout<<"*******"<<endl;
	}
//cout<<ans<<" "<<op1<<" "<<t1<<endl;

	if(op1=='+')
		ans+=t1;
	else 
		if(op1=='-')ans-=t1;
	else
		if(op1=='*')ans*=t1;
	else
	{
		if(t1==0)
			return -100000000;
		ans/=t1;
	}
	return ans;

}


int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
	while(scanf("%s",&str)==1)
	{
		long long ans=calc();
		if(ans==-100000000)
			printf("impossible\n");
		else 
			cout<<ans<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值