Download eval/stack/04d95e22-b36f-4e5d-bccb-fa22baa5e484.cpp from Arkimond9620/AlgoSol-15: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/datasets/Arkimond9620/AlgoSol-15/resolve/main/eval/stack/04d95e22-b36f-4e5d-bccb-fa22baa5e484.cpp
- Command line
-
hf download hf://datasets/Arkimond9620/AlgoSol-15/eval/stack/04d95e22-b36f-4e5d-bccb-fa22baa5e484.cpp
-
curl -L -o 04d95e22-b36f-4e5d-bccb-fa22baa5e484.cpp https://huggingface.co/datasets/Arkimond9620/AlgoSol-15/resolve/main/eval/stack/04d95e22-b36f-4e5d-bccb-fa22baa5e484.cpp
1.45 kB
| using namespace std; | |
| const int NMAX = 100005; | |
| int n; | |
| char s[NMAX]; | |
| int st[NMAX], top; | |
| inline bool isDigit(char ch) | |
| { | |
| return '0' <= ch && ch <= '9'; | |
| } | |
| int main() | |
| { | |
| ifstream fin("evaluare.in"); | |
| ofstream fout("evaluare.out"); | |
| fin >> (s + 1); | |
| n = strlen(s + 1); | |
| s[0] = '('; | |
| s[n + 1] = ')'; | |
| ++n; | |
| for (int i = 0;i <= n;++i) | |
| { | |
| if (s[i] == '+') | |
| continue; | |
| else if (s[i] == '-') | |
| st[++top] = 1000000002; | |
| else if (s[i] == '*') | |
| st[++top] = 1000000003; | |
| else if (s[i] == '/') | |
| st[++top] = 1000000004; | |
| else if (s[i] == '(') | |
| st[++top] = 1000000001; | |
| else if (s[i] == ')') | |
| { | |
| int x = 0; | |
| while (top > 0 && st[top] != 1000000001) | |
| x += st[top--]; | |
| --top; | |
| if (top == 0) | |
| st[++top] = x; | |
| else if (st[top] == 1000000003) | |
| --top, st[top] *= x; | |
| else if (st[top] == 1000000004) | |
| --top, st[top] /= x; | |
| else if (st[top] == 1000000002) | |
| st[top] = -x; | |
| else | |
| st[++top] = x; | |
| } | |
| else | |
| { | |
| int x = 0; | |
| while (i <= n && isDigit(s[i])) | |
| x = x * 10 + (s[i++] - '0'); | |
| --i; | |
| if (top == 0) | |
| st[++top] = x; | |
| else if (st[top] == 1000000003) | |
| --top, st[top] *= x; | |
| else if (st[top] == 1000000004) | |
| --top, st[top] /= x; | |
| else if (st[top] == 1000000002) | |
| st[top] = -x; | |
| else | |
| st[++top] = x; | |
| } | |
| } | |
| fout << st[1] << "\n"; | |
| fin.close(); | |
| fout.close(); | |
| return 0; | |
| } | |