[luoguP1022] 计算器的改良(模拟)
Posted 蒟蒻zht的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[luoguP1022] 计算器的改良(模拟)相关的知识,希望对你有一定的参考价值。
超级大模拟。。
代码
#include <cstdio> #include <cstring> #include <iostream> #define isword(x) ((x) >= ‘a‘ && (x) <= ‘z‘) int n, a1, b1, a2, b2, f, x; char s[1001], c; int main() { int i; scanf("%s", s + 1); n = strlen(s + 1); i = f = 1; while(i <= n) { if(!isdigit(s[i])) { if(isword(s[i])) { c = s[i]; i++; continue; } if(isdigit(s[i - 1]) && i != 1) b1 += x * f; if(!isdigit(s[i - 1]) && i != 1) { if(!isdigit(s[i - 2])) a1 += f; else a1 += x * f; } if(s[i] == ‘+‘) f = 1; if(s[i] == ‘-‘) f = -1; if(s[i] == ‘=‘) break; i++; } x = 0; while(isdigit(s[i])) x = (x << 1) + (x << 3) + s[i++] - ‘0‘; } i++; f = 1; while(i <= n) { if(!isdigit(s[i])) { if(isword(s[i])) { c = s[i]; i++; continue; } if(isdigit(s[i - 1]) && s[i - 1] != ‘=‘) b2 += x * f; if(!isdigit(s[i - 1]) && s[i - 1] != ‘=‘) { if(!isdigit(s[i - 2])) a2 += f; else a2 += x * f; } if(s[i] == ‘+‘) f = 1; if(s[i] == ‘-‘) f = -1; i++; } x = 0; while(isdigit(s[i])) x = (x << 1) + (x << 3) + s[i++] - ‘0‘; } if(isdigit(s[i - 1])) b2 += x * f; if(!isdigit(s[i - 1])) { if(!isdigit(s[i - 2])) a2 += f; else a2 += x * f; } a1 -= a2; b2 -= b1; printf("%c=", c); if(b2 == 0) puts("0.000"); else printf("%.3lf\n", double(b2) / double(a1)); return 0; }
以上是关于[luoguP1022] 计算器的改良(模拟)的主要内容,如果未能解决你的问题,请参考以下文章