Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <windows.h>
  6. #include <cstdio>
  7. using namespace std;
  8.  
  9. void logic(string arg, vector <string> &stack, int* par){
  10. cin >> arg;
  11. if (arg.find("(") != string::npos){
  12. *par += arg.rfind("(") + 1;
  13. }
  14. else if(arg.find(")") != string::npos){
  15. *par -= (arg.length() - arg.find(")"));
  16. }
  17. if (arg.find("TRUE") != string::npos){
  18. stack.push_back("TRUE");
  19. }
  20. else if (arg.find("FALSE") != string::npos){
  21. stack.push_back("FALSE");
  22. }
  23. else if (arg.find("NOT") != string::npos){
  24. logic(arg, stack, par);
  25. if (stack.back() == "TRUE"){
  26. stack.pop_back();
  27. stack.push_back("FALSE");
  28. } else{
  29. stack.pop_back();
  30. stack.push_back("TRUE");
  31. }
  32. }
  33. else if(arg == "AND"){
  34. string stackBefore = stack.back();
  35. logic(arg, stack, par);
  36. if ((stackBefore == "FALSE") || (stack.back() == "FALSE")){
  37. stack.pop_back();
  38. stack.pop_back();
  39. stack.push_back("FALSE");
  40. }
  41. else if ((stackBefore == "TRUE") && (stack.back() == "TRUE")){
  42. stack.pop_back();
  43. stack.pop_back();
  44. stack.push_back("TRUE");
  45. }
  46. }
  47. else if(arg == "OR"){
  48. string stackBefore = stack.back();
  49. logic(arg, stack, par);
  50. if ((stackBefore == "TRUE") || (stack.back() == "TRUE")){
  51. stack.pop_back();
  52. stack.pop_back();
  53. stack.push_back("TRUE");
  54. }
  55. else if ((stackBefore == "FALSE") && (stack.back() == "FALSE")){
  56. stack.pop_back();
  57. stack.pop_back();
  58. stack.push_back("FALSE");
  59. }
  60. }
  61. }
  62.  
  63. string logicValue(vector <string> &stack, int* par){
  64. logic("", stack, par);
  65.  
  66. if (*par){
  67. return logicValue(stack, par);
  68. }
  69. else{
  70. return stack.back();
  71. }
  72. }
  73.  
  74. int main(){
  75. SetConsoleOutputCP(CP_UTF8);
  76. ifstream file;
  77. vector <string> stack;
  78. int par = 0;
  79. int method;
  80. char locate[255];
  81. cout << "Укажите, каким образом будет считано простое логическое выражение (0 - из файла, 1 - из консоли):" << endl;
  82. cin >> method;
  83. if (method){
  84. cout << "Введите логическое выражение:" << endl;
  85. cout << logicValue(stack, &par) << endl;
  86. } else{
  87. cout << "Введите расположение файла:" << endl;
  88. cin >> locate;
  89. freopen(locate, "r", stdin);
  90. cout << logicValue(stack, &par) << endl;
  91. }
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement