数据库课程设计
这学期的大作业,数据库课程设计,SQL语句解析,主要是实现SQL中select语句的功能,还包括where限制语句和order by语句。
虽然写的有点挫,但还是第一次写这么长的代码,纪念一下吧。
/************************************************************************* > File Name: sql.cpp > Author: Chierush > Mail: qinxiaojie1@gmail.com > Created Time: 2013年06月21日 星期五 10时48分42秒 ************************************************************************/ #include <iostream> #include <cstring> #include <cstdlib> #include <set> #include <cstdio> #include <string> #include <vector> #include <map> #include <cmath> #include <algorithm> #include <iomanip> #include <fstream> #define LL long long #define LLU unsigned long long using namespace std; template <class T> int cmp(T x,T y)//比较函数模板 { if (x<y) return 1; else if (x==y) return 0; return -1; } vector<int>Order,Select;//排序指标,SELECT的项 class Student { public: LL Sno,Sage;//学号 string Sname,Ssex,Sdept;//姓名,性别,学院 void print() const;//输出记录 bool operator<(const Student &c) const;//重载<运算符 bool operator==(const Student &c);//重载==运算符 }; struct node { int id,place; bool operator<(const node &c) const { return place<c.place; } }; map<string,int>ID;//项的编号 map<int,string>str;//编号对应的项 vector<Student> S;//学生集合S bool DESC[5],DISTINCT,where,order;//降序标识,取不同标识,where语句标识,order语句标识 bool Between,NotBetween;//beween语句标识,not between语句标识 bool In,NotIn;//in语句,not in语句标识 bool Like,NotLike;//like,not like语句标识 bool Null,NotNull;//is null,is not null语句标识 bool And,Or,Single;//and,or单语句限制标识 string BetweenLowerString,BetweenUpperString;//string类型between上下界 LL BetweenLowerLL,BetweenUpperLL;//long long类型的between上下界 set<LL>InSetLL;//in语句long long类型内容集合 set<string>InSetString;//in语句string类型集合 string LikeValue;//like对象 string SingleValueString;//where语句中限制条件string类型的值 LL SingleValueLL;//where语句中限制条件long long类型的值 LL CompareQueueValueLL[10];//where语句中and或or语句拆分后每个限制单元的long long类型比较值 string CompareQueueValueString[10];//where语句中and或or语句拆分后每个限制单元的string类型比较值 int CompareQueueOperator[10];//where语句中and或or语句拆分后每个限制单元的比较符号的编号 int CompareQueueObject[10];//where语句中and或or语句拆分后每个限制单元的被比较的项的编号 int CountUnit;//and或or连接的限制语句中的限制单元个数 int BetweenObject;//受between语句限制的项的编号 int InObject;//受in语句限制的项的编号 int LikeObject;//受like语句限制的项的编号 int NullObject;//受is null语句限制的项的编号 int SingleObject;//where语句中受单限制的项的编号 int SingleOperator;//where语句中单语句运算符的代号 template <class T> bool UnitCompare(T x,T y,int ope)//大小限制条件下的比较函数模板 { if (ope==1) return x>=y; else if (ope==2) return x<=y; else if (ope==3) return x!=y; else if (ope==4) return x==y; else if (ope==5) return x<y; else return x>y; } void init();//初始化 void welcome();//欢迎窗口 void error();//错误提示 void help();//帮助文档 void command();//等待输入命令 void analyse(string);//分析输入的命令 void output();//输出函数 void quit();//结束 bool check(Student);//判断某记录是否符合where限制条件 bool SetFind(Student);//查找集合中有没有指定的对象 bool CheckIn(Student);//查看该学生是否满足in语句限制 bool CheckBetween(Student);//查看该学生是否满足between语句限制 bool CheckLike(Student);//查看该学生是否满足like语句限制 bool CheckNull(Student);//查看该学生是否满足is null语句限制 bool CheckSingle(Student);//查看该学生是否满足单条件语句限制 bool CheckAnd(Student);//查看该学生是否满足and语句连接的多条件语句限制 bool CheckOr(Student);//查看该学生是否满足or语句连接的多条件语句限制 bool CheckUnit(Student,int);//查看该学生是否满足多条件语句限制下的某单一语句限制 void ReadFile();//读文件函数 void MakeOrder(string);//处理排序条件 void MakeSelect(string);//处理选择的项 void MakeWhere(string);//处理where中的限制条件 void MakeBetween(string);//处理between语句限制 void MakeIn(string);//处理in语句限制 void MakeLike(string);//处理like语句限制 void MakeNull(string);//处理null语句限制 void MakeSingle(string);//处理where语句中单条件限制 void MakeAnd(string);//处理where语句中AND关联词连接的限制 void MakeOr(string);//处理where语句中OR关联词连接的限制 void MakeUnit(string,int);//出来where语句多条件限制下的单个限制 void PrintChar(char,int);//输出指定数量的字符 LL StringToLL(string);//把指定的串转化成long long string StringToString(string);//提取制定的串 int main() { init(); string cmd; welcome(); while (getline(cin,cmd)) { if (cmd=="quit") return 0; else if (cmd=="help") { help(); break; } else if (cmd=="enter") break; else error(); } command(); while (getline(cin,cmd)) { if (cmd=="quit") return 0; analyse(cmd); command(); } return 0; } bool Student::operator<(const Student &c) const { int flag; for (int i=0;i<Order.size();++i) { if (Order[i]==0) flag=cmp(Sno,c.Sno); else if (Order[i]==1) flag=cmp(Sage,c.Sage); else if (Order[i]==2) flag=cmp(Sname,c.Sname); else if (Order[i]==3) flag=cmp(Ssex,c.Ssex); else flag=cmp(Sdept,c.Sdept); if (flag==1) return (DESC[Order[i]])?false:true; else if (flag==-1) return (DESC[Order[i]])?true:false; } return true; } bool Student::operator==(const Student &c) { for (int i=0;i<Select.size();++i) if (Select[i]==0 && Sno!=c.Sno) return false; else if (Select[i]==1 && Sage!=c.Sage) return false; else if (Select[i]==2 && Sname!=c.Sname) return false; else if (Select[i]==3 && Ssex!=c.Ssex) return false; else if (Select[i]==4 && Sdept!=c.Sdept) return false; return true; } void Student::print() const { cout<<"|"; for (int i=0;i<Select.size();++i) if (Select[i]==0) { if (Sno!=-1) cout<<" "<<setiosflags(ios::left)<<setw(15)<<Sno<<'|'; else cout<<" "<<setiosflags(ios::left)<<setw(15)<<"NULL"<<'|'; } else if (Select[i]==1) { if (Sage!=-1) cout<<" "<<setiosflags(ios::left)<<setw(12)<<Sage<<'|'; else cout<<" "<<setiosflags(ios::left)<<setw(12)<<"NULL"<<'|'; } else if (Select[i]==2) { if (Sname!="NULL") { cout<<" "<<setiosflags(ios::left)<<setw(12)<<Sname; for (int j=0;j<Sname.length()/3;++j) cout<<' '; } else cout<<" "<<setiosflags(ios::left)<<setw(12)<<"NULL"; cout<<'|'; } else if (Select[i]==3) { if (Ssex!="NULL") { cout<<" "<<setiosflags(ios::left)<<setw(12)<<Ssex; for (int j=0;j<Ssex.length()/3;++j) cout<<' '; } else cout<<" "<<setiosflags(ios::left)<<setw(12)<<"NULL"; cout<<'|'; } else { if (Sdept!="NULL") cout<<" "<<setiosflags(ios::left)<<setw(12)<<Sdept<<'|'; else cout<<" "<<setiosflags(ios::left)<<setw(12)<<"NULL"<<'|'; } cout<<resetiosflags(ios::left)<<endl; } void init() { ID.clear(); str.clear(); ID["Sno"]=0,ID["Sage"]=1,ID["Sname"]=2,ID["Ssex"]=3,ID["Sdept"]=4; str[0]="Sno",str[1]="Sage",str[2]="Sname",str[3]="Ssex",str[4]="Sdept"; } void error() { puts(""); puts("====================================================================================================================="); puts(""); puts("Input Error! Please try again : "); puts(""); } void welcome() { puts(" ====================================================="); puts(" | |"); puts(" | |"); puts(" | |"); puts(" | |"); puts(" | Welcome to MySQL |"); puts(" | |"); puts(" | |"); puts(" | |"); puts(" | |"); puts(" | |"); puts(" | Please press |"); puts(" | |"); puts(" | \'help\' for help |"); puts(" | \'quit\' to quit |"); puts(" | \'enter\' to enter |"); puts(" | |"); puts(" | |"); puts(" ====================================================="); puts(""); puts(""); puts(" Made by Qinxiaojie,All rights reserved"); puts(""); printf("Please input your choose : "); } void help() { puts(""); puts(" HELP"); puts(""); puts("====================================================================================================================="); puts("Please follow the following format:"); puts(""); puts(" SELECT [ALL|*|DISTINCT] <属性表达式1>[,<属性表达式2>…] "); puts(" FROM <表名1>[,<表名2>…] "); puts(" [WHERE <条件表达式>] "); puts(" [ORDER BY <属性组> [ASC|DESC];"); puts(" 注意:<属性表达式>中无聚集函数;<条件表达式>中无嵌套查询。"); puts(""); puts("If there are any question else to you,please read the book《数据库系统概论》or send e-mail to qinxiaojie1@gmail.com"); puts(""); puts("Good Luck!"); puts(""); puts("====================================================================================================================="); puts(""); puts("Please any key to return"); puts(""); string cmd; getline(cin,cmd); } void command() { puts(""); puts("====================================================================================================================="); puts(""); puts("Please Enter Your Command : "); puts("Don\'t forget to enter \'run\' to run your command after input"); puts(""); } void analyse(string cmd) { Order.clear(); Select.clear(); S.clear(); DISTINCT=where=order=false; do { if (cmd=="run" || cmd=="RUN") { ReadFile(); if (order) sort(S.begin(),S.end()); output(); return; } else if (cmd.find("SELECT")!=string::npos || cmd.find("select")!=string::npos) { MakeSelect(cmd); if (!Select.size()) error(); } else if (cmd.find("FROM")!=string::npos || cmd.find("from")!=string::npos) { if (cmd.find("Student")!=string::npos || cmd.find("STUDENT")!=string::npos || cmd.find("student")!=string::npos) continue; else error(); } else if (cmd.find("WHERE")!=string::npos || cmd.find("where")!=string::npos) { where=true; MakeWhere(cmd); } else if (cmd.find("ORDER BY")!=string::npos || cmd.find("order by")!=string::npos) { MakeOrder(cmd); if (Order.size()) order=true; else error(); } else if (cmd=="quit") quit(); else error(); }while (getline(cin,cmd)); } void quit() { exit(0); } void output() { puts(""); if (!Select.size() || !S.size()) { puts(""); puts(" Student"); puts(""); printf("==========================================================================================================="); puts("=========="); puts(""); puts(" NULL"); puts(""); return; } int width=Select.size()+1; for (int i=0;i<Select.size();++i) if (Select[i]==0) width+=17; else width+=14; for (int i=(width/2-4>=0)?width/2-4:0;i>=0;--i) cout<<' '; cout<<"Student"<<endl<<endl; PrintChar('=',width); cout<<'|'; for (int i=0;i<Select.size();++i) if (Select[i]) cout<<" "<<setiosflags(ios::left)<<setw(12)<<str[Select[i]]<<'|'; else cout<<" "<<setiosflags(ios::left)<<setw(15)<<str[Select[i]]<<'|'; cout<<endl; PrintChar('-',width); for (int i=0;i<S.size();++i) { S[i].print(); PrintChar('-',width); } puts(""); } void PrintChar(char c,int n) { for (int i=0;i<n;++i) cout<<c; cout<<endl; } void ReadFile() { Student p; ifstream inData("input.txt"); S.clear(); while (!inData.eof()) { inData>>p.Sno>>p.Sage>>p.Sname>>p.Ssex>>p.Sdept; if (inData.eof()) break; if (!where || (where && check(p))) { if (DISTINCT && !SetFind(p)) S.push_back(p); else if (!DISTINCT) S.push_back(p); } } inData.close(); } void MakeOrder(string cmd) { memset(DESC,false,sizeof(DESC)); Order.clear(); string now; node p[5]; for (int i=0;i<5;++i) p[i].id=i,p[i].place=cmd.find(str[i]); sort(p,p+5); for (int i=0;i<5;++i) if (p[i].place!=string::npos) Order.push_back(p[i].id); for (int i=0;i<Order.size();++i) if (cmd.substr(cmd.find(str[Order[i]]),str[Order[i]].size()+6).find("DESC")!=string::npos) DESC[Order[i]]=true; else if (cmd.substr(cmd.find(str[Order[i]]),str[Order[i]].size()+6).find("desc")!=string::npos) DESC[Order[i]]=true; } void MakeSelect(string cmd) { Select.clear(); for (map<string,int>::iterator it=ID.begin();it!=ID.end();it++) if (cmd.find(it->first)!=string::npos) Select.push_back(it->second); if (cmd.find("*")!=string::npos || cmd.find("ALL")!=string::npos || cmd.find("all")!=string::npos) { for (int i=0;i<5;++i) Select.push_back(i); } else if (cmd.find("DISTINCT")!=string::npos || cmd.find("distinct")!=string::npos) DISTINCT=true; sort(Select.begin(),Select.end()); } void MakeWhere(string cmd) { bool flag=false; for (int i=0;i<5;++i) if (cmd.find(str[i])!=string::npos) { flag=true; break; } if (!flag) { error(); return; } In=NotIn=Between=NotBetween=Like=NotLike=Null=NotNull=And=Or=Single=false; if (cmd.find("BETWEEN")!=string::npos || cmd.find("between")!=string::npos) { if (cmd.find("NOT BETWEEN")!=string::npos || cmd.find("not between")!=string::npos) NotBetween=true; else Between=true; MakeBetween(cmd); } else if (cmd.find("IN")!=string::npos || cmd.find("in")!=string::npos) { if (cmd.find("NOT IN")!=string::npos || cmd.find("not in")!=string::npos) NotIn=true; else In=true; MakeIn(cmd); } else if (cmd.find("LIKE")!=string::npos || cmd.find("like")!=string::npos) { if (cmd.find("NOT LIKE")!=string::npos || cmd.find("not like")!=string::npos) NotLike=true; else Like=true; MakeLike(cmd); } else if (cmd.find("NULL")!=string::npos || cmd.find("null")!=string::npos) { if (cmd.find("NOT NULL")!=string::npos || cmd.find("not null")!=string::npos) NotNull=true; else Null=true; MakeNull(cmd); } else if (cmd.find("AND")!=string::npos || cmd.find("and")!=string::npos) { And=true; MakeAnd(cmd); } else if (cmd.find("OR")!=string::npos || cmd.find("or")!=string::npos) { Or=true; MakeOr(cmd); } else { Single=true; MakeSingle(cmd); } } bool check(Student p) { if (In) { if (CheckIn(p)) return true; else return false; } else if (NotIn) { if (!CheckIn(p)) return true; else return false; } else if (Between) { if (CheckBetween(p)) return true; else return false; } else if (NotBetween) { if (!CheckBetween(p)) return true; else return false; } else if (Like) { if (CheckLike(p)) return true; else return false; } else if (NotLike) { if (!CheckLike(p)) return true; else return false; } else if (Null) { if (CheckNull(p)) return true; else return false; } else if (NotNull) { if (!CheckNull(p)) return true; else return false; } else if (And) { if (CheckAnd(p)) return true; else return false; } else if (Or) { if (CheckOr(p)) return true; else return false; } else { if (CheckSingle(p)) return true; else return false; } } bool SetFind(Student c) { for (int i=0;i<S.size();++i) if (S[i]==c) return true; return false; } void MakeBetween(string cmd) { for (int i=0;i<5;++i) if (cmd.find(str[i])!=string::npos) { BetweenObject=i; break; } int posBetween=cmd.find("BETWEEN"); if (posBetween==string::npos) posBetween=cmd.find("between"); int posAnd=cmd.find("AND"); if (posAnd==string::npos) posAnd=cmd.find("and"); if (BetweenObject<2) { BetweenLowerLL=StringToLL(cmd.substr(posBetween+8,posAnd-posBetween-9)); BetweenUpperLL=StringToLL(cmd.substr(posAnd+4,cmd.length()-posAnd-4)); } else { BetweenLowerString=StringToString(cmd.substr(posBetween+8,posAnd-posBetween-9)); BetweenUpperString=StringToString(cmd.substr(posAnd+4,cmd.length()-posAnd-4)); } } void MakeIn(string cmd) { InSetLL.clear(); InSetString.clear(); for (int i=0;i<5;++i) if (cmd.find(str[i])!=string::npos) { InObject=i; break; } if (InObject<2) { int i=cmd.find("(")+1; int pre=i; while (i<cmd.length()) { if (cmd[i]==')' || cmd[i]==';') break; for (;i<cmd.length() && cmd[i]!=',' && cmd[i]!=')';++i); InSetLL.insert(StringToLL(cmd.substr(pre,i-pre))); pre=++i; } } else { int i=cmd.find("(")+2; int pre=i; while (i<cmd.length()) { if (cmd[i]==';' || cmd[i]==')') break; for (;i<cmd.length() && cmd[i]!='\'' && cmd[i]!='\"' && cmd[i]!=',' && cmd[i]!=')';++i); InSetString.insert(StringToString(cmd.substr(pre,i-pre))); i+=3; pre=i; } } } void MakeLike(string cmd) { for (int i=0;i<5;++i) if (cmd.find(str[i])!=string::npos) { LikeObject=i; break; } int pos,last; for (pos=0;pos<cmd.length();++pos) if (cmd[pos]=='\"' || cmd[pos]=='\'') break; ++pos; for (last=cmd.length()-1;cmd[last]!='\'' && cmd[last]!='\"';--last); LikeValue=cmd.substr(pos,last-pos); } void MakeNull(string cmd) { for (int i=0;i<5;++i) if (cmd.find(str[i])!=string::npos) { NullObject=i; break; } } void MakeSingle(string cmd) { for (int i=0;i<5;++i) if (cmd.find(str[i])!=string::npos) { SingleObject=i; break; } if (cmd.find(">=")!=string::npos || cmd.find("!<")!=string::npos) SingleOperator=1; else if (cmd.find("<=")!=string::npos ||cmd.find("!>")!=string::npos) SingleOperator=2; else if (cmd.find("<>")!=string::npos || cmd.find("!=")!=string::npos) SingleOperator=3; else if (cmd.find("=")!=string::npos) SingleOperator=4; else if (cmd.find("<")!=string::npos) SingleOperator=5; else SingleOperator=6; int pos; for (pos=cmd.length()-1;pos>=0;--pos) if (cmd[pos]=='<' || cmd[pos]=='>' || cmd[pos]=='=') break; if (SingleObject<2) SingleValueLL=StringToLL(cmd.substr(pos+1,cmd.length()-pos-1)); else SingleValueString=StringToString(cmd.substr(pos+2,cmd.length()-pos-3)); } void MakeAnd(string cmd) { CountUnit=0; memset(CompareQueueValueLL,-1,sizeof(CompareQueueValueLL)); memset(CompareQueueOperator,-1,sizeof(CompareQueueOperator)); memset(CompareQueueObject,-1,sizeof(CompareQueueObject)); for (int i=0;i<10;++i) CompareQueueValueString[i]=""; int pre=6; for (int i=6;i<cmd.length();++i) if (cmd.substr(i,3)=="AND" || cmd.substr(i,3)=="and") { MakeUnit(cmd.substr(pre,i-1-pre),CountUnit++); pre=i+3; } MakeUnit(cmd.substr(pre,cmd.length()-pre),CountUnit++); } void MakeOr(string cmd) { CountUnit=0; memset(CompareQueueValueLL,-1,sizeof(CompareQueueValueLL)); memset(CompareQueueOperator,-1,sizeof(CompareQueueOperator)); memset(CompareQueueObject,-1,sizeof(CompareQueueObject)); for (int i=0;i<10;++i) CompareQueueValueString[i]=""; int pre=6; for (int i=6;i<cmd.length();++i) if (cmd.substr(i,2)=="OR" || cmd.substr(i,2)=="or") { MakeUnit(cmd.substr(pre,i-1-pre),CountUnit++); pre=i+2; } MakeUnit(cmd.substr(pre,cmd.length()-pre),CountUnit++); } void MakeUnit(string cmd,int pos) { int &unitobject=CompareQueueObject[pos]; int &unitoperator=CompareQueueOperator[pos]; LL &unitvaluell=CompareQueueValueLL[pos]; string &unitvaluestring=CompareQueueValueString[pos]; for (int i=0;i<5;++i) if (cmd.find(str[i])!=string::npos) { unitobject=i; break; } if (cmd.find(">=")!=string::npos || cmd.find("!<")!=string::npos) unitoperator=1; else if (cmd.find("<=")!=string::npos ||cmd.find("!>")!=string::npos) unitoperator=2; else if (cmd.find("<>")!=string::npos || cmd.find("!=")!=string::npos) unitoperator=3; else if (cmd.find("=")!=string::npos) unitoperator=4; else if (cmd.find("<")!=string::npos) unitoperator=5; else unitoperator=6; for (pos=cmd.length()-1;pos>=0;--pos) if (cmd[pos]=='<' || cmd[pos]=='>' || cmd[pos]=='=') break; if (unitobject<2) unitvaluell=StringToLL(cmd.substr(pos+1,cmd.length()-pos-1)); else unitvaluestring=StringToString(cmd.substr(pos+2,cmd.length()-pos-2)); } LL StringToLL(string str) { LL ans=0; for (int i=0;i<str.length();++i) if (str[i]>='0' && str[i]<='9') ans=ans*10+str[i]-'0'; return ans; } string StringToString(string str) { int l=0,r=str.length()-1; while (str[l]==' ' || str[l]=='\'' || str[l]=='\"' || str[l]==',') ++l; while (str[r]==' ' || str[r]=='\'' || str[r]=='\"' || str[r]==';' || str[r]==',') --r; string now=str.substr(l,r-l+1); return now; } bool CheckIn(Student p) { if (InObject==0) { if (InSetLL.count(p.Sno)) return true; else return false; } else if (InObject==1) { if (InSetLL.count(p.Sage)) return true; else return false; } else if (InObject==2) { if (InSetString.count(p.Sname)) return true; else return false; } else if (InObject==3) { if (InSetString.count(p.Ssex)) return true; else return false; } else { if (InSetString.count(p.Sdept)) return true; else return false; } } bool CheckBetween(Student p) { if (BetweenObject==0) { if (p.Sno>=BetweenLowerLL && p.Sno<=BetweenUpperLL) return true; else return false; } else if (BetweenObject==1) { if (p.Sage>=BetweenLowerLL && p.Sage<=BetweenUpperLL) return true; else return false; } else if (BetweenObject==2) { if (p.Sname>=BetweenLowerString && p.Sname<=BetweenUpperString) return true; else return false; } else if (BetweenObject==3) { if (p.Ssex>=BetweenLowerString && p.Ssex<=BetweenUpperString) return true; else return false; } else { if (p.Sdept>=BetweenLowerString && p.Sdept<=BetweenUpperString) return true; else return false; } } bool CheckLike(Student p) { if (LikeObject==0) { char str[50]; sprintf(str,"%lld",p.Sno); string now=""; for (int i=0;i<strlen(str);++i) now+=str[i]; if (now.find(LikeValue)!=string::npos) return true; else return false; } else if (LikeObject==1) { char str[50]; sprintf(str,"%lld",p.Sage); string now=""; for (int i=0;i<strlen(str);++i) now+=str[i]; if (now.find(LikeValue)!=string::npos) return true; else return false; } else if (LikeObject==2) { if (p.Sname.find(LikeValue)!=string::npos) return true; else return false; } else if (LikeObject==3) { if (p.Ssex.find(LikeValue)!=string::npos) return true; else return false; } else if (LikeObject==4) { if (p.Sdept.find(LikeValue)!=string::npos) return true; else return false; } } bool CheckNull(Student p) { if (NullObject==0) { if (p.Sno==-1) return true; else return false; } else if (NullObject==1) { if (p.Sage==-1) return true; else return false; } else if (NullObject==2) { if (p.Sname=="NULL") return true; else return false; } else if (NullObject==3) { if (p.Ssex=="NULL") return true; else return false; } else { if (p.Sdept=="NULL") return true; else return false; } } bool CheckSingle(Student p) { if (SingleObject==0) { if (UnitCompare(p.Sno,SingleValueLL,SingleOperator)) return true; else return false; } else if (SingleObject==1) { if (UnitCompare(p.Sage,SingleValueLL,SingleOperator)) return true; else return false; } else if (SingleObject==2) { if (UnitCompare(p.Sname,SingleValueString,SingleOperator)) return true; else return false; } else if (SingleObject==3) { if (UnitCompare(p.Ssex,SingleValueString,SingleOperator)) return true; else return false; } else { if (UnitCompare(p.Sdept,SingleValueString,SingleOperator)) return true; else return false; } } bool CheckAnd(Student p) { bool ans=true; for (int i=0;i<CountUnit;++i) ans&=CheckUnit(p,i); return ans; } bool CheckOr(Student p) { bool ans=false; for (int i=0;i<CountUnit;++i) ans|=CheckUnit(p,i); return ans; } bool CheckUnit(Student p,int pos) { LL unitvaluell; string unitvaluestring; int unitoperator=CompareQueueOperator[pos]; if (CompareQueueObject[pos]<2) unitvaluell=CompareQueueValueLL[pos]; else unitvaluestring=CompareQueueValueString[pos]; if (CompareQueueObject[pos]==0) return UnitCompare(p.Sno,unitvaluell,unitoperator); else if (CompareQueueObject[pos]==1) return UnitCompare(p.Sage,unitvaluell,unitoperator); else if (CompareQueueObject[pos]==2) return UnitCompare(p.Sname,unitvaluestring,unitoperator); else if (CompareQueueObject[pos]==3) return UnitCompare(p.Ssex,unitvaluestring,unitoperator); else return UnitCompare(p.Sdept,unitvaluestring,unitoperator); }