• 设计 表结构,属性和关键字,写入记录
  • 设计表的存储结构,方便使用文件系统进行写入和读取
  • 对输入的字符串做处理,可根据关键字或正则获取属性,表名,条件部分。
  • 每一次查询,或修改操作时都根据字符串中的表名判断该表是否存在

查询

  • 对条件进行处理,截取单个条件,分字母,数字,日期,字符串等情况进行对比,对比又根
    据运算符划分多个不同情况。
  • 读取记录,将对应属性名的值取出,属性的定位可以根据数据字典,进行条件判断
  • 单个条件的boolean值根据连接关键字and还是or进行最后组合,判断该条记录是否符合。and
    连接部分可当作小分区,or可当作大分区。or分区的值由and小分区的值决定,最终boolean
    值由or大分区的值决定
  • 符合条件的记录,根据sql字符串中的属性进行映射,最后输出结果

除此之外:还有修改,删除,插入操作,插入会对插入的属性做完整性检查,与数据字典存储的结构进行匹配。

功能还不够完善,部分情况未做处理

#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;elseflag=0;//cout<<"数字"<<t[i]<<endl;}}if(flag==1&&n==0)return 1;elsereturn 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;elsereturn 0;}else if(yuns==2){if(le==ri)return 1;elsereturn 0;}else if(yuns==3){if(le>ri)return 1;elsereturn 0;}else if(yuns==4){if(le!=ri)return 1;elsereturn 0;}else if(yuns==5){if(le<=ri)return 1;elsereturn 0;}else if(yuns==6){if(le>=ri)return 1;elsereturn 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;elsereturn 1;}else if(yuns==2){if(le==ri)return 1;elsereturn 0;}else if(yuns==3){if(le>ri)return 0;elsereturn 1;}else if(yuns==4){if(le!=ri)return 1;elsereturn 0;}else if(yuns==5){if(le<=ri)return 0;elsereturn 1;}else if(yuns==6){if(le>=ri)return 0;elsereturn 1;}}else if(lei==5){cout<<"需要传两个表的相应值"<<endl;}else if(lei==6){if(yuns==2){if(strcmp(lesu,risu)==0)return 1;elsereturn 0;}else if(yuns==4){if(strcmp(lesu,risu)!=0)return 1;elsereturn 0;}}else if(lei==7){if(yuns==2){//cout<<lesu<<"=="<<risu<<"=="<<endl;if(strcmp(lesu,risu)==0)return 1;elsereturn 0;}else if(yuns==4){//cout<<lesu<<"!="<<risu<<"=="<<endl;if(strcmp(lesu,risu)!=0)return 1;elsereturn 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;elsereturn 0;}else if(shutype==2){//cout<<datas[shuno]<<"=="<<know<<"==lei="<<shutype<<endl;if(compareType(datas[shuno],know,shutype,bj.type,no)==1)return 1;elsereturn 0;}else if(shutype==3){//cout<<datas[shuno]<<"=="<<know<<"==lei="<<shutype<<endl;if(compareType(datas[shuno],know,shutype,bj.type,no)==1)return 1;elsereturn 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;elsereturn 0;}else if(shutype==7){//cout<<datas[shuno]<<"=="<<know<<"==lei="<<shutype<<endl;if(compareType(datas[shuno],know,shutype,bj.type,no)==1)return 1;elsereturn 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;elsereturn 0;}else if(shutype==2){if(compareType(datas[shuno],know,shutype,bj.type,no)==1)return 1;elsereturn 0;}else if(shutype==3){if(compareType(datas[shuno],know,shutype,bj.type,no)==1)return 1;elsereturn 0;}else if(shutype==5){cout<<"遇上了属性和带表的属性 这是做连接?"<<endl;}else if(shutype==6){if(compareType(datas[shuno],know,shutype,bj.type,no)==1)return 1;elsereturn 0;}else if(shutype==7){if(compareType(datas[shuno],know,shutype,bj.type,no)==1)return 1;elsereturn 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;elsefread(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;elsefread(&tf,sizeof(struct tableformat),1,fp);while(!feof(fp)){if(strcmp(tf.belotable,bs[0])==0)break;elsefread(&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;elsefread(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);elsecout<<"文件打开失败"<<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 ,汉字 7int 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);elsecout<<"文件打开失败"<<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());}elsecout<<"语法错误"<<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);elsecout<<"文件打开失败"<<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);elsecout<<"文件打开失败"<<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);elsecout<<"文件打开失败"<<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);elsecout<<"文件打开失败"<<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;elsefread(&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  newnamecout<<"重命名失败"<<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;elsefread(&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;}}

C语言使用文件系统简单模拟数据库相关推荐

  1. c语言模拟自动饮料机,C语言--小程序--简单模拟ATM机

    原标题:C语言--小程序--简单模拟ATM机 ATM.H #ifndef ATM_h #define ATM_h #include #include #include typedef struct _ ...

  2. CCF CSP 201609-2 火车购票(C++语言100分)[简单模拟题]

    1. 问题链接:CCF 201609-2 火车购票 试题编号: 201609-2 试题名称: 火车购票 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 请实现一个铁路购票系统的 ...

  3. Go语言从0到1实现最简单的数据库!

    导语 | 后台开发对于数据库操作是必不可少的事情,了解数据库原理对于平常的工作的内功积累还是很有帮助的,这里实现一个最简单的数据库加深自己对数据库的理解. 一.go实现数据库目的 了解数据是如何在内存 ...

  4. 银行业务队列简单模拟 (25 分)c语言c++

    7-2 银行业务队列简单模拟 (25 分) 设某银行有A.B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 -- 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客.给定到达银 ...

  5. c语言简单的atm机编程,C语言ATM机的简单模拟.doc

    PAGE PAGE 10 程序设计实习 -ATM机的简单模拟 院系:信息科学与技术学院 专业:计算机科学与技术 班级:A0611 学号:33号 姓名:石晓珍 ATM机的简单模拟 程序的具体功能: 用C ...

  6. 简单文件数据库-模拟图书馆管理系统

    题目要求 编写一个程序模拟图书管理系统.用户分为管理员和读者两类,分别显示不同文本格式菜单,通过菜单项对应数字进行选择.读者菜单包括借书.还书.查询等功能.管理员菜单包括图书和读者信息录入.修改和删除 ...

  7. 【R语言】Splatter,一个用于简单模拟单细胞RNA测序数据的R包

    Splatter是一个用于模拟单细胞RNA测序数据的R包,本文概述并介绍Splatter的功能 一.参数功能 名称 功能 说明 可以通过splatEstimate函数估计 备注 nGenes -> ...

  8. C语言编译抛骰子,简单模拟投掷骰子

    已结贴√ 问题点数:20 回复次数:6 简单模拟投掷骰子 #include #include #include enum Status { CONTINUE, WON, LOST };  // enu ...

  9. 自己实现一个简单的数据库

    How do you build a database? (self.Database) How do you build a database? (self.Database) Its a grea ...

最新文章

  1. Silverlight 3.0 Beta版 正式发布
  2. Notes-stringr-part2
  3. 【每周CV论文】深度学习文本检测与识别入门必读文章
  4. mnist 数据集的识别源码解析
  5. 北京师范大学计算机应用基础考试,北京师范大学-计算机应用基础作业(一至九全套)...
  6. OpenWrt——satisfy_dependencies_for: Cannot satisfy the following dependencies
  7. Filter,FilterChain,FilterConfig
  8. Dockerfile 使用 ARG 参数实现构建模板
  9. 人工智障学习笔记——机器学习(12)LDA降维
  10. js使用在vue中Date来计算日期,当前日期前多少天的日期 两个日期相减或相加等
  11. java 泛型 类型实参(type argument)
  12. Eclipse中移除未使用的类引用的三种办法
  13. POJ 3264:Balanced Lineup(RMQ模板题)
  14. c++实现高斯滤波器
  15. A.7链表练习题——集合的交差并
  16. 在html中在tools作用,图层蒙版的作用是什么
  17. 让工作快乐起来500强企业推崇的新理念
  18. 网易互联网雷火互娱22届校招及社招内推
  19. php twig中文手册,使用技巧 · Twig 中文文档 · 看云
  20. C++基本数据类型的字节数、范围大小、溢出处理

热门文章

  1. EasyDSS点播服务分享时间出错如何修改?
  2. windows快捷键(转)
  3. xp系统服务器找不到打印机无法连接失败,Win7连接Windows XP网络打印机失败?windows无法连接到打印机拒绝访问解决方法...
  4. IOS 的z-index 失效的原因
  5. 触控科技陈昊芝:捕鱼达人装机量1亿 月活跃用户3246万
  6. JDA人脸检测算法详解
  7. 《文明之光 第一册》导读
  8. js编码java解码_常用的js、java编码解码方法
  9. Android 获取手机语言环境 区分简体和繁体,香港,澳门,台湾繁体
  10. 基于java校园食堂综合评价系统