听取WA声一片,一个零蛋(悬关)
查看原帖
听取WA声一片,一个零蛋(悬关)
980130
cleverclever楼主2023/7/24 13:50

请教大佬,本蒟蒻的代码错在哪里?

#include <bits/stdc++.h>

using namespace std;

string s;
char x;
double zhi = 0.0;
int deng = 1, tot, xi, fu = 1;

int main()
{
    getline(cin, s);

    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] == '-')
        {
            zhi -= tot * deng * fu;
            tot = 0;
            fu = -1;
        }
        else if (s[i] == '+')
            {
                zhi -= tot * deng * fu;
                tot = 0;
                fu = 1;
            }
            else if (s[i] == '=')
                {
                    zhi -= tot * deng * fu;
                    deng = -1;
                    tot = 0;
                    fu = 1;
                }
                else if (s[i] >= 'a' && s[i] <= 'z')
                    {
                        xi = xi + tot * deng * fu;
                        if (xi == 0)
                            xi = 1;
                        x = s[i];
                        tot = 0;
                    }
                    else
                        tot = tot * 10 + (s[i] - '0');
    }

    zhi += tot * fu;

    printf("%c=%.3lf\n", x, zhi / xi);

    return 0;
}
2023/7/24 13:50
加载中...