功能还不够完善,部分情况未做处理
#include<stdio.h> #include<iostream> #include<string.h> #include<direct.h> #include<vector> #include<stdlib.h> using namespace std; typedef struct tableformat { char bsname[10][10];//属性名 char stype[10][10];//属性类型 int length[10];//每个属性的长度 char key[10];//主键 char belotable[20];//表名 int num;//属性个数 int notnull[10]; }tafot; typedef struct tiaojian { int type;//1 <,2 = ,3 >,4 !=,5 <=,6 >= int part; int le; char leftbie[20];//记录属性表名 char lefts[30]; int ri; char rightbie[20];//记录属性表名 char rights[30]; }tiao; typedef struct everygaindata { char first[20][50]; char second[20][50]; char three[20][50]; char four[20][50]; char five[20][50]; char six[20][50]; }ega; typedef struct shuxingji { //char biename[20]; char shu[5][20]; }shuji; typedef struct duobiao { char bioname[20]; char dsu[20][50]; }dou; typedef struct filedatas { //char biename[20]; char elem[20][50]; }fdata; typedef struct cunrentlocation { int loca; int type; }curent; void turn(char t[]) { for(int i=0;i<strlen(t);i++) if(t[i]>='A'&&t[i]<='Z') t[i]+=32; } int sou(char t[]) { int i=0; int flag=0; int n=0; for(i=0;i<strlen(t);i++) { if((t[i]>='A'&&t[i]<='Z')||(t[i]>='a'&&t[i]<='z')) { n=1; //cout<<"字母"<<t[i]<<endl; } else { if((t[i]>='0'&&t[i]<='9')||t[i]=='.') flag=1; else flag=0; //cout<<"数字"<<t[i]<<endl; } } if(flag==1&&n==0) return 1; else return 0; } string dateDeal(string sd) { string other=""; for(int i=0;i<sd.length();i++) if(sd[i]>='0'&&sd[i]<='9') other+=sd[i]; return other; } int soulocation(char ss[],tafot tf) { int i=0; while(i<tf.num) { if(strcmp(ss,tf.bsname[i])==0) { return i; } i++; } if(i==tf.num) cout<<"未找到"<<endl; } int compareType(char lesu[],char risu[],int lei,int yuns,int no)//两种问题 没有到汉字的比较 还有条件个数不对没传完 年份数据要去掉— { double le=0,ri=0; string zhuan=""; char interval[20]={0}; if(lei==1||lei==2)//整数。小数,年月日 { if(no==1) { le=atof(lesu); ri=atof(risu); } else { le=atof(risu); ri=atof(lesu); } if(yuns==1) { //cout<<le<<" < "<<ri<<endl; if(le<ri) return 1; else return 0; } else if(yuns==2) { if(le==ri) return 1; else return 0; } else if(yuns==3) { if(le>ri) return 1; else return 0; } else if(yuns==4) { if(le!=ri) return 1; else return 0; } else if(yuns==5) { if(le<=ri) return 1; else return 0; } else if(yuns==6) { if(le>=ri) return 1; else return 0; } } else if(lei==3) { if(no==1) { zhuan=lesu; zhuan=dateDeal(zhuan); strcpy(interval,zhuan.c_str()); le=atof(interval); ri=atof(risu); } else { zhuan=risu; zhuan=dateDeal(zhuan); strcpy(interval,zhuan.c_str()); le=atof(interval); ri=atof(lesu); } if(yuns==1) { //cout<<le<<" < "<<ri<<endl; if(le<ri) return 0; else return 1; } else if(yuns==2) { if(le==ri) return 1; else return 0; } else if(yuns==3) { if(le>ri) return 0; else return 1; } else if(yuns==4) { if(le!=ri) return 1; else return 0; } else if(yuns==5) { if(le<=ri) return 0; else return 1; } else if(yuns==6) { if(le>=ri) return 0; else return 1; } } else if(lei==5) { cout<<"需要传两个表的相应值"<<endl; } else if(lei==6) { if(yuns==2) { if(strcmp(lesu,risu)==0) return 1; else return 0; } else if(yuns==4) { if(strcmp(lesu,risu)!=0) return 1; else return 0; } } else if(lei==7) { if(yuns==2) { //cout<<lesu<<"=="<<risu<<"=="<<endl; if(strcmp(lesu,risu)==0) return 1; else return 0; } else if(yuns==4) { //cout<<lesu<<"!="<<risu<<"=="<<endl; if(strcmp(lesu,risu)!=0) return 1; else return 0; } } } int compare(tiao bj,char datas[][50],tafot tf) { int shuno=0,shutype=0,no=0; char know[30]={0}; if(bj.le==bj.ri)//类型相同的数据 { if(bj.le!=5) { //if(strcmp(bj.lefts,bj.rights)==0) // return 1; //else // return 0; cout<<"暂不作处理"<<endl; } else { cout<<"两边都是属性名 做连接操作"<<endl; } } else//未知的不是属性就是表属性 { if(bj.le==4||bj.ri==4) { if(bj.le==4) { shuno=soulocation(bj.lefts,tf); shutype=bj.ri; strcpy(know,bj.rights); no=1; } else if(bj.ri==4) { shuno=soulocation(bj.rights,tf); shutype=bj.le; strcpy(know,bj.rights); no=2; } if(shutype==1) { //cout<<datas[shuno]<<"=="<<know<<"==lei="<<shutype<<endl; if(compareType(datas[shuno],know,shutype,bj.type,no)==1) return 1; else return 0; } else if(shutype==2) { //cout<<datas[shuno]<<"=="<<know<<"==lei="<<shutype<<endl; if(compareType(datas[shuno],know,shutype,bj.type,no)==1) return 1; else return 0; } else if(shutype==3) { //cout<<datas[shuno]<<"=="<<know<<"==lei="<<shutype<<endl; if(compareType(datas[shuno],know,shutype,bj.type,no)==1) return 1; else return 0; } else if(shutype==5) { cout<<"遇上了属性和带表的属性 这是做连接?"<<endl; } else if(shutype==6) { //cout<<datas[shuno]<<"=="<<know<<"==lei="<<shutype<<endl; if(compareType(datas[shuno],know,shutype,bj.type,no)==1) return 1; else return 0; } else if(shutype==7) { //cout<<datas[shuno]<<"=="<<know<<"==lei="<<shutype<<endl; if(compareType(datas[shuno],know,shutype,bj.type,no)==1) return 1; else return 0; } } else if(bj.le==5||bj.ri==5) { if(bj.le==5) { shuno=soulocation(bj.lefts,tf); shutype=bj.ri; strcpy(know,bj.rights); no=1; } else if(bj.ri==5) { shuno=soulocation(bj.rights,tf); shutype=bj.le; strcpy(know,bj.rights); no=2; } if(shutype==1) { if(compareType(datas[shuno],know,shutype,bj.type,no)==1) return 1; else return 0; } else if(shutype==2) { if(compareType(datas[shuno],know,shutype,bj.type,no)==1) return 1; else return 0; } else if(shutype==3) { if(compareType(datas[shuno],know,shutype,bj.type,no)==1) return 1; else return 0; } else if(shutype==5) { cout<<"遇上了属性和带表的属性 这是做连接?"<<endl; } else if(shutype==6) { if(compareType(datas[shuno],know,shutype,bj.type,no)==1) return 1; else return 0; } else if(shutype==7) { if(compareType(datas[shuno],know,shutype,bj.type,no)==1) return 1; else return 0; } } else { cout<<"数据错误"<<endl; return 0; } } } void output(tiao taj[],string cpath,int n,char bo[][20],char sn[][20]) { int flag=2,i=0,j=0,k=0,part=0,m=0; int te=0,xiabiao=0; int touy[5]={0}; char data[20][50]={0}; string filename="E:\\codeblocks编程\\test\\"; string wei=".txt"; FILE *fp=fopen(cpath.c_str(),"r"); FILE *ft=NULL; tafot tf={{0},{0},{0},{0},{0},0,{0}}; //从数据字典中找到相应的属性 并记录对应位置 方便输出数据时做映射 fread(&tf,sizeof(struct tableformat),1,fp); while(!feof(fp)) { if(strcmp(tf.belotable,bo[0])==0) { break; } else { fread(&tf,sizeof(struct tableformat),1,fp); } } // cout<<strlen(sn[i])<<endl;//if(sn[i]=="\0") if(strcmp(sn[i],"")!=0) //cout<<"表名"<<tf.belotable<<"属性名"<<tf.bsname[0]<<"属性类型"<<tf.stype[0]<<"属性长度"<<tf.length[0]<<endl; while(strcmp(sn[i],"\0")!=0)//将单表做投影 { while(m<tf.num)//跟取出的数据词典中的每个属性作比较,确定该属性在数据数组中的位置 { if(strcmp(sn[i],tf.bsname[m])==0) { touy[j++]=m+1; break; } m++; } m=0; i++; } m=0; /* cout<<"输出一下投影 "<<touy[0]; while(touy[te]!=0) { cout<<"投影下标"<<touy[te]<<endl; te++; } */ //读取数据环节并作比较 filename=filename+bo[0]+wei; //cout<<filename<<endl; if((ft=fopen(filename.c_str(),"r"))==NULL) cout<<"文件打开失败"<<endl; else { fread(data,sizeof(data),1,ft); //cout<<"数据="<<data[0]<<data[1]<<data[2]<<data[3]<<endl; } while(!feof(ft)) { part=taj[k].part;//每读一次数据都需要和所有的条件进行比较 每次都从第一个条件的分区开始 //cout<<"分区="<<part<<"条件个数"<<n<<endl; while(k<=n)//k<条件个数 { //cout<<"条件 "<<taj[k].lefts<<" 和 "<<taj[k].rights<<endl; if(part==taj[k].part) { if(compare(taj[k],data,tf)==1)//条件成立 { if(flag==1||flag==2) { flag=1; } else //前一个and条件结果是错误的 { flag=0; } } else { flag=0; } k++; } else { part=taj[k].part; if(compare(taj[k],data,tf)==1) { flag=1; } else { if(flag==1) flag=1; else { flag=0; } } k++; } } k=0; if(flag==1)//比较完n个条件后 若flag==1则说明该条记录符合条件 对其在进行投影就可以输出了 { while(touy[m]!=0)//输出投影对应下标的数据 { xiabiao=touy[m]-1; cout<<data[xiabiao]<<" "; m++; } cout<<endl; } m=0;flag=2; fread(data,sizeof(data),1,ft); } fclose(fp); fclose(ft); } void outputjian(string cpath,char bs[][20],char ss[][20]) { int i=0,j=0,m=0,k=0,xia=0; char out[20][50]={0}; int touy[5]={0}; string filenaeme="E:\\codeblocks编程\\test\\"; string wei=".txt"; FILE *fp=NULL; FILE *ft=NULL; tafot tf={{0},{0},{0},{0},{0},0,{0}}; if(strcmp(ss[0],"\0")==0)//不做投影 { filenaeme=filenaeme+bs[0]+wei; if((ft=fopen(filenaeme.c_str(),"r"))==NULL) cout<<"文件打开失败"<<endl; else fread(out,sizeof(out),1,ft); while(!feof(ft)) { for(int i=0;i<20;i++) if(strcmp(out[i],"\0")!=0) cout<<out[i]<<" "; cout<<endl; fread(out,sizeof(out),1,ft); } } else//做投影 { if((fp=fopen(cpath.c_str(),"r"))==NULL) cout<<"文件打开失败"<<endl; else fread(&tf,sizeof(struct tableformat),1,fp); while(!feof(fp)) { if(strcmp(tf.belotable,bs[0])==0) break; else fread(&tf,sizeof(struct tableformat),1,fp); } while(strcmp(ss[i],"\0")!=0)//将单表做投影 { while(m<tf.num)//跟取出的数据词典中的每个属性作比较,确定该属性在数据数组中的位置 { if(strcmp(ss[i],tf.bsname[m])==0) { touy[j++]=m+1; break; } m++; } m=0; i++; } m=0; filenaeme=filenaeme+bs[0]+wei; if((ft=fopen(filenaeme.c_str(),"r"))==NULL) cout<<"文件打开失败"<<endl; else fread(out,sizeof(out),1,ft); while(!feof(ft)) { while(touy[m]!=0)//输出投影对应下标的数据 { xia=touy[m]-1; cout<<out[xia]<<" "; m++; } cout<<endl; m=0; fread(out,sizeof(out),1,ft); } } fclose(ft); fclose(fp); } void outputs(tiao taj[],string cpath,int n,char bo[][20],char sn[][20]) { int i=0,j=0,k=0,flag=2,sign=1,nfile=0,log=1; int n1=0,n2=0,n3=0,n4=0,n5=0; char data[20][50]={0}; fdata da={{0}}; //ega ga={{0},{0},{0},{0},{0}}; dou db={{0},{0}}; vector<dou> result; vector<fdata> fd1; vector<fdata> fd2; vector<fdata> fd3; vector<fdata> fd4; vector<fdata> fd5; string filename="E:\\codeblocks编程\\test\\"; string wei=".txt"; FILE *fcd=NULL; FILE *fp=NULL; FILE *ft=NULL; tafot cidian[5]={{{0},{0},{0},{0},{0},0,{0}}, {{0},{0},{0},{0},{0},0,{0}}, {{0},{0},{0},{0},{0},0,{0}}, {{0},{0},{0},{0},{0},0,{0}}, {{0},{0},{0},{0},{0},0,{0}} }; //vector< vector<char> > b(20, vector<char>(50)); //创建一个10*5的char型二维向量 while(strcmp(bo[nfile],"\0"))//i可以记录文件数 对应着词典数 { filename=filename+bo[nfile]+wei; if((fp=fopen(filename.c_str(),"r"))!=NULL) fread(da.elem,sizeof(da.elem),1,fp); else cout<<"文件打开失败"<<endl; while(!feof(fp))//读完一个文件 换下一个表存 { if(sign==1) fd1.push_back(da); else if(sign==2) fd2.push_back(da); else if(sign==3) fd3.push_back(da); else if(sign==4) fd4.push_back(da); else if(sign==5) fd5.push_back(da); fread(da.elem,sizeof(da.elem),1,fp); } fclose(fp);sign++;nfile++; } //获取多张表的数据字典 if((fcd=fopen(cpath.c_str(),"r"))==NULL) cout<<"文件打开失败"<<endl; while(strcmp(bo[i],"\0")) { fread(&cidian[i],sizeof(struct tableformat),1,fcd); while(feof(fcd)) { if(strcmp(cidian[i].belotable,bo[0])==0) { break; } else { fread(&cidian[i],sizeof(struct tableformat),1,fcd); } } rewind(fcd);i++; } i=0; //连接数据 //memcpy(ga.first,fd1[0].elem,sizeof(char)*20*50); if(nfile==2) { while(n1<fd1.size()) { memcpy(db.dsu,fd1[0].elem,sizeof(char)*20*50); strcpy(db.bioname,bo[i++]); result.push_back(db); } } else if(nfile==3) { } else if(nfile==4) { } else if(nfile==5) { } if(fd1.size()>0) { memcpy(db.dsu,fd1[0].elem,sizeof(char)*20*50); strcpy(db.bioname,bo[i++]); result.push_back(db); } if(fd2.size()>0) { memcpy(db.dsu,fd2[0].elem,sizeof(char)*20*50); strcpy(db.bioname,bo[i++]); result.push_back(db); } if(fd3.size()>0) { memcpy(db.dsu,fd3[0].elem,sizeof(char)*20*50); strcpy(db.bioname,bo[i++]); result.push_back(db); } if(fd4.size()>0) { memcpy(db.dsu,fd4[0].elem,sizeof(char)*20*50); strcpy(db.bioname,bo[i++]); result.push_back(db); } if(fd5.size()>0) { memcpy(db.dsu,fd5[0].elem,sizeof(char)*20*50); strcpy(db.bioname,bo[i++]); result.push_back(db); } fd1.~vector<fdata>(); fd2.~vector<fdata>(); fd3.~vector<fdata>(); fd4.~vector<fdata>(); fd5.~vector<fdata>(); result.~vector<dou>();//析构该结构体vector数组 } int analyzeType(string sts,int p,int q,tiao *con)//分析存在什么内容,然后记录数据类型,再截取数据部分存入条件类型中 { //0---p-1,,预算符,,q-----sts.length()-1 //数字 1 ,小数 2 ,年月日 3 ,字母 4 ,表字母 5 ,英文单词 6 ,汉字 7 int pose=0; int sz=0,xsd=0,zm=0,hx=0,dyh=0; int les=-1,ris=-1,lez=-1,riz=-1,led=-1,rid=-1; int xsdlocation=0; string temp=""; for(int i=0;i<p;i++)//这里仅仅是运算符的左半部分 _______运算符 { if(sts[i]>='0'&&sts[i]<='9') { sz=1; if(les==-1)//记录第一个数字出现的下标 les=i; if(les!=-1)//记录最后一个数字出现的下标 ris=i; } else if(sts[i]>='a'&&sts[i]<='z') { zm=1; if(lez==-1) lez=i; if(lez!=-1) riz=i; } else if(sts[i]=='.') { xsd=1; xsdlocation=i; } else if(sts[i]=='\'') { dyh=1; if(led==-1) led=i; if(led!=-1) rid=i; } else if(sts[i]=='-') { hx=1; } } if(sz==1&&zm==0&&xsd==0&&dyh==0&&hx==0) { con->le=1;//整数 temp=sts.substr(les,ris-les+1); strcpy(con->rights,temp.c_str()); } else if(sz==1&&zm==0&&xsd==1&&dyh==0&&hx==0) { con->le=2;//小数 temp=sts.substr(les,ris-les+1); strcpy(con->rights,temp.c_str()); } else if(sz==1&&zm==0&&xsd==0&&dyh==1&&hx==1) { con->le=3;//年月日 temp=sts.substr(les,ris-les+1); temp=dateDeal(temp);//处理日期横线 strcpy(con->rights,temp.c_str()); } else if(sz==0&&zm==1&&xsd==0&&dyh==0&&hx==0) { con->le=4;//属性名 temp=sts.substr(lez,riz-lez+1); //cout<<lez<<" 两个下标 "<<riz<<" temp="<<temp<<endl; strcpy(con->lefts,temp.c_str()); } else if(sz==0&&zm==1&&xsd==1&&dyh==0&&hx==0) { con->le=5;//带表名的属性 temp=sts.substr(lez,xsdlocation-lez); strcpy(con->rights,temp.c_str()); temp=sts.substr(xsdlocation+1,riz-xsdlocation); strcpy(con->rightbie,temp.c_str()); } else if(sz==0&&zm==1&&xsd==0&&dyh==1&&hx==0) { con->le=6;//英文单词 temp=sts.substr(led+1,rid-led-1); strcpy(con->rights,temp.c_str()); } else if(sz==0&&zm==0&&xsd==0&&dyh==1&&hx==0) { con->le=7;//汉字 temp=sts.substr(led+1,rid-led-1); strcpy(con->rights,temp.c_str()); } sz=0;xsd=0;zm=0;hx=0;dyh=0; les=-1;ris=-1;lez=-1;riz=-1;led=-1;rid=-1; for(int i=q;i<sts.length();i++)//运算符——————————————右边 { if(sts[i]>='0'&&sts[i]<='9') { sz=1; if(les==-1)//记录第一个数字出现的下标 les=i; if(les!=-1)//记录最后一个数字出现的下标 ris=i; } else if(sts[i]>='a'&&sts[i]<='z') { zm=1; if(lez==-1) lez=i; if(lez!=-1) riz=i; } else if(sts[i]=='.') { xsd=1; xsdlocation=i; } else if(sts[i]=='\'') { dyh=1; if(led==-1) led=i; if(led!=-1) rid=i; } else if(sts[i]=='-') { hx=1; } } if(sz==1&&zm==0&&xsd==0&&dyh==0&&hx==0) { con->ri=1;//整数 temp=sts.substr(les,ris-les+1); strcpy(con->rights,temp.c_str()); } else if(sz==1&&zm==0&&xsd==1&&dyh==0&&hx==0) { con->ri=2;//小数 temp=sts.substr(les,ris-les+1); strcpy(con->rights,temp.c_str()); } else if(sz==1&&zm==0&&xsd==0&&dyh==1&&hx==1) { con->ri=3;//年月日 temp=sts.substr(les,ris-les+1); temp=dateDeal(temp);//处理日期横线 strcpy(con->rights,temp.c_str()); } else if(sz==0&&zm==1&&xsd==0&&dyh==0&&hx==0) { con->ri=4;//属性名 temp=sts.substr(lez,riz-lez+1); strcpy(con->rights,temp.c_str()); } else if(sz==0&&zm==1&&xsd==1&&dyh==0&&hx==0) { con->ri=5;//带表名的属性 temp=sts.substr(lez,xsdlocation-lez); strcpy(con->rights,temp.c_str()); temp=sts.substr(xsdlocation+1,riz-xsdlocation); strcpy(con->rightbie,temp.c_str()); } else if(sz==0&&zm==1&&xsd==0&&dyh==1&&hx==0) { con->ri=6;//英文单词 temp=sts.substr(led+1,rid-led-1); strcpy(con->rights,temp.c_str()); } else if(sz==0&&zm==0&&xsd==0&&dyh==1&&hx==0) { con->ri=7;//汉字 temp=sts.substr(led+1,rid-led-1); strcpy(con->rights,temp.c_str()); } } void analyzeCondition(string st,int partlocal,tiao *condi) { int pose=0;//记录出现运算符的位置 if((pose=st.find("!=",0))!=-1) { condi->type=4; condi->part=partlocal; analyzeType(st,pose,pose+2,condi); } else if((pose=st.find("<=",0))!=-1) { condi->type=5; condi->part=partlocal; analyzeType(st,pose,pose+2,condi); } else if((pose=st.find(">=",0))!=-1) { condi->type=6; condi->part=partlocal; analyzeType(st,pose,pose+2,condi); } else if((pose=st.find("<",0))!=-1) { condi->type=1; condi->part=partlocal; analyzeType(st,pose,pose+1,condi); } else if((pose=st.find(">",0))!=-1) { condi->type=3; condi->part=partlocal; analyzeType(st,pose,pose+1,condi); } else if((pose=st.find("=",0))!=-1) { condi->type=2; condi->part=partlocal; analyzeType(st,pose,pose+1,condi); } } void analyze(string s,tiao tj[],int *nm) { int m=0,k=0,part=1; int posetion=0; curent cu[5]={0}; curent linshi; string stemp=""; while(((posetion=s.find("and",posetion))!=-1))//可能一个也没有1找到意味着就一个条件 或者只有一个or { cu[m].loca=posetion; cu[m++].type=1; //cout<<posetion<<endl; posetion++; } posetion=0; while(((posetion=s.find("or",posetion))!=-1)) { cu[m].loca=posetion; cu[m++].type=2; //cout<<posetion<<endl; posetion++; } posetion=0;*nm=m; if(m!=0)//有多个条件 { for(int i=0;i<m;i++) for(int j=0;j<m-i-1;j++) { if(cu[j].loca>cu[j+1].loca) { linshi=cu[j]; cu[j]=cu[j+1]; cu[j+1]=linshi; } } //cout<<"m="<<m<<endl; while(k<m) { while(cu[k].type!=2&&cu[k].type!=0) { //cout<<posetion<<"=当前and位置="<<cu[k].loca<<" 1.1条件字符串"<<stemp<<s.substr(17,20)<<endl; stemp=s.substr(posetion,cu[k].loca-posetion); //cout<<posetion<<"=当前and位置="<<cu[k].loca<<" 1条件字符串"<<stemp<<endl; analyzeCondition(stemp,part,&tj[k]); posetion=cu[k].loca+3; k++; } if(cu[k].type==2) { stemp=s.substr(posetion,cu[k].loca-posetion); //cout<<"当前or位置="<<cu[k].loca<<" 2条件字符串"<<stemp<<endl; analyzeCondition(stemp,part,&tj[k]); posetion=cu[k].loca+2; part++; k++; } //k++; } if(cu[k-1].type==1) { stemp=s.substr(posetion,s.length()-1); //cout<<"3条件字符串"<<stemp<<endl; analyzeCondition(stemp,part,&tj[k]); } else if(cu[k-1].type==2) { part++; stemp=s.substr(posetion,s.length()-1); //cout<<"4条件字符串"<<stemp<<endl; analyzeCondition(stemp,part,&tj[k]); } } else//只有一个条件 { analyzeCondition(s,0,&tj[0]); } } void analyzeTable(string s,char bs[][20]) { int i=0,lez=-1,riz=0,k=0; int posedou=0,predou=0; int doulocat[5]={0}; string sst=""; string stem=""; if((posedou=s.find(",",0))!=-1) { //doulocat[i++]=posedou; for(int j=predou;j<posedou;j++) { if((s[j]>='a'&&s[j]<='z')||s[j]=='_') { if(lez==-1) lez=j; if(lez!=-1) riz=j; } } stem=s.substr(lez,riz-lez+1); strcpy(bs[k++],stem.c_str()); posedou+=1; predou=posedou; lez=-1,riz=0; while((posedou=s.find(",",posedou))!=-1) { //doulocat[i++]=posedou; for(int j=predou;j<posedou;j++) { if((s[j]>='a'&&s[j]<='z')||s[j]=='_') { if(lez==-1) lez=j; if(lez!=-1) riz=j; } } stem=s.substr(lez,riz-lez+1); strcpy(bs[k++],stem.c_str()); posedou+=1; if(posedou!=-1) predou=posedou; lez=-1,riz=0; } for(int j=predou;j<s.length();j++) { if((s[j]>='a'&&s[j]<='z')||s[j]=='_') { if(lez==-1) lez=j; if(lez!=-1) riz=j; } } stem=s.substr(lez,riz-lez+1); strcpy(bs[k++],stem.c_str()); } else { for(int j=0;j<s.length();j++) { if((s[j]>='a'&&s[j]<='z')||s[j]=='_') { if(lez==-1) lez=j; if(lez!=-1) riz=j; } } stem=s.substr(lez,riz-lez+1); strcpy(bs[k++],stem.c_str()); } } void analyzeAlter(string cpath,string sa,char bo[][20],int leix) { int lez=-1,riz=0,sle=-1,sri=-1,kzuo=0,kyou=0,intlocation=0; int flag=0,te=0,xia=0,k=0,m=0; char shutype[10]={0}; char out[20][50]={0}; string temp=""; string filename="E:\\codeblocks编程\\test\\"; string wei=".txt"; string tempfile="E:\\codeblocks编程\\test\\lin.txt"; string tpfile="E:\\codeblocks编程\\test\\interval.txt"; FILE *fp=fopen(cpath.c_str(),"r"); FILE *ft=NULL; FILE *fg=NULL; FILE *fh=NULL; tafot th={{0},{0},{0},{0},{0},0,{0}}; ft=fopen(tempfile.c_str(),"w"); if(fp!=NULL) fread(&th,sizeof(struct tableformat),1,fp); else cout<<"文件打开失败"<<endl; while(!feof(fp)) { if(strcmp(th.belotable,bo[0])==0) { break; } else { fwrite(&th,sizeof(struct tableformat),1,ft); fread(&th,sizeof(struct tableformat),1,fp); } } //fclose(fp); for(int j=0;j<sa.length();j++) { if(sle==-1) { if(sa[j]>='a'&&sa[j]<='z') { if(lez==-1) lez=j; if(lez!=-1) riz=j; } if(riz!=0&&sa[j]==' ')//表示已经读取了一小段字母后 再次遇到空格 sle=0; } else { if(sa[j]>='a'&&sa[j]<='z') { if(sle==0) sle=j; if(sle!=0) sri=j; } } } temp=sa.substr(lez,riz-lez+1); for(int n=0;n<th.num;n++)//查询该属性是否存在 { if(strcmp(temp.c_str(),th.bsname[n])==0) { flag=1; break; } } if(leix==1) { if(flag==1) { cout<<"该列已存在"<<endl; fclose(fp); fclose(ft); if(remove(tempfile.c_str())!=0) cout<<"删除临时文件失败"<<endl; } else { strcpy(th.bsname[th.num],temp.c_str()); temp=sa.substr(sle,sri-sle+1); if((intlocation=sa.find("int",0))!=-1) { strcpy(th.stype[th.num],"int"); } else if((intlocation=sa.find("integer",0))!=-1) { strcpy(th.stype[th.num],"integer"); } else { strcpy(th.stype[th.num],temp.c_str()); kzuo=sa.find("(",0); kyou=sa.find(")",0); if(kzuo!=-1&&kyou!=-1) { temp=sa.substr(kzuo+1,kyou-kzuo-1); th.length[th.num]=atoi(temp.c_str()); } else cout<<"语法错误"<<endl; } th.num++; /* for(te=0;te<th.num;te++) { cout<<"变量 "<<th.bsname[te]<<" 类型"<<th.stype[te]<<endl; } */ while(!feof(fp))//继续将剩余的写入临时文件 { fwrite(&th,sizeof(struct tableformat),1,ft); fread(&th,sizeof(struct tableformat),1,fp); } fclose(fp); fclose(ft); fg=fopen(tempfile.c_str(),"r"); fh=fopen(cpath.c_str(),"w"); if(fg!=NULL) fread(&th,sizeof(struct tableformat),1,fg); else cout<<"文件打开失败"<<endl; while(!feof(fg))//继续将剩余的写入临时文件 { fwrite(&th,sizeof(struct tableformat),1,fh); fread(&th,sizeof(struct tableformat),1,fg); } fclose(fg); fclose(fh); if(remove(tempfile.c_str())!=0) cout<<"删除临时文件失败"<<endl; } } else { if(flag==0) { cout<<"该列不存在"<<endl; fclose(fp); fclose(ft); if(remove(tempfile.c_str())!=0) cout<<"删除临时文件失败"<<endl; } else//需要找到位置 再全部前移 { //cout<<temp<<endl; for(int j=0;j<th.num;j++) { if(strcmp(temp.c_str(),th.bsname[j])==0) { xia=j; break; } } //cout<<"2该列存在,可以删除"<<endl; for(k=xia;k<th.num-1;k++) { strcpy(th.bsname[k],th.bsname[k+1]); strcpy(th.stype[k],th.stype[k+1]); th.length[k]=th.length[k+1]; th.notnull[k]=th.notnull[k+1]; } //cout<<"3该列存在,可以删除"<<endl; //strcpy(th.bsname[k],"\0"); //strcpy(th.stype[k],"\0"); th.length[k]=0; th.notnull[k]=0; th.num--; memset(th.bsname[k],0,sizeof(char)*10); memset(th.stype[k],0,sizeof(char)*10); /* for(te=0;te<th.num;te++){ cout<<"变量 "<<th.bsname[te]<<" 类型"<<th.stype[te]<<endl; } */ while(!feof(fp))//继续将剩余的写入临时文件 { fwrite(&th,sizeof(struct tableformat),1,ft); fread(&th,sizeof(struct tableformat),1,fp); } fclose(fp); fclose(ft); fg=fopen(tempfile.c_str(),"r"); fh=fopen(cpath.c_str(),"w"); if(fg!=NULL) fread(&th,sizeof(struct tableformat),1,fg); else cout<<"文件打开失败"<<endl; while(!feof(fg))//继续将剩余的写入临时文件 { fwrite(&th,sizeof(struct tableformat),1,fh); fread(&th,sizeof(struct tableformat),1,fg); } fclose(fg); fclose(fh); if(remove(tempfile.c_str())!=0) cout<<"删除临时文件失败"<<endl; //再对数据文件经行操作 filename=filename+bo[0]+wei; if((ft=fopen(tpfile.c_str(),"w"))==NULL) cout<<"文件打开失败"<<endl; if((fp=fopen(filename.c_str(),"r"))!=NULL) fread(out,sizeof(out),1,fp); else cout<<"文件打开失败"<<endl; while(!feof(fp))//继续将剩余的写入临时文件 { for(m=xia;m<th.num;m++) strcpy(out[m],out[m+1]); memset(out[m],0,sizeof(char)*50); fwrite(out,sizeof(out),1,ft); fread(out,sizeof(out),1,fp); } //两个文件数据倒一下,再删除多余的 fclose(fp); fclose(ft); fg=fopen(tpfile.c_str(),"r"); fh=fopen(filename.c_str(),"w"); if(fg!=NULL) fread(out,sizeof(out),1,fg); else cout<<"文件打开失败"<<endl; while(!feof(fg))//继续将剩余的写入临时文件 { fwrite(out,sizeof(out),1,fh); fread(out,sizeof(out),1,fg); } fclose(fg); fclose(fh); if(remove(tpfile.c_str())!=0) cout<<"删除临时文件失败"<<endl; } } } void updatedata(string cpath,tiao suta[],tiao uptiao[],char bo[][20],int n) { int i=0,j=0,m=0,k=0,xia=0,part=0; int flag=2,te=0; char data[20][50]={0}; char sn[5][20]={0}; int touy[5]={0}; string filename="E:\\codeblocks编程\\test\\"; string wei=".txt"; string tempfile="E:\\codeblocks编程\\test\\change.txt"; FILE *fp=NULL; FILE *ft=NULL; FILE *fw=fopen(tempfile.c_str(),"w+"); FILE *fg=NULL; FILE *fh=NULL; tafot tf={{0},{0},{0},{0},{0},0,{0}}; while(strcmp(suta[i].lefts,"\0")!=0) { strcpy(sn[i],suta[i].lefts); i++; } i=0; /* while(strcmp(sn[te],"\0")!=0){ cout<<"获取的属性表"<<sn[te]<<"和修改的地方左="<<suta[te].lefts<<"修改的地方右边="<<suta[te].rights<<endl; te++; } te=0; cout<<"条件个数"<<n<<endl; while(strcmp(uptiao[te].lefts,"\0")!=0) { cout<<"判断条件左= "<<uptiao[te].lefts<<" 判断条件右= "<<uptiao[te].rights<<" 条件类型 "<<uptiao[te].type<<endl; te++; } */ if((fp=fopen(cpath.c_str(),"r"))==NULL) cout<<"文件打开失败"<<endl; else fread(&tf,sizeof(struct tableformat),1,fp); while(!feof(fp)) { if(strcmp(tf.belotable,bo[0])==0) { break; } else { fread(&tf,sizeof(struct tableformat),1,fp); } } //cout<<"表名"<<tf.belotable<<"属性名"<<tf.bsname[0]<<"属性类型"<<tf.stype[0]<<"属性长度"<<tf.length[0]<<endl; while(strcmp(sn[i],"\0")!=0)//将单表做投影 { while(m<tf.num)//跟取出的数据词典中的每个属性作比较,确定该属性在数据数组中的位置 { if(strcmp(sn[i],tf.bsname[m])==0) { touy[j++]=m+1; break; } m++; } m=0; i++; } m=0; if(n!=0) { filename=filename+bo[0]+wei; //cout<<filename<<endl; if((ft=fopen(filename.c_str(),"r"))==NULL) cout<<"文件打开失败"<<endl; else { fread(data,sizeof(data),1,ft); //cout<<"数据="<<data[0]<<data[1]<<data[2]<<data[3]<<endl; } while(!feof(ft)) { //fread(data,sizeof(data),1,ft); part=uptiao[k].part;//每读一次数据都需要和所有的条件进行比较 每次都从第一个条件的分区开始 //cout<<"分区="<<part<<"条件个数"<<n<<endl; while(k<=n)//k<条件个数 { if(part==uptiao[k].part) { if(compare(uptiao[k],data,tf)==1)//条件成立 { if(flag==1||flag==2) { flag=1; } else //前一个and条件结果是错误的 { flag=0; } } else { flag=0; } k++; } else { part=uptiao[k].part; if(compare(uptiao[k],data,tf)==1) { flag=1; } else { if(flag==1) flag=1; else { flag=0; } } k++; } } k=0; if(flag==1)//比较完n个条件后 若flag==1则说明该条记录符合条件 对其在进行投影就可以输出了 { while(touy[m]!=0)//输出投影对应下标的数据 { xia=touy[m]-1; strcpy(data[xia],suta[m].rights); m++; } } m=0;flag=2; fwrite(data,sizeof(data),1,fw); fread(data,sizeof(data),1,ft); } fclose(fp); fclose(ft); fclose(fw); //if(ft!=NULL) // cout<<"还有数据存在"<<endl; fg=fopen(filename.c_str(),"w"); fh=fopen(tempfile.c_str(),"r"); fread(data,sizeof(data),1,fh); while(!feof(fh)) { //fread(data,sizeof(data),1,fh); fwrite(data,sizeof(data),1,fg); fread(data,sizeof(data),1,fh); } fclose(fg); fclose(fh); if(remove(tempfile.c_str())!=0) cout<<"删除失败"<<endl; } else if(n==0)//没有条件 { filename=filename+bo[0]+wei; //cout<<filename<<endl; if((ft=fopen(filename.c_str(),"r"))==NULL) cout<<"文件打开失败"<<endl; else { fread(data,sizeof(data),1,ft); //cout<<"数据="<<data[0]<<data[1]<<data[2]<<data[3]<<endl; } while(!feof(ft)) { while(touy[m]!=0)//输出投影对应下标的数据 { xia=touy[m]-1; strcpy(data[xia],suta[m].rights); m++; } m=0; fwrite(data,sizeof(data),1,fw); fread(data,sizeof(data),1,ft); } fclose(fp); fclose(ft); fclose(fw); //if(ft!=NULL) // cout<<"还有数据存在"<<endl; fg=fopen(filename.c_str(),"w"); fh=fopen(tempfile.c_str(),"r"); fread(data,sizeof(data),1,fh); while(!feof(fh)) { //fread(data,sizeof(data),1,fh); fwrite(data,sizeof(data),1,fg); fread(data,sizeof(data),1,fh); } fclose(fg); fclose(fh); if(remove(tempfile.c_str())!=0) cout<<"删除失败"<<endl; } }//可能与用多个指针的问题 或者未读取完毕 void create(char sh)//需要判断表名是否存在 { int leftnum=0;//lefenum记录最外层括号数,nlef记录内层括号,来判断是否成对出现 char ch;ch=sh; int k=0,j=0,i=0;//i是控制记录属性个数 char temp[10]={0}; char biao[20]={0}; char element[10]={0}; char sty[10]={0}; char keys[10]={0}; char isnu[10]={0}; char stypear[4][10]={"int","char","varchar","integer"}; int len=0; FILE *fp=NULL,*fw=NULL; string filenaeme="E:\\codeblocks编程\\test\\"; string wei=".txt"; tafot tf={{0},{0},{0},{0},{0},0,{0}}; while(ch==' ') ch=getchar(); while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) { temp[k++]=ch; ch=getchar(); } turn(temp); if(strlen(temp)!=0) { if(strcmp(temp,"table")==0) { while(ch==' ') ch=getchar(); k=0; while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')||ch=='_')//读取表名 { biao[k++]=ch; ch=getchar(); } turn(biao); if(strlen(biao)!=0);//搜索表不存在的方法 1 打开失败open 2 函数返回值 { filenaeme=filenaeme+biao+wei;//可以将string赋值给数组 if(access(filenaeme.c_str(),F_OK) == -1)//可以添加指定路径查询文件是否存在 0表示存在,-1表示不存在 { fp=fopen(filenaeme.c_str(),"w+"); strcpy(tf.belotable,biao);//记录表格名字 //cout<<"表格名字="<<tf.belotable<<endl; memset(biao,0,sizeof biao);//String.h 中的函数 用于清空数组 while(ch==' ') ch=getchar(); if(ch=='(') { ch=getchar(); while(leftnum!=1)//进入表格元素部分,并且循环识别每个元素 { while(ch==' '||ch==',')// ch=getchar(); while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')||(ch>='0'&&ch<='9'))//获取属性名 { element[j++]=ch; ch=getchar(); } turn(element); j=0;//初始化 方便后续使用 if(strlen(element)!=0);//如果数组不为空 { if(strcmp(element,"primary")==0)//先判断是不是主键 { memset(element,0,sizeof element); while(ch!=')') { while(ch==' ') ch=getchar(); while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) { keys[j++]=ch; ch=getchar(); } turn(keys); j=0;//初始化 方便后续使用 if(strlen(keys)!=0);//如果数组不为空 { if(strcmp(keys,"key")==0) { if(ch='(') { ch=getchar(); while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) { tf.key[j++]=ch; ch=getchar(); } turn(tf.key);j=0; } } else { cout<<"语法错误"<<endl; break; } } //ch=getchar(); } } else//不是主键 { strcpy(tf.bsname[i],element);//记录表格元素 memset(element,0,sizeof element); while(ch==' '||ch==',') ch=getchar(); while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) { sty[j++]=ch; ch=getchar(); } turn(sty);j=0;//初始化 方便后续使用 if(strlen(sty)!=0);//如果元素类型数组不为空 { if(strcmp(sty,"int")==0||strcmp(sty,"integer")==0||strcmp(sty,"float")==0) { strcpy(tf.stype[i],sty);//记录表格元素类型 memset(sty,0,sizeof sty); tf.length[i]=0; while(ch==' ') ch=getchar(); while(ch!=',')//数据是否可以为空 { while(ch==' ') ch=getchar(); while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) { isnu[j++]=ch; ch=getchar(); } } turn(isnu);j=0; if(strlen(isnu)!=0) { if(strcmp(isnu,"notnull")==0) { tf.notnull[i]=1; //cout<<"isnu[]="<<isnu<<endl; memset(isnu,0,sizeof isnu); } else { memset(isnu,0,sizeof isnu); } } i++; } else { strcpy(tf.stype[i],sty);//记录表格元素类型然后置空 memset(sty,0,sizeof sty); if(ch=='(')//记得数字i要加1 如果不是整型后面就会跟括号 { ch=getchar(); while(ch>='0'&&ch<='9') { len=len*10+ch-'0'; ch=getchar(); } if(len!=0) { tf.length[i]=len; len=0; } if(ch==')') { ch=getchar(); while(ch==' ') ch=getchar(); while(ch!=',')//数据是否可以为空 { while(ch==' ') ch=getchar(); while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) { isnu[j++]=ch; ch=getchar(); } } turn(isnu);j=0; if(strlen(isnu)!=0) { if(strcmp(isnu,"notnull")==0) { tf.notnull[i]=1; // cout<<"isnu[]="<<isnu<<endl; memset(isnu,0,sizeof isnu); } else { memset(isnu,0,sizeof isnu); } } i++; } } else { cout<<"sql语言错误1"<<endl; } } } } } if(ch==')')//到达最后一个括号 leftnum=1; ch=getchar(); } tf.num=i; //表不存在 需要将表格式写进数据字典 if((fw=fopen("E:\\codeblocks编程\\test\\shujuzidian.txt","a+"))==false) fw=fopen("E:\\codeblocks编程\\test\\shujuzidian.txt","w+"); fwrite(&tf,sizeof(struct tableformat),1,fw); // cout<<"执行文件存取操作"<<endl; } } else { cout<<"表已存在"<<endl; memset(biao,0,sizeof biao); } } } else { cout<<"输入sql语句错误2"<<endl; } } fclose(fw); fclose(fp); // return; } void drop(char sh,string cpath) { int k=0; char ch=sh; char temp[10]={0}; char biao[20]={0}; string filenaeme="E:\\codeblocks编程\\test\\"; string wei=".txt"; string filenaeme1="E:\\codeblocks编程\\test\\temp.txt"; FILE *fp=NULL; FILE *ft=NULL; FILE *fg=NULL; FILE *fh=NULL; tafot ts={{0},{0},{0},{0},{0},0,{0}}; while(ch==' ') ch=getchar(); while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')||ch=='_') { temp[k++]=ch; ch=getchar(); } turn(temp); if(strlen(temp)!=0) { if(strcmp(temp,"table")==0) { k=0; while(ch==' ') ch=getchar(); while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) { biao[k++]=ch; ch=getchar(); } turn(biao); if(strlen(biao)!=0); { filenaeme=filenaeme+biao+wei;//确定表的路径 //cout<<access(filenaeme.c_str(),F_OK)<<endl; if(access(filenaeme.c_str(),F_OK) == 0) { if(remove(filenaeme.c_str())==0)//除此之外还能用unlink删除指定路径文件,若果有调用,则等调用完关闭后删除 cout<<"删除成功"<<endl; else{cout<<"删除失败"<<endl;} ft=fopen(filenaeme1.c_str(),"w");//创建临时字典 fp=fopen(cpath.c_str(),"r");//打开数据字典 fread(&ts,sizeof(struct tableformat),1,fp);//注意文件指针移动顺序,会不会出现多读取一次 while(!feof(fp)) { if(strcmp(biao,ts.belotable)!=0) { //cout<<biao<<"和"<<ts.belotable<<"和"<<strcmp(biao,ts.belotable)<<endl; fwrite(&ts,sizeof(struct tableformat),1,ft); } fread(&ts,sizeof(struct tableformat),1,fp); } fclose(fp); fclose(ft); fg=fopen(cpath.c_str(),"w"); fh=fopen(filenaeme1.c_str(),"r"); fread(&ts,sizeof(struct tableformat),1,fh); while(!feof(fh)) { fwrite(&ts,sizeof(struct tableformat),1,fg); fread(&ts,sizeof(struct tableformat),1,fh); } fclose(fg); fclose(fh); if(remove(filenaeme1.c_str())!=0) cout<<"删除失败"<<endl; /* if(remove(cpath.c_str())==0); cout<<"删除原数据字典"<<endl; if(rename(filenaeme1.c_str(),cpath.c_str())==-1);//oldname newname cout<<"重命名失败"<<endl; */ } else { cout<<"该表不存在"<<biao<<"和"<<filenaeme<<endl; } } } } //return; } void inserts(char sh,string cpath) { char ch=sh; char biao[20]={0}; char value[8]={0}; char data[10][16]={0}; int k=0,j=0,i=0; char temp[10]={0}; string filenaeme="E:\\codeblocks编程\\test\\"; string wei=".txt"; FILE *fp=fopen(cpath.c_str(),"r");//数据字典 FILE *ft=NULL; tafot tg={{0},{0},{0},{0},{0},0,{0}}; char as[20][50]={0}; char zf[256]={0}; char *p=NULL; while(ch==' ') ch=getchar(); while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) { temp[k++]=ch; ch=getchar(); } turn(temp); if(strlen(temp)!=0) { if(strcmp(temp,"into")==0) { k=0; while(ch==' ') ch=getchar(); while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')||ch=='_') { biao[k++]=ch; ch=getchar(); } turn(biao); if(strlen(biao)!=0); { filenaeme=filenaeme+biao+wei;//确定表的路径 if(access(filenaeme.c_str(),F_OK) == 0) { fread(&tg,sizeof(struct tableformat),1,fp);//获取数据类型 while(!feof(fp)) { if(strcmp(biao,tg.belotable)==0) break; else { fread(&tg,sizeof(struct tableformat),1,fp); } } //cout<<"从数据字典读出的表名"<<tg.belotable<<endl; //for(int n=0;n<tg.num;n++) //cout<<n<<"个属性长度="<<tg.length[n]<<endl; while(ch==' ') ch=getchar(); while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) { value[j++]=ch; ch=getchar(); } turn(value);j=0; if(strcmp(value,"values")==0); { while(ch==' ') ch=getchar(); if(ch=='(') { gets(zf); p=strtok(zf,"', )"); while(p!=NULL)//注意程序结尾时指针p可能为空了 这时候任何出现关于p的操作都可能崩溃 { //p=strtok(NULL,"', )"); if(sou(p)==0) { // cout<<"2此时的i="<<i<<"和"<<strlen(p)<<"长度定义"<<tg.length[i]<<"和"<<p<<endl; if(tg.length[i]>=strlen(p)) { strcpy(as[i],p); i++; } else { cout<<"长度超出定义"<<endl; } } else { strcpy(as[i],p); i++; } p=strtok(NULL,"', )"); } } if((ft=fopen(filenaeme.c_str(),"a+"))!=NULL) fwrite(as,sizeof(as),1,ft); //fwrite(as,(sizeof(char)*20*50),1,ft); else { cout<<"文件打开失败"<<endl; } } } else { cout<<"表不存在"<<endl; } } } } else { cout<<"语法错误"<<endl; } free(p); fclose(fp); fclose(ft); } void selectc(string cpath) { int posexing=0,posefrom=0,posewhere=0; int num=0; char s[200]={0}; string str=""; string sshu=""; string sbiao=""; string stiao=""; char biao[6][20]={0}; char su[5][20]={0};//属性集合 tiao tj[5]={{0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}} }; gets(s); str=s; if((posexing=str.find("*",0))!=-1) { if((posefrom=str.find("from",0))!=-1) { if((posewhere=str.find("where",0))!=-1) { cout<<" * from where "<<endl; } else//* from table没有条件 { sbiao=str.substr(posefrom+4,str.length()-posefrom-4); //cout<<"sbiao= "<<sbiao<<endl; analyzeTable(sbiao,biao); outputjian(cpath,biao,su); } } else { cout<<" 语法错误 "<<endl; } } else { if((posefrom=str.find("from",0))!=-1) { if((posewhere=str.find("where",0))!=-1) { sshu=str.substr(0,posefrom-1); sbiao=str.substr(posefrom+4,posewhere-posefrom-4); stiao=str.substr(posewhere+5,str.length()-posewhere-4); //cout<<"sshu= "<<sshu<<endl; //cout<<"sbiao= "<<sbiao<<endl; //cout<<"stiao= "<<stiao<<endl; analyzeTable(sshu,su); analyzeTable(sbiao,biao); analyze(stiao,tj,&num); output(tj,cpath,num,biao,su); } else//不带条件的 { sshu=str.substr(0,posefrom-1); sbiao=str.substr(posefrom+4,str.length()-posefrom-4); //cout<<"sshu= "<<sshu<<endl; //cout<<"sbiao= "<<sbiao<<endl; analyzeTable(sshu,su); analyzeTable(sbiao,biao); outputjian(cpath,biao,su); } } else { cout<<" 语法错误 "<<endl; } } } void update(string cpath) { int i=0,j=0; int number=0; int poseset=0,posewere=0,posed=0,pred=0; char s[200]={0}; string str=""; string temp=""; string temp1=""; string stiao=""; string sdtiao=""; char shu[][20]={0}; char biao[2][20]={0}; tiao sta[5]={{0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}} }; tiao uptiao[5]={{0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}} }; gets(s); str=s; if((poseset=str.find("set",0))!=-1) { //这是表的部分 temp=str.substr(0,poseset-1); cout<<"这是表的部分="<<temp<<endl; analyzeTable(temp,biao); //修改的部分 if((posewere=str.find("where",0))!=-1) { temp=str.substr(poseset+3,posewere-poseset-3); cout<<"这是修改的部分="<<temp<<endl; if((posed=temp.find(",",0))!=-1) { temp1=temp.substr(pred,posed-1); cout<<"修改的xiaoxiao部分="<<temp1<<endl; analyzeCondition(temp1,1,&sta[i]); posed++; pred=posed;i++; while((posed=temp.find(",",posed))!=-1) { temp1=temp.substr(pred,posed-1); cout<<"修改的xiaoxiao部分="<<temp1<<endl; analyzeCondition(temp1,1,&sta[i]); pred=++posed;i++; } temp1=temp.substr(pred,temp.length()-1); cout<<"最后一部分="<<temp1<<endl; analyzeCondition(temp1,1,&sta[i]); } else//只有一条需要修改 { analyzeCondition(temp,1,&sta[0]); cout<<"只有一条需要修改"<<temp<<endl; } //条件部分 stiao=str.substr(posewere+5,str.length()-posewere-4); cout<<"条件部分"<<stiao<<endl; analyze(stiao,uptiao,&number); updatedata(cpath,sta,uptiao,biao,number); } else//没有条件 每一条记录都要修改 { temp=str.substr(poseset+3,posewere-poseset-3); if((posed=temp.find(",",0))!=-1) { temp1=temp.substr(pred,posed-1); analyzeCondition(temp1,1,&sta[i]); pred=++posed;i++; while((posed=temp.find(",",posed))!=-1) { temp1=temp.substr(pred,posed-1); analyzeCondition(temp1,1,&sta[i]); pred=++posed;i++; } temp1=temp.substr(pred,temp.length()-1); analyzeCondition(temp1,1,&sta[i]); } else//只有一条需要修改 { analyzeCondition(temp,1,&sta[0]); } updatedata(cpath,sta,uptiao,biao,0); } } else { cout<<"语法错误"<<endl; } } void alter(string cpath) { char st[200]={0}; char biao[2][20]={0}; string ss=""; int posetable=0,poseadd=0,posedrop=0,lez=0,riy=0; string sbiao="",salter=""; gets(st); ss=st; if((posetable=ss.find("table",0))!=-1) { if((poseadd=ss.find("add",0))!=-1) { sbiao=ss.substr(posetable+5,poseadd-posetable-5); analyzeTable(sbiao,biao); salter=ss.substr(poseadd+3,ss.length()-poseadd-3); //cout<<salter<<endl; analyzeAlter(cpath,salter,biao,1); } else if((posedrop=ss.find("drop",0))!=-1) { sbiao=ss.substr(posetable+5,posedrop-posetable-5); analyzeTable(sbiao,biao); salter=ss.substr(posedrop+4,ss.length()-posedrop-4); //cout<<salter<<endl; analyzeAlter(cpath,salter,biao,2); } else { cout<<"语法错误"<<endl; } } else { cout<<"语法错误"<<endl; } } void deletes(string cpath) { char str[200]={0}; char biao[2][20]={0}; string ss=""; string stemp=""; string filename="E:\\codeblocks编程\\test\\"; string wei=".txt"; int posewhere=0,posefrom=0; int i=0,j=0,m=0,k=0,nu=0,flag=2,part=0; char data[20][50]={0}; string tempfile="E:\\codeblocks编程\\test\\change.txt"; FILE *fp=NULL; FILE *ft=NULL; FILE *fw=NULL; FILE *fg=NULL; FILE *fh=NULL; tiao uptiao[5]={{0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}}, {0,0,0,{0},{0},0,{0},{0}} }; tafot tf={{0},{0},{0},{0},{0},0,{0}}; gets(str); ss=str; if((posefrom=ss.find("from",0))!=-1) { if((posewhere=ss.find("where",0))!=-1) { stemp=ss.substr(posefrom+4,posewhere-posefrom-4); analyzeTable(stemp,biao); stemp=ss.substr(posewhere+5,ss.length()-posewhere-4); analyze(stemp,uptiao,&nu); if((fp=fopen(cpath.c_str(),"r"))==NULL) cout<<"1文件打开失败"<<endl; else fread(&tf,sizeof(struct tableformat),1,fp); while(!feof(fp)) { if(strcmp(tf.belotable,biao[0])==0) { break; } else { fread(&tf,sizeof(struct tableformat),1,fp); } } if((fw=fopen(tempfile.c_str(),"w+"))==NULL) cout<<"2文件打开失败"<<endl; filename=filename+biao[0]+wei; //cout<<filename<<endl; if((ft=fopen(filename.c_str(),"r"))==NULL) cout<<"2文件打开失败"<<endl; else { fread(data,sizeof(data),1,ft); } while(!feof(ft)) { //fread(data,sizeof(data),1,ft); part=uptiao[k].part;//每读一次数据都需要和所有的条件进行比较 每次都从第一个条件的分区开始 //cout<<"分区="<<part<<"条件个数"<<n<<endl; while(k<=nu)//k<条件个数 { if(part==uptiao[k].part) { if(compare(uptiao[k],data,tf)==1)//条件成立 { if(flag==1||flag==2) { flag=1; } else //前一个and条件结果是错误的 { flag=0; } } else { flag=0; } k++; } else { part=uptiao[k].part; if(compare(uptiao[k],data,tf)==1) { flag=1; } else { if(flag==1) flag=1; else { flag=0; } } k++; } } k=0; if(flag==1)//比较完n个条件后 若flag==1则说明该条记录符合条件 继续读,不写入该记录 { fread(data,sizeof(data),1,ft); } else { fwrite(data,sizeof(data),1,fw); fread(data,sizeof(data),1,ft); } flag=2; } fclose(fp); fclose(ft); fclose(fw); //if(ft!=NULL) // cout<<"还有数据存在"<<endl; fg=fopen(filename.c_str(),"w"); fh=fopen(tempfile.c_str(),"r"); fread(data,sizeof(data),1,fh); while(!feof(fh)) { //fread(data,sizeof(data),1,fh); fwrite(data,sizeof(data),1,fg); fread(data,sizeof(data),1,fh); } fclose(fg); fclose(fh); if(remove(tempfile.c_str())!=0) cout<<"删除失败"<<endl; } else//没有条件 { stemp=ss.substr(posefrom+4,ss.length()-posefrom-3); analyzeTable(stemp,biao); filename=filename+biao[0]+wei; if((fp=fopen(filename.c_str(),"w"))==NULL) cout<<"3文件打开失败"<<endl; fclose(fp); fclose(ft); fclose(fw); fclose(fg); fclose(fh); } } else { cout<<"语法错误"<<endl; } } main()//跳出循环可用break,或者某一层循环continue,跳出子函数用return 无返回值也可以使用 { int k=0,n; char temp[10]={0}; char ch; //char sqlstr[8][10]={"create","table","select","from","where","insert","update","delete"}; char sqlstr2[5][10]={"and","or","in","not","exits"}; string cidianpath="E:\\codeblocks编程\\test\\shujuzidian.txt"; if(access("test",F_OK) == -1)//创建根目录 如果不存在,就创建 除此之外 可用system调用dos创建文件夹system("md E:\\mydir"); mkdir("test"); //rmdir("test"); cout<<"请输入sql语句\n"<<endl; while(1) { while((ch=getchar())!='\n') { while(ch==' ') ch=getchar(); while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) { temp[k++]=ch; ch=getchar(); } k=0; if((n=strlen(temp))!=0);//如果数组不为空 { turn(temp); if(strcmp(temp,"create")==0) { // cout<<"创造表格"<<endl; memset(temp,0,sizeof temp); create(ch); //cout<<"执行创建"<<endl; } else if(strcmp(temp,"drop")==0) { memset(temp,0,sizeof temp); drop(ch,cidianpath); //cout<<"执行删除"<<endl; } else if(strcmp(temp,"insert")==0) { //cout<<"执行插入"<<temp<<endl; memset(temp,0,sizeof temp); inserts(ch,cidianpath); // cout<<"执行插入"<<endl; } else if(strcmp(temp,"select")==0) { memset(temp,0,sizeof temp); selectc(cidianpath); //cout<<"执行查询"<<endl; } else if(strcmp(temp,"update")==0) { memset(temp,0,sizeof temp); update(cidianpath); } else if(strcmp(temp,"alter")==0) { memset(temp,0,sizeof temp); alter(cidianpath); } else if(strcmp(temp,"delete")==0) { memset(temp,0,sizeof temp); deletes(cidianpath); } } } cout<<"请输入sql语句\n"<<endl; } }