HDU 3347

题目链接 : 

Calculate the expression

 
You may find it’s easy to calculate the expression such as: 
a = 3 
b = 4 
c = 5 
a + b + c = ? 
Isn’t it? 

Input

The first line contains an integer stands for the number of test cases. 

Each test case start with an integer n stands for n expressions will follow for this case. 
Then n – 1 expressions in the format: [variable name][space][=][space][integer] will follow. 
You may suppose the variable name will only contain lowercase letters and the length will not exceed 20, and the integer will between -65536 and 65536. 
The last line will contain the expression you need to work out. 
In the format: [variable name| integer][space][+|-][space][variable name| integer] …= ? 
You may suppose the variable name must have been defined in the n – 1 expression and the integer is also between -65536 and 65536. 

You can get more information from the sample. 

Output

For each case, output the result of the last expression.

Sample Input

3
4
aa = 1
bb = -1
aa = 2
aa + bb + 11 = ?
1
1 + 1 = ?
1
1 + -1 = ?
Sample Output
12
2
0

           题目大意:前n-1行格式是固定的,一定是给变量赋值,最后一行就是变量+常数的加减,题意:从输入开始,1.输入样例数;2.然后输入一组样例中的行数n;3.前n-1行为定义变量(之间使用空格隔开),只需要map存进去就可以了(这里有覆盖的情况,故使用mp["s"] = "***"的方法赋值,因为insert的方法如果里面存在的话,插不进入数值);4.然后就是最后一行输入计算式子(之间使用空格隔开)。

            代码好像有点长。。。不过应该很好懂

            

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<string>
using namespace std;
map<string,int>m;
char str[1000];
char ch1[1000];
char ch[1000];
char sh[1000];
char sh1[1000];
int fuhao[1000];
int shuzi[2000];
void My_strncpy(char a[],char b[],int i)
{
	for(int k = 0;k<=i;k++)
		a[k] = b[k];
	for(int k = i+1;k<=1000;k++)
		a[k] = '\0';
}
int main()
{

    int T,a,j;
    scanf("%d",&T);
	int p =0;
    while(T--)
    {
        scanf("%d\n",&a);
        memset(fuhao,0,sizeof(fuhao));
		memset(shuzi,0,sizeof(shuzi));
		memset(str,'\0',sizeof(ch));
		memset(ch,'\0',sizeof(ch));
		memset(sh,'\0',sizeof(ch));
		memset(ch1,'\0',sizeof(ch));
		memset(sh1,'\0',sizeof(ch));
		m.clear();   // 初始化。。。 
        int k = 1;
        int flag = 0;
        while(k<a)
        {
        	k++; gets(ch);
            int n = strlen(ch);
            for(int i=0;i<n;i++)
            {
                if(ch[i] == '=')
                {
                    j=i;
                    break;
                }
            }
            int ten = 1,ans = 0;
            for(int i=n-1;i>j;i--)  // 求等号后面的值
            {
            	if(ch[i] == '-')
            	{
            		flag = 1;
            		continue;
            	}
                if(ch[i] != ' ')
                {
                    ans += ten * (ch[i]-'0');
                    ten *= 10;
                }
            }
            if(flag)
            {
            	ans *= -1;
            	flag = 0;
            }
            for(int i=0;i<j;i++)
            {
                if(ch[i] == ' ')
                {
        			memset(str,'\0',sizeof(str));
                    strncpy(str,ch,i);
                    m[str] = ans;
                    break; 
                }
            }
            //cout<<str<<" "<<m[str]<<endl;
        }
        
            gets(sh);
            int nsh = strlen(sh);
            int fu = 1;
            for(int i =0;i<nsh;i++)
            {
                if(sh[i] == '+'&&sh[i-1] == ' '&& sh[i+1] == ' ')
                {
                    fuhao[fu++] = 1;
                    sh[i] = ' ';
                }
                if(sh[i] == '-'&&sh[i-1] == ' '&& sh[i+1] == ' ') // 防止 负数的 负号 造成误判 
                {
                    fuhao[fu++] = 2;
                    sh[i] = ' ';
                }
            }
            //for(int i=0;i<nsh;i++)
//            	cout<<sh[i];
            int tot = 0,ix; // 因为第一个数前面没有空格 所以特判第一个未知数
            for(int i = 0;i<nsh;i++)
            {
                if(sh[i] == ' ')// 是变量
                {
                	memset(str,'\0',sizeof(str));
                    strncpy(str,sh,i);
                    if(m[str]!=0)
                    {
                        shuzi[tot++] = m[str];
                    }
                    else{//是数字
                            int kk = 0;
                            if(sh[0] == '-') // 判断第一个数是不是负数 
                            	flag = 1;
                        for(int ten = 1,k =i-1;k>=flag;k--)
                        {
                             kk += (sh[k]-'0') * ten;
                            ten *= 10;
                        }
                        if(flag)
                        {
                        	kk *= -1;
                        	flag = 0;
                        }
                        shuzi[tot++] = kk;
                    }
                    ix = i;
                    break;
                }
            }
            //   bb + cc + dd + ee ...
            for(int k = ix + 1;sh[k] != '='; k++)
            {
                if(sh[k] != ' '&& sh[k] != '-')
                {
                    sh1[p++] = sh[k];
                }
                if(sh[k] == ' ' && p != 0)
                {
                	
                	memset(str,'\0',sizeof(str));
                    strncpy(str,sh1,p);
                   	//cout<<str<<" ";
				   if(m[str] != 0 )//未知数
                    {
                        shuzi[tot++] = m[str];
                    }
                    else{//是数字
                        int kk = 0;
                        //for(int i=0;i<=k;i++)
//                        cout<<sh[i]<<" ";
//                        cout<<endl;
//                        cout<<k<<" "<<p<<" "<<sh[k-p-1]<<endl;
                        if(sh[k-p-1] == '-')
                        	flag = 1;
                        for(int ten = 1,ke = k-1;ke>= k - p;ke--)
                        {
                             kk += (sh[ke]-'0') * ten;
                            ten *= 10;
                        }
                        if(flag){
                        	kk *= -1;
                        	flag = 0;
						}
                        shuzi[tot++] = kk;
                    }
                    p = 0;
                }
            }
//            for(int i=0;i<tot;i++)
//                cout<<shuzi[i]<<" ";
//            cout<<endl;
//            for(int i=1;i<fu;i++)
//                cout<<fuhao[i]<<" ";
//            cout<<endl;
            int cnt = shuzi[0];
            for(int i=1;i <= tot; i++)
            {
            	if(fuhao[i] == 1) //  前面处理过 1 为 +  
            	{
            		cnt += shuzi[i];
            	}
            	else {
            		cnt -= shuzi[i]; // 2 为 - 
            	}
            }
            cout<<cnt<<endl;
    }
    return 0; 
}

T_T !


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值