Advertisement
anon20016

C reworked

Nov 14th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #define _CRT_SECURE_NO_DEPRECATE
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <map>
  7. #include <stack>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. int main() {
  13. freopen("input.txt", "r", stdin);
  14. freopen("output.txt", "w", stdout);
  15. string s;
  16. cin >> s;
  17. stack<char> c;
  18. for (int i = 0; i < s.size(); i++) {
  19. if (s[i] == ')') {
  20. if (c.size() == 0) {
  21. printf("at position %d : expected ( or END, found )", i + 1);
  22. return 0;
  23. }
  24. if (c.top() == '|') {
  25. c.pop();
  26. c.pop();
  27. }
  28. else {
  29. printf("at position %d : expected ( or |, found (", i + 1);
  30. return 0;
  31. }
  32. }
  33. if (s[i] == '|') {
  34. if (c.size() == 0) {
  35. printf("at position %d : expected (, found |", i + 1);
  36. return 0;
  37. }
  38. if (c.top() == '|') {
  39. printf("at position %d : expected ( or ), found |", i + 1);
  40. return 0;
  41. }
  42. c.push('|');
  43. }
  44. if (s[i] == '(') {
  45. c.push('(');
  46. }
  47. }
  48. if (c.size() == 0) {
  49. cout << "correct, length = " << s.size();
  50. return 0;
  51. }
  52. if (c.size() != 0) {
  53. if (c.top() == '(') {
  54. printf("at position %d: expected ( or |, found END", s.size() + 1);
  55. return 0;
  56. }
  57. else {
  58. printf("at position %d: expected ( or ), found END", s.size() + 1);
  59. return 0;
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement