HDU OJ 1237 本地通过在线WA不知原因已经考虑大数 测试了非常多的数据

#include
#include
#include
#include
#include
#include

using namespace std;

long long int ds(const string& s)
{
long long int a=0;
for (int i = 0; i < s.length(); i++)
{
a += ((char)s[i] - ‘0’) * pow(10,(s.length() -1- i));
}
return a;
}

void Fix(string& s)
{
for (int i = 0; i < s.length(); i++)
{
if (s[i] == ’ ')
{
s.erase(i, 1);
i = i - 1;
}
}
if (s[0] == ‘-’ || s[0] == ‘+’)
{
s.insert(0, 1, ‘0’);
}
}

int compare(char a, char b)
{
if ((a == ‘’ || a == ‘/’) && (b == ‘+’ || b == ‘-’)) return 1;
else if (a == ‘-’ && (b == ‘-’ || b == ‘+’)) return 1;
else if (a == ‘/’ && (b == '
’ || b == ‘/’)) return 1;
else return 0;
}

long double cal(stack & ods, stack & ops)
{
long double a, b, c;
char temp = ods.top(); ods.pop();
a = ops.top(); ops.pop(); b = ops.top(); ops.pop();
switch (temp)
{
case ‘+’:
c = b + a;
break;
case ‘-’:
c = b - a;
break;
case ‘*’:
c = b * a;
break;
case ‘/’:
c = b / a;
break;
}
return c;
}

void res(stack & ops, stack & ods, vector & opsv, vector & odsv)
{
int opp = 0, odp = 0;
int flag = 1;
while (!opsv.empty())
{
part1:if (flag == 1)
{
ops.push(opsv[0]);
opsv.erase(opsv.begin());
}
else
{
if (ods.empty())
{
ods.push(odsv[0]);
odsv.erase(odsv.begin());
}
else if (compare(ods.top(), odsv[0]))
{
ops.push(cal(ods, ops));
goto part1;
}
else
{
ods.push(odsv[0]);
odsv.erase(odsv.begin());
}
}
flag = -flag;
}
while (!ods.empty())
{
ops.push(cal(ods, ops));
}
//cout << ops.top() << endl;
//if (ops.top() == 0) cout << 0 << endl;
//else
printf("%.2llf\n", ops.top());
ops.pop();
}

void store(vector & opsv, vector & odsv, string & s)
{
stringstream temp; long long int tempo;
for (int i = 0; i < s.length(); i++)
{
if (s[i]<‘0’ || s[i]>‘9’)
{
odsv.push_back(s[i]);
// temp << s.substr(0, i);
//temp >> tempo;
opsv.push_back(ds(s.substr(0,i)));
s.erase(0, i + 1);
i = -1;
// temp.str(""); temp.clear();
}
}
// temp << s;
// temp >> tempo;
opsv.push_back(ds(s));
// temp.str(""); temp.clear();
}

int main()
{
string s; stack ops; stack ods;
vectoropsv; vectorodsv;
char c;
while (getline(cin, s))
{

    //if (s == "0" && (c = getchar() == '\n')) break;
    Fix(s);
    if (s == "0") break;
    //if (s == "") continue;
    store(opsv, odsv, s);
    res(ops, ods, opsv, odsv);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值