HDU 3347 Calculate the expression【模拟】

1 篇文章 0 订阅

题目来戳呀

Problem Description

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
题意:输出一行算式的结果,可能有数字可能有字母,你要把字母代表的值存起来后来才能运算。
想法:我还没清楚什么是模拟,题解都说是模拟(心累)
看起来很简单(好像也没有……),写起来不容易。
这份代码转自他人(找不到渊博了 qaq),很简短但是很难理解,啃了很久,因为里面用到了一些不常用的函数。

//代码重点:string的各种应用!
#include <iostream>
#include <sstream>
#include <map>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cctype>
using namespace std;
map<string, int> mm;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        string a, b;
        int c;
        mm.clear();
        for(int i = 1; i < n; i ++)
        {
            cin >> a >> b >> c;  //x=3(a,b,c)
            mm[a] = c;
        }
        string str;  //就是最后的那一行!
        int flag = 1;
        getchar(); // 接受回车符
        getline(cin, str);
        stringstream ss(str); // 定义输入流
        string str1;  //str1就是上面输入流读取的单字!(我猜)
        int sum = 0;
        while( ss >> str1)
        {
            if(str1 == "-")
            {
                flag = -1;
            }
            if(str1 == "+")
            {
                flag = 1;
            }
            if(str1 == "=")
            {
                break;
            }
            if(isalpha(str1[0])) // 判断是否是字母
            {
                if(mm.find(str1) != mm.end())
                {
                    sum += mm[str1] * flag;
                }//把这里实践一下啊!实践完就知道if(str1 == "+")为什么不能去掉了
            }
            else
            {//因为还是string类型的数字啊摔!要转化成int才能运算!
                int temp;
                temp = atoi(str1.c_str());// 转换成char 在转化成int
                    sum += temp * flag;
            }
        }
        cout << sum << endl;
    }
}

恩,下面是壮神的代码,一个酷爱用switch的boy↓
他用struct代替了map,其实应该他的更详细会更好理解一点,但是我已经懂了楼上就不想再用这么麻烦的~(≧▽≦)/~啦啦啦

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
struct S
{
    char name[25];
    int num;
};
int main()
{
    int N;
    while(scanf("%d",&N)!=EOF)
    {
        for(int j=0; j<N; ++j)
        {
            S s[1005];
            memset(s,0,sizeof(s));
            int n,x=0;
            scanf("%d",&n);
            for(int i=1; i<n; ++i)
            {
                char a1[25]="0";
                int a2,j;
                scanf("%s = %d",a1,&a2);
                a1[strlen(a1)]=0;
                for(j=0; j<x; ++j)
                {
                    if(strcmp(a1,s[j].name)==0)
                    {
                        s[j].num=a2;
                        break;
                    }
                }
                if(j==x)
                {
                    strcpy(s[x].name,a1);
                    s[x].num=a2;
                    x++;
                }
            }
            getchar();
            char a[1005],b[100];
            gets(a);
            int len=strlen(a);
            int x1=0,flag=0,ans=0;
            for(int i=0; i<len; ++i)
            {
                if(a[i]=='=')
                {
                    cout<<ans<<endl;
                    break;
                }
                else if(a[i]!=' ')
                {
                    b[x1++]=a[i];
                }
                else if(a[i]==' ')
                {
                    b[x1]=0;
                    if(b[0]=='+'&&strlen(b)==1) flag=0;
                    else if(b[0]=='-'&&strlen(b)==1) flag=1;
                    else if(b[0]>='a'&&b[0]<='z')
                    {
                        for(int j=0; j<x; ++j)
                        {
                            if(strcmp(b,s[j].name)==0)
                            {
                                switch(flag)
                                {
                                    case 0:ans+=s[j].num;break;
                                    case 1:ans-=s[j].num;break;
                                }
                                break;
                            }
                        }
                    }
                    else
                    {
                        int a1=atoi(b);
                        switch(flag)
                        {
                            case 0:ans+=a1;break;
                            case 1:ans-=a1;break;
                        }
                    }
                    x1=0;
                }
            }
        }
    }
    return 0;
}

ps:啃到最后忍不住去问了小姐姐,小姐姐真好啊(*  ̄3)(ε ̄ *)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值