Advertisement
welwelar

lr2.cpp

Oct 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.28 KB | None | 0 0
  1. #include <sstream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. typedef char base;
  7. struct s_expr;
  8. struct  two_ptr
  9. {
  10.         s_expr *hd;
  11.         s_expr *tl;
  12. };
  13.  
  14. struct s_expr
  15. {
  16.         bool tag;
  17.         union
  18.         {
  19.                 base atom;
  20.                 two_ptr pair;
  21.         }node;
  22. };
  23.  
  24. typedef s_expr *lisp;
  25.  
  26. lisp head (const lisp s);
  27. lisp tail (const lisp s);
  28. lisp cons (const lisp h, const lisp t);
  29. lisp make_atom (const base x);
  30. bool isAtom (const lisp s);
  31. bool isNull (const lisp s);
  32. void destroy (lisp s);
  33. base getAtom (const lisp s);
  34. void read_lisp (lisp& y, stringstream &xstream);
  35. void read_s_expr (base prev, lisp& y, stringstream &xstream);
  36. void read_seq (lisp& y, stringstream &xstream);
  37. void write_lisp (const lisp x);
  38. void write_seq (const lisp x);
  39.  
  40. lisp  del_atom(lisp s, char x)
  41. {
  42.         if (isNull(s)) return NULL;
  43.         if (isAtom(s))
  44.         {
  45.                 if(s->node.atom != x)
  46.                         return s;
  47.                 else
  48.                 {
  49.                         delete s;
  50.                         return NULL;
  51.                 }
  52.         }
  53.         s->node.pair.hd = del_atom(head(s),x);
  54.         s->node.pair.tl = del_atom(tail(s),x);
  55.         return ((head(s) == NULL)? tail(s) : s);
  56. }
  57.  
  58. int main()
  59. {
  60.         stringstream xstream;
  61.         bool b = 1;
  62.         lisp s = NULL;
  63.         char x;
  64.         char str[100];
  65.         int c = 0;
  66.         while(c != 3)
  67.         {
  68.                 cout << "Введите 1, если хотите ввести выражение с клавиатуры.\n"
  69.                         "Введите 2, если еспользовать выражение из файла test.txt.\n"
  70.                         "Введите 3, если хотите закончить работу." << endl;
  71.                 cin >> c;
  72.                 switch(c)
  73.                 {
  74.                         case 1:
  75.                         {
  76.                                 cout<<"Введите искомый элемент X\n";
  77.                                 cin >> x;
  78.                                 cout << "Введите выражение: \n";
  79.                                 cin.get();
  80.                                 cin.getline(str, 1000);
  81.                                 xstream << str;
  82.                                 read_lisp(s, xstream);
  83.                                 break;
  84.                         }
  85.                         case 2:
  86.                         {
  87.                                 cout<<"Введите искомый элемент X\n";
  88.                                 cin >> x;
  89.                                 ifstream outfile;
  90.                                 outfile.open("test.txt");
  91.                                 if (!outfile)
  92.                                 {
  93.                                         cout << "Входной файл не открыт!\n";
  94.                                         b = 0;
  95.                                         break;
  96.                                 }
  97.                                 outfile.read(str, 1000);
  98.                                 outfile.close();
  99.                                 xstream << str;
  100.                                 read_lisp (s, xstream);
  101.                                 break;
  102.                         }
  103.                         case 3:
  104.                         {
  105.                                 b=0;
  106.                                 break;
  107.                         }
  108.                         default:
  109.                         {
  110.                                 cout<<"Введите верное число\n";
  111.                                 break;
  112.                         }
  113.                 }
  114.                 if(b)
  115.                 {
  116.                         cout << "Введен list1: \n";
  117.                         write_lisp (s);
  118.                         cout<<endl;
  119.                         cout << "Произведен поиск элемента: " << x << endl;
  120.                         if(s = del_atom(s,x))
  121.                         {
  122.                                 cout << "Элемент Х удален.\n";
  123.                                 write_lisp(s);
  124.                                 cout << endl;
  125.                         }
  126.                         else
  127.                                 cout << s << "Элемент Х не найден.\n";
  128.                         destroy(s);
  129.                 }
  130.         }
  131. return 0;
  132. }
  133.  
  134. lisp head(const lisp s)
  135. {
  136.         if (s != NULL)
  137.                 if (!isAtom(s))
  138.                         return s->node.pair.hd;
  139.                 else
  140.                 {
  141.                         cerr << "Error: Head(atom) \n";
  142.                         exit(1);
  143.                 }
  144.         else
  145.         {
  146.                 cerr << "Error: Head(nil) \n";
  147.                 exit(1);
  148.         }
  149. }
  150.  
  151. bool isAtom(const lisp s)
  152. {
  153.         if(s == NULL)
  154.                 return false;
  155.         else
  156.                 return (s -> tag);
  157. }
  158.  
  159. bool isNull (const lisp s)
  160. {
  161.         return s==NULL;
  162. }
  163.  
  164. lisp tail(const lisp s)
  165. {
  166.         if (s != NULL)
  167.                 if (!isAtom(s))
  168.                         return s->node.pair.tl;
  169.         else
  170.         {
  171.                 cerr << "Error: Tail(atom) \n";
  172.                 exit(1);
  173.         }
  174.         else
  175.         {
  176.                 cerr << "Error: Tail(nil) \n";
  177.                 exit(1);
  178.         }
  179. }
  180.  
  181. lisp cons(const lisp h, const lisp t)
  182. {
  183.         lisp p;
  184.         if (isAtom(t))
  185.         {
  186.                 cerr << "Error: Tail is head \n";
  187.                 exit(1);
  188.         }
  189.         else
  190.         {
  191.                 p = new s_expr;
  192.                 if ( p == NULL)
  193.                 {
  194.                         cerr << "Memory not enough\n";
  195.                         exit(1);
  196.                 }
  197.                 else
  198.                 {
  199.                         p->tag = false;
  200.                         p->node.pair.hd = h;
  201.                         p->node.pair.tl = t;
  202.                         return p;
  203.                 }
  204.         }
  205. }
  206.  
  207. lisp make_atom(const base x)
  208. {
  209.         lisp s;
  210.         s = new s_expr;
  211.         s -> tag = true;
  212.         s->node.atom = x;
  213.         return s;
  214. }
  215.  
  216.  
  217. void destroy (lisp s)
  218. {
  219.         if (s != NULL)
  220.         {
  221.                 if (!isAtom(s))
  222.                 {
  223.                         destroy (head (s));
  224.                         destroy (tail(s));
  225.                 }
  226.                 delete s;
  227.         }
  228. }
  229.  
  230. base getAtom (const lisp s)
  231. {
  232.         if (!isAtom(s))
  233.         {
  234.                 cerr << "Error: getAtom(s) for !isAtom(s) \n";
  235.                 exit(1);
  236.         }
  237.         else
  238.                 return (s->node.atom);
  239. }
  240. void read_lisp (lisp& y, stringstream &xstream)
  241. {
  242.         base x;
  243.         do
  244.                 xstream >> x;
  245.         while (x==' ');
  246.                 if(x)
  247.                         read_s_expr (x, y, xstream);
  248. }
  249.  
  250. void read_s_expr (base prev, lisp& y, stringstream &xstream)
  251. {
  252.         if (prev == ')' )
  253.         {
  254.                 cerr << "Error: the initial brace is closing\n";
  255.                 exit(1);
  256.         }
  257.         else
  258.                 if (prev != '(' )
  259.                         y = make_atom (prev);
  260.                 else read_seq (y, xstream);
  261. }
  262.  
  263. void read_seq (lisp& y, stringstream &xstream)
  264. {
  265.         base x;
  266.         lisp p1, p2;
  267.         if (!(xstream >> x))
  268.         {
  269.                 cerr << "Error: there is no closing bracket\n";
  270.                 exit(1);
  271.         }
  272.         else
  273.         {
  274.                 while  ( x==' ' )
  275.                         xstream >> x;
  276.                 if ( x == ')' )
  277.                         y = NULL;
  278.                 else
  279.                 {
  280.                         read_s_expr (x, p1, xstream);
  281.                         read_seq (p2, xstream);
  282.                         y = cons (p1, p2);
  283.                 }
  284.         }
  285. }
  286.  
  287. void write_lisp (const lisp x)
  288. {
  289.         if (isNull(x))
  290.                 cout << " ()";
  291.         else
  292.                 if (isAtom(x))
  293.                         cout << ' ' << x->node.atom;
  294.                 else
  295.                 {
  296.                         cout << " (" ;
  297.                         write_seq(x);
  298.                         cout << " )";
  299.                 }
  300. }
  301.  
  302. void write_seq (const lisp x)
  303. {
  304.         if (!isNull(x))
  305.         {
  306.                 write_lisp(head (x));
  307.                 write_seq(tail (x));
  308.         }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement