#include<iostream>
#include<cmath>
using namespace std;
class Node
{
int info;
Node* link;
public:
Node(int n = 0, Node* p = NULL)
{
info = n;
link = p;
}
friend class list;
};
class list
{
private:
Node* first; //链表头指针
public:
list()//构造函数
{
first = new Node(0);
}
Node* CreatNode(int data)//建立新节点
{
Node* P = new Node(data);
return P;
}
void Insert(Node* p)//将p插入生成链表
{
Node* tempP = first->link, * tempQ = first;//tempQ指向tempP前面的一个节点
while (tempP != NULL)
{
tempQ = tempP;
tempP = tempP->link;
}
p->link = tempP; //插入节点
tempQ->link = p;
first->info++;
}
void MakeEmpty()//清空一个链表
{
Node* tempP;
while (first->link != NULL)
{
tempP = first->link;
first->link = tempP->link;
delete tempP;
first->info--;
}
}
void PrintList()
{
cout << first->info << " Nodes:";
Node* tempP = first->link;
while (tempP != NULL)
{
cout << tempP->info << " ";
tempP = tempP->link;
}
cout << endl;
}
void DeleteNode(Node* p)
{
Node* tempP = first->link, * tempQ = first;
while (tempP != NULL && tempP != p)
{
tempQ = tempP;
tempP = tempP->link;
}
if (tempP == p)
{
tempQ->link = tempP->link;
delete p;
first->info--;
}
}
void Not()
{
Node* tempP = first;
Node* tempQ = first->link;
Node* tempR = first->link->link;
while (tempR != NULL)
{
if (tempQ->info == 4)
{
if (tempR->info == 1)
tempR->info = 0;
else if(tempR->info == 0)
tempR->info = 1;
DeleteNode(tempQ);
tempQ = tempP->link;
}
tempQ = tempR;
tempR = tempQ->link;
}
}
void And()
{
Node* tempP = first->link;
Node* tempQ = first->link->link;
Node* tempR = tempQ->link;
while (tempR != NULL)
{
if (tempQ->info == 3)
{
if (tempP->info == 1 && tempR->info == 1)
{
tempP->info = 1;
DeleteNode(tempQ);
DeleteNode(tempR);
tempQ = tempP->link;
if (tempQ == NULL)
tempR = NULL;
else
tempR = tempQ->link;
}
else
{
tempP->info = 0;
DeleteNode(tempQ);
DeleteNode(tempR);
tempQ = tempP->link;
if (tempQ == NULL)
tempR = NULL;
else
tempR = tempQ->link;
}
}
if (tempR != NULL)
{
tempQ = tempQ->link;
tempP = tempP->link;
tempR = tempR->link;
}
else
{
break;
}
}
}
void Or()
{
Node* tempP = first->link;
Node* tempQ = first->link->link;
Node* tempR = tempQ->link;
while (tempR != NULL)
{
if (tempQ->info == 2)
{
if (tempP->info == 1 ||tempR->info == 1)
{
tempP->info = 1;
DeleteNode(tempQ);
DeleteNode(tempR);
if (tempP->link != NULL)
{
tempQ = tempP->link;
tempR = tempQ->link;
}
else
{
tempQ = NULL;
tempR = NULL;
}
}
else
{
tempP->info = 0;
DeleteNode(tempQ);
DeleteNode(tempR);
if (tempP->link != NULL)
{
tempQ = tempP->link;
tempR = tempQ->link;
}
else
{
tempQ = NULL;
tempR = NULL;
}
}
}
if (tempR != NULL)
{
tempQ = tempQ->link;
tempP = tempP->link;
tempR = tempR->link;
}
else
{
break;
}
}
}
void Print()
{
if (first->info > 1)
cout << "error";
else
{
if (first->link->info == 1)
cout << "true";
else
cout << "false";
}
}
~list()//析构函数,删除所有结点
{
MakeEmpty();
delete first;
}
};
int main()
{
list l;
string temp[255];
int size = 0;//字符数减一
while (cin >> temp[size])
{
int symble = 0;
if (cin.get() == '\n')
symble = 1;
if (temp[size] == "or")
{
l.Insert(l.CreatNode(2));
}
else if (temp[size] == "and")
{
l.Insert(l.CreatNode(3));
}
else if (temp[size] == "not")
{
l.Insert(l.CreatNode(4));
}
else if (temp[size] == "false")
{
l.Insert(l.CreatNode(0));
}
else if (temp[size] == "true")
{
l.Insert(l.CreatNode(1));
}
//cout << size << ":";
//l.PrintList();
size++;
if (symble)
break;
}
//cout << "总:";
//l.PrintList();
//cout << "Not:";
l.Not();
//l.PrintList();
//cout << "And:";
l.And();
//l.PrintList();
//cout << "Or:";
l.Or();
//l.PrintList();
l.Print();
}