#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
int whword(vector<char*>& lst, int a, int b, const char* pat){
for(int lx = a;lx <= b;lx++)
if(strcmp(lst[lx], pat) == 0)
return lx;
return -1;
}
int whword(vector<char*>& lst, const char* pat){
return whword(lst, 0, ((int)lst.size()) - 1, pat);
}
struct sing{
bool pnot;
char pc;
};
sing operator not(sing a){
a.pnot = not a.pnot;
return a;
}
void printsing(sing a){
if(a.pnot)
printf("not ");
printf("%c", a.pc);
return;
}
sing transsing(vector<char*>& lst, int a, int b){
sing ret;
int wrd_cnt = b-a+1;
ret.pnot = wrd_cnt == 2;
ret.pc = lst[b][0];
return ret;
}
struct cond{
int cnt;
sing sg1;
int con;
sing sg2;
};
cond operator not(cond a){
a.con = 1-a.con;
a.sg1 = not a.sg1;
a.sg2 = not a.sg2;
return a;
}
void printcond(cond a){
if(a.cnt == 1)
printsing(a.sg1);
else{
printsing(a.sg1);
printf(" %s " , a.con == 1 ? "and" : "or");
printsing(a.sg2);
}
return;
}
cond transcond(vector<char*>& lst, int a, int b){
int wh_and = whword(lst, a, b, "and");
int wh_or = whword(lst, a, b, "or");
int pos = max(wh_or, wh_and);
cond ret;
if(pos == -1){
ret.cnt = 1;
ret.sg1 = transsing(lst, a, b);
return ret;
}
ret.cnt = 2;
ret.con = pos == wh_and;
ret.sg1 = transsing(lst, a, pos-1);
ret.sg2 = transsing(lst, pos+1, b);
return ret;
}
int main(){
int T; scanf("%d\n", &T); while(T--){
char buf[50];
fgets(buf, 50, stdin);
if(feof(stdin)) break;
char* ptr = strtok(buf, " ");
vector<char*> lst;
while(ptr != NULL){
lst.push_back(ptr);
//printf("[%s]\n", ptr);
ptr = strtok(NULL, " ");
}
if(lst[(int)lst.size() - 1][0] == '\n')
lst.pop_back();
if(lst.size() < 4){
T++;
continue;
}
int wh_IF = whword(lst, "IF"), wh_THEN = whword(lst, "THEN");
//printf("IF = %d, THEN = %d\n", wh_IF, wh_THEN);
cond cc1 = not transcond(lst, wh_IF+1, wh_THEN-1),
cc2 = not transcond(lst, wh_THEN+1, (int)lst.size()-1);
printf("IF "); printcond(cc2); printf(" THEN ") ; printcond(cc1);
puts("");
}
return 0;
}
沒有留言:
張貼留言