//选择一种高级语言实现下列语句的功能。
//CREATE TABLE <表名>(<列名><数据类型>[<列完整性约束条件>][,<列名><数据//类型>[<列完整性约束条件>]…][,<表完整性约束条件>] )//ALTER TABLE <表名> [ADD <新列名><数据类型>[<列完整性约束>]] [DROP<列完整//性约束名>][MODIFY <列名><数据类型>]//使用说明//1、将程序文件table.sql放在D盘根目录下。
//2、在C盘根目录下建立一个名为"数据库"的文件夹,用于存储表。
//3、建立的表存储路径为C:\数据库:\table.dbf。
//4、在程序文件table.sql中只有一条建表语句和三条修改表语句,在以程序方式执//行时注意执行的次数,慎重选择“是否继续执行”。
//5、程序输入的SQL语句格式如下://create table student//(//SNO int PRIMARY KEY,//SNAME char(10) UNIQUE,//SAGE int,//SDEPT char(20) NOT NULL,//COURSE char(20),//GRADE int//);//alter table student add CNO int NOT NULL;//alter table student alter column SAGE short;//alter table student drop SDEPT;#include<stdio.h>#include<stdlib.h>#include<string.h>#include<conio.h>//宏定义#define YEAR 0#define MONTH 1#define DAY 2#define FOX_VERISON_INFO 262#define MAX 40//字段类型#define DATE 0x44#define DOUBLE 0x45#define FLOAT 0x46#define SHORT 0x47#define INT 0x48#define TRUE 1#define FALSE 0//文件头结构体定义struct DbfHead{char dbFlag;char year;char month;char day;int recCounts;short firstRecAddr;short recLen;char undo[20];};typedef struct DbfHead DbfHead, *pDbfHead; //字段描述结构体定义struct FieldDcp{char fieldName[10];char undo1;char fieldType;short offset;char undo2[2];char fieldLen;char numDecis;char undo3[14];};typedef struct FieldDcp FieldDcp, *pFieldDcp; //字段数据结构体定义struct DbfField{char *fieldData;char fieldName[10];char fieldType;char fieldLen;char fieldDcis;short offset;};typedef struct DbfField DbfField, *pDbfField; //记录结构体定义struct DbfRecchar delFlag;DbfField field[MAX];int realCounts;};typedef struct DbfRec DbfRec, *pDbfRec;//dbf文件句柄定义struct DbfHand{char filename[50];DbfHead header;DbfRec rec;long curRecNo;long curFpAddr;FILE *fd;};typedef struct DbfHand DbfHand, *pDbfHand;//约束条件typedef struct Condition{int flag;//flag 用于区分约束条件(PARIMARY KEY 1 UNIQUE 2 NOT NULL 3) }Condition;pDbfHand f;DbfRec rec;char table_name[20];char sql[300],sql1[300];char GetDate(int getMode)//获取日期{if( DAY ==getMode)return 01;if(MONTH == getMode)return 06;if(YEAR == getMode)return 10;elseexit(0);}short GetHeadLength(pDbfRec rec)//获得文件头长度{return rec->realCounts*32+32+2;}short GetRecLength(pDbfRec rec)//获得文件体长度register int i=0;int count=0;for(i=0;i<rec->realCounts;i++){if(rec->field[i].fieldType=='D'){count+=8;continue;}else if(rec->field[i].fieldType=='I'){count+=4;continue;}else if(rec->field[i].fieldType=='T'){count+=6;continue;}count+=rec->field[i].fieldLen;}return count+1;}pDbfHand NewDbfHead(pDbfHand hand, pDbfRec rec)//新建文件头{int ret = -1;hand-> header.dbFlag = 0x03;hand-> header.day = GetDate(DAY);hand-> header.month = GetDate(MONTH);hand-> header.year = GetDate(YEAR);hand-> header.recLen = GetRecLength(rec);hand-> header.recCounts = 0;hand-> header.firstRecAddr = GetHeadLength(rec);if (0!= fseek(hand-> fd, 0, SEEK_SET))return NULL;ret = fwrite((char*)&hand->header,sizeof(DbfHead) , 1 , hand->fd);if(ret != -1)return hand;elsereturn NULL;}pDbfHand UpdateHead(pDbfHand hand)//更新文件头{if (0!= fseek(hand-> fd, 0, SEEK_SET))return NULL;if (-1== fwrite((char *)&hand-> header, sizeof(DbfHead) , 1 , hand->fd)) return NULL;fclose(hand->fd);return hand;}pDbfHand WriteFieldDicsribe(pDbfHand hand, pDbfRec rec)//写字段描述部分{int i = 0;FieldDcp field={0};field.offset = 0x01;for ( i = 0; i < rec-> realCounts; i++){field.fieldType = rec-> field[i].fieldType;field.numDecis = 0;switch ( field.fieldType){case DOUBLE:field.fieldLen = 8;break;case FLOAT:field.fieldLen = 4;break;case INT:field.fieldLen = 4;break;case SHORT:field.fieldLen = 2;break;default:field.fieldLen = rec-> field[i].fieldLen;break;}strcpy(field.fieldName, rec-> field[i].fieldName);field.offset +=( short)rec-> field[i].fieldLen;if(-1 ==fwrite((char*)&field, sizeof(FieldDcp) , 1 , hand->fd))return NULL;}hand-> rec.delFlag = 0x20;hand-> rec.realCounts = rec-> realCounts;for (i = 0; i < rec-> realCounts; i++ )hand-> rec.field[i] = rec-> field[i];return hand;void WriteFieldEnd(pDbfHand hand)//写文件头结束标志{int i = 0;char buf[2] = {0x0D, 0x00};char versionBuf[FOX_VERISON_INFO] = {0};if (0 == fseek(hand-> fd, 0, SEEK_END)){if (-1== fwrite(buf, 2 , 1 , hand->fd))return;}return ;}pDbfHand ReadDbfHead(pDbfHand hand)//读文件头信息{int i=0,j=0;if (-1 != (fseek(hand-> fd, 0, SEEK_SET))){if (0 != fread((char*)&hand-> header, 32 , 1 , hand->fd))return hand;}return NULL;}int GetFieldCount(pDbfHand hand)//得到字段个数{int i = 0;int offset = 1;FieldDcp field ={0};char cEnd = 0;for (i=0;i<MAX;i++){memset((char *)&field,0,sizeof(FieldDcp));if ( -1 == fseek(hand-> fd, (i+1)*32, SEEK_SET))return -1;if ( 0 != fread((char*)&field, 32 , 1 , hand->fd)){fread(&cEnd,1 , 1 , hand->fd);if (0x0D == cEnd)return (i + 1);}}return -1;}pDbfHand ReadFieldDiscribe(pDbfHand hand)//读字段描述信息int i = 0,j=0;int fieldCount = GetFieldCount(hand);hand-> rec.realCounts = fieldCount;for ( i = 0; i < hand-> rec.realCounts; i++){FieldDcp field = {0};if (-1 == fseek(hand-> fd, (i+1)*32, SEEK_SET))return NULL;if (-1 == fread((char*)&field, 32 , 1 , hand->fd))return NULL;memcpy(hand-> rec.field[i].fieldName,field.fieldName,sizeof(field.fieldName));hand-> rec.field[i].fieldLen = field.fieldLen;hand-> rec.field[i].fieldType = field.fieldType;hand-> rec.field[i].fieldDcis = field.numDecis;hand-> rec.field[i].offset = field.offset;}for(i=0;i<hand->rec.realCounts;i++){for(j=0;hand->rec.field[i].fieldName[j]!='\0';j++){hand->rec.field[i].fieldName[j]=tolower(hand->rec.field[i].fieldName[j]);}}return hand;}char *SaveDate( char *str)//保存内容为日期时,格式华{int i=0, j=0;char temp[9]={0};if (strlen(str) != 10) //日期格式输入不合法return NULL;for ( i=0, j=0; i <10; i++ ){if ( i == 4 || i ==7 )continue;else{if(*(str+i) >= '0' && *(str+i) <= '9'){temp[j]=*(str+i);j++;}elsereturn NULL;}}str=NULL;str=( char *)realloc(str, 8);memset(str, 0x00, 8);memcpy(str, temp, 8);return str;}int WriteRecord(pDbfHand hand, pDbfRec record)//在当前位置写一条纪录,覆盖原有内容{int i = 0;char *buf = NULL;int curFieldLen = 0;int actDataLen = 0;int nPos = 1;int nFiledCount = 0;int rest = TRUE;nFiledCount = hand-> rec.realCounts;buf = ( char*)malloc(hand-> header.recLen + 1 );memset(buf,0x20,hand-> header.recLen + 1);buf[0] = ' ';for ( i = 0; i < nFiledCount; i++ ){if (hand-> rec.field[i].fieldType == DATE && record-> field[i].fieldData != NULL ) record-> field[i].fieldData = SaveDate(record-> field[i].fieldData);curFieldLen = hand-> rec.field[i].fieldLen;actDataLen = strlen(record-> field[i].fieldData);if ( actDataLen > curFieldLen)actDataLen = curFieldLen;memcpy(&buf[nPos], record-> field[i].fieldData, actDataLen);nPos += hand-> rec.field[i].fieldLen;}buf[hand-> header.recLen] = '\0 ';if ( -1 == fwrite(buf, hand-> header.recLen , 1 , hand->fd))rest = FALSE;return rest;}char *ReadDbfDate( char *str)//读日期时,格式化{int i=0, j=0;char temp[11] = {0};char *strRest = NULL;for ( i=0, j=0; i < 8; i++, j++ ){if ( i==4 || i==6){temp[j] = '/';j++;}temp[j]=*(str+i);}str = (char *)calloc(11,sizeof(char));memset(str, 0x00, 11);memcpy(str,temp,10);return str;}char *DsdStrEndSpace(char *str, int size)//去掉字符串的后面空格{int i=0,flag=0;for(i=size-1; i>= 0; i--){if (*(str+i) != 0x20)break;str[i] =0x00;}return str;}/*pDbfField GetCurrentField(pDbfHand handle, pDbfRec rec ,int fieldId)//得到当前文件{return (pDbfField)&(rec-> field[fieldId]);}*/int GetFieldNum(pDbfHand hand,char* field_name)//得到文件个数{int flag=0;int j;for(j=0;j<hand->rec.realCounts;j++){if(0==strcmp(field_name,hand->rec.field[j].fieldName)){flag=1;break;}}if(flag)return j;elsereturn -1;}int IsBottomRecord(pDbfHand hand)//判断是否是第一条记录{int recCounts = hand-> header.recCounts;if (hand-> curRecNo != hand-> header.recCounts)return FALSE;return TRUE;}int GotoTop(pDbfHand hand)//返回顶部{hand-> curRecNo = 1;hand-> curFpAddr = hand-> header.firstRecAddr;if (-1 == fseek(hand-> fd, hand-> curFpAddr, SEEK_SET))return FALSE;return TRUE;}int GotoBottom(pDbfHand hand)//移动到最后一条记录{int recLen = hand-> header.recLen;long offset = 0;offset = recLen * (hand-> header.recCounts -1);hand-> curRecNo = hand-> header.recCounts;hand-> curFpAddr = hand-> header.firstRecAddr + offset;if (-1 == fseek(hand-> fd, hand-> curFpAddr, SEEK_SET))return FALSE;return TRUE;}int GoNextRecord(pDbfHand hand)//移动到下一条记录{if (TRUE == IsBottomRecord(hand))return TRUE;else{hand-> curRecNo += 1;hand-> curFpAddr += hand-> header.recLen;if (-1 == fseek(hand-> fd, hand-> header.recLen, SEEK_CUR)) return FALSE;}return TRUE;}int GetCurRecord(pDbfHand hand,pDbfRec rec)//得到当前记录{int i = 0,m;char curFieldLen = 0;long fieldOffset = 0;char pBuffData[1024] = {0};char *recData;(*rec)=hand->rec;for(m=0;m<hand->rec.realCounts;m++){rec->field[m].fieldData=(char*)malloc(hand->rec.field[m].fieldLen+1);memset(rec->field[m].fieldData, 0x00, hand->rec.field[m].fieldLen+1);}if (-1== fseek(hand->fd,hand->curFpAddr, SEEK_SET)) //Move file pointer return 0;recData = pBuffData;recData = recData + 1;if (-1 == fread(recData,hand->header.recLen , 1 , hand->fd))return 0;rec->delFlag = recData[0];recData = recData + 1;for (i = 0; i < hand->rec.realCounts; i++, fieldOffset += curFieldLen){curFieldLen = hand->rec.field[i].fieldLen;memcpy(rec->field[i].fieldData,recData+fieldOffset,curFieldLen);rec->field[i].fieldData = DsdStrEndSpace(rec->field[i].fieldData, curFieldLen);if (rec->field[i].fieldType == DATE && rec->field[i].fieldData[0] != 0x20 ) rec->field[i].fieldData =ReadDbfDate(rec->field[i].fieldData);}return 1;}pDbfHand AddRec(pDbfHand hand, pDbfRec record)//向文件尾中插入一条记录{char dataEndFlag = 0x1A;if ((hand-> fd =fopen("c:\\数据库\\table.dbf","r+b")) == NULL)return NULL;if ( hand-> header.recCounts == 0)fseek(hand-> fd, -0L, SEEK_END);elsefseek(hand-> fd, -1L, SEEK_END);if (FALSE == WriteRecord(hand, record))return NULL;if ( -1 == fwrite(&dataEndFlag, 1 , 1 , hand->fd))return NULL;hand-> curRecNo += 1;hand-> header.recCounts += 1;hand-> curFpAddr += hand-> header.recLen;if (NULL == UpdateHead(hand))return hand;}pDbfHand AddField(pDbfHand hand,char* field_name,char type,int len,int dec)//在文件中插入一条记录{int i,reccounts;pDbfRec *rec;if(hand->header.recCounts){rec=(pDbfRec*)malloc(sizeof(pDbfRec)*hand->header.recCounts);hand->curFpAddr=hand->header.firstRecAddr;hand->curRecNo=1;if (-1 == fseek(hand-> fd,hand->curFpAddr, SEEK_SET))return FALSE;for(i=0;i<hand->header.recCounts;i++){rec[i]=(pDbfRec)malloc(sizeof(DbfRec));GetCurRecord(hand,rec[i]);GoNextRecord(hand);}for(i=0;i<hand->header.recCounts;i++){rec[i]->field[hand->rec.realCounts].fieldData=(char*)malloc(len+1);memset(rec[i]->field[hand->rec.realCounts].fieldData, 0x00, len+1);rec[i]->realCounts++;}}strcpy(hand->rec.field[hand->rec.realCounts].fieldName,field_name);hand->rec.field[hand->rec.realCounts].fieldName[strlen(field_name)]='\0';hand->rec.field[hand->rec.realCounts].fieldType=type;hand->rec.field[hand->rec.realCounts].fieldLen=len;hand->rec.field[hand->rec.realCounts].fieldDcis=dec;hand->rec.realCounts++;reccounts=hand->header.recCounts;hand->fd=fopen("c:\\数据库\\table.dbf","w");fclose(hand->fd);hand->fd=fopen("c:\\数据库\\table.dbf","w+b");if(NewDbfHead(hand, &hand->rec) == NULL)return NULL;if (NULL == WriteFieldDicsribe(hand, &hand->rec))return NULL;WriteFieldEnd(hand);if(0!=fclose(hand->fd))for(i=0;i<reccounts;i++)AddRec(hand,rec[i]);return hand;}pDbfHand AlterField(pDbfHand hand,char* field_name,char type,int len,int dec)//修改一条记录{int num,i,reccounts;pDbfRec *rec;num=GetFieldNum(hand,field_name);if(hand->header.recCounts){rec=(pDbfRec*)malloc(sizeof(pDbfRec)*hand->header.recCounts);hand->curFpAddr=hand->header.firstRecAddr;hand->curRecNo=1;if (-1 == fseek(hand-> fd,hand->curFpAddr, SEEK_SET))return FALSE;for(i=0;i<hand->header.recCounts;i++){rec[i]=(pDbfRec)malloc(sizeof(DbfRec));GetCurRecord(hand,rec[i]);GoNextRecord(hand);rec[i]->field[num].fieldDcis=dec;rec[i]->field[num].fieldLen=len;rec[i]->field[num].fieldType=type;}}hand->rec.field[num].fieldType=type;hand->rec.field[num].fieldLen=len;hand->rec.field[num].fieldDcis=dec;reccounts=hand->header.recCounts;hand->fd=fopen("c:\\数据库\\table.dbf","w");fclose(hand->fd);hand->fd=fopen("c:\\数据库\\table.dbf","w+b");if(NewDbfHead(hand, &hand->rec) == NULL)return NULL;if (NULL == WriteFieldDicsribe(hand, &hand->rec))return NULL;WriteFieldEnd(hand);if(0!=fclose(hand->fd))return NULL;for(i=0;i<reccounts;i++)AddRec(hand,rec[i]);return hand;}pDbfHand DropField(pDbfHand hand,char* field_name,int n)//删除一条记录{int num,i,j,reccounts;pDbfRec *rec;num=GetFieldNum(hand,field_name);if(hand->header.recCounts){rec=(pDbfRec*)malloc(sizeof(pDbfRec)*hand->header.recCounts);hand->curFpAddr=hand->header.firstRecAddr;hand->curRecNo=1;if (-1 == fseek(hand-> fd,hand->curFpAddr, SEEK_SET))return FALSE;for(i=0;i<hand->header.recCounts;i++){rec[i]=(pDbfRec)malloc(sizeof(DbfRec));GetCurRecord(hand,rec[i]);GoNextRecord(hand);rec[i]->field[num].fieldDcis=rec[i]->field[num+1].fieldDcis;rec[i]->field[num].fieldLen=rec[i]->field[num+1].fieldLen;rec[i]->field[num].fieldType=rec[i]->field[num+1].fieldType;num++;}}if(num==-1){for(j=0;j<10;j++)hand->rec.field[num].fieldName[j]=0;hand->rec.field[num].fieldType=0;hand->rec.field[num].fieldLen=0;hand->rec.field[num].fieldDcis=0;}else{for(;num<n;num++){strcpy(hand->rec.field[num].fieldName,hand->rec.field[num+1].fieldName);hand->rec.field[num].fieldType=hand->rec.field[num+1].fieldType;hand->rec.field[num].fieldLen=hand->rec.field[num+1].fieldLen;hand->rec.field[num].fieldDcis=hand->rec.field[num+1].fieldDcis;}}hand->header.recCounts--;reccounts=hand->header.recCounts;hand->fd=fopen("c:\\数据库\\table.dbf","w");fclose(hand->fd);hand->fd=fopen("c:\\数据库\\table.dbf","w+b");if(NewDbfHead(hand,&hand->rec) == NULL)return NULL;if (NULL == WriteFieldDicsribe(hand,&hand->rec))return NULL;WriteFieldEnd(hand);if(0!=fclose(hand->fd))return NULL;for(i=0;i<reccounts;i++)AddRec(hand,rec[i]);return hand;}pDbfHand CreateDbf(DbfRec rec)//建立DBF文件{pDbfHand hand = NULL;if ((hand = (pDbfHand)malloc(sizeof(DbfHand))) ==NULL) return NULL;memset(hand, 0x00, sizeof(DbfHand));if((hand->fd=fopen("c:\\数据库\\table.dbf","w+b"))!=NULL){if(NewDbfHead(hand, &rec) == NULL)return NULL;if(NULL == WriteFieldDicsribe(hand, &rec))return NULL;WriteFieldEnd(hand);}fclose(hand->fd);return hand;}pDbfHand OpenDbf()//打开DBF文件{pDbfHand hand = NULL;if ((hand = (pDbfHand) malloc(sizeof(DbfHand))) ==NULL) return NULL;memset(hand, 0x00, sizeof(DbfHand));if ((hand-> fd =fopen("c:\\数据库\\table.dbf","r+b")) == NULL) return NULL;if(NULL == ReadDbfHead(hand))return NULL;if(NULL == ReadFieldDiscribe(hand))return NULL;GotoTop(hand);return hand;}void ShowBeginning(){printf("+----------------------------------------------------------------------------+\n");printf("||\n");printf("| 欢迎进入SQL系统|\n");printf("||\n");printf("+----------------------------------------------------------------------------+\n");}void Show(){printf(" 输入语句格式如下:\n");printf(" create table student\n");printf(" (\n");printf(" SNO int PRIMARY KEY,\n");printf(" SNAME char(10) UNIQUE,\n");printf(" SAGE int,\n");printf(" SDEPT char(20) NOT NULL,\n");printf(" COURSE char(20),\n");printf(" GRADE int\n");printf(" );\n");printf(" alter table student add CNO int NOT NULL;\n");printf(" alter table student alter column SAGE short;\n");printf(" alter table student drop SDEPT;\n");}int Read()//用于读取从键盘键入的SQL语句{char c;int i,j;printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++\n");printf("请输入SQL语句:\n\n");for(i=0;(c=getch())!=';';i++){if(c==27)exit(0);if(c==8){i-=2;if(i<-1)i=-1;system("cls");Show();printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++\n");printf("请输入SQL语句:\n\n");for(j=0;j<=i;j++){if(sql1[j]==13){puts("");}printf("%c",sql1[j]);}continue;}sql1[i]=c;if(c==13){puts("");sql[i]=' ';}else if(c=='('||c==')'||c=='\''){printf("%c",c);sql[i]=' ';}else{printf("%c",c);sql[i]=c;}}sql[i]=';';sql[i+1]='\0';printf(";");puts("");return 0;}int position;//文件指针的位置int Read1()//用于读取从程序文件中读取的SQL语句{FILE *fp;char c;int i,j;printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++\n");printf("请输入SQL语句:\n\n");fp=fopen("d:\\table.sql","r");fseek(fp,position,0);for(i=0;(c=fgetc(fp))!=';';i++){if(c==27)exit(0);if(c==8){i-=2;if(i<-1)i=-1;system("cls");Show();printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++\n");printf("请输入SQL语句:\n\n");for(j=0;j<=i;j++){if(sql1[j]==13)puts("");printf("%c",sql1[j]);}continue;}sql1[i]=c;if(c==13){puts("");sql[i]=' ';}else if(c=='('||c==')'||c=='\''){printf("%c",c);sql[i]=' ';}else{printf("%c",c);sql[i]=c;}}sql[i]=';';sql[i+1]='\0';printf(";");puts("");position=ftell(fp);return 0;}int CREATE(){char type[6][10]={"char","int","short","float","double"};char Type1[6]={'C','N','S','F','D'};FILE *fp=NULL;Condition con[MAX];//约束条件结构体int i,j,num=0,error=1;//num 字段数error输入错误标志char temp[10],condition[10];//table_name 表名condition 约束条件memset(con,0,sizeof(con[0])*MAX);//结构体中各值初始化为空for(i=0;sql[i]==' ';i++);for(j=0;sql[i]!=' '&&sql[i]!=';';i++,j++)temp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,"create")!=0)//判断create是否写错{error=0;printf("\n你输入的'create'有误,请重新创建!\n");}else{for(;sql[i]==' ';i++);for(j=0;sql[i]!=' '&&sql[i]!=';';i++,j++)temp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,"table")!=0)//判断table是否写错{error=0;printf("\n你输入的'table'有误,请重新创建!\n");}else{for(;sql[i]==' ';i++);for(j=0;sql[i]!=' '&&sql[i]!=';';i++,j++)//提取表名table_name[j]=tolower(sql[i]);table_name[j]='.'; //加后缀.dbftable_name[j+1]='d';table_name[j+2]='b';table_name[j+3]='f';table_name[j+4]='\0';do{int field_lengtf=0,flag=1;//field_lengtf 字段长度for(;sql[i]==' '||sql[i]==',';i++);if(sql[i]==';')break;for(j=0;sql[i]!=' ';i++,j++)//提取列名rec.field[num].fieldName[j]=sql[i];rec.field[num].fieldName[j]='\0';for(;sql[i]==' ';i++);for(j=0;sql[i]!=' '&&sql[i]!=',';i++,j++)//提取列数据类型temp[j]=tolower(sql[i]);temp[j]='\0';for(j=0;j<5;j++){if(strcmp(temp,type[j])==0){rec.field[num].fieldType=Type1[j];break;}}if(j==5)//列数据类型有误{error=0;printf("\n你输入的列数据类型有误(务必是char,int,short,float,double),请重新创建!\n");break;}else{/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/if(rec.field[num].fieldType=='C'){//如果类型是char需要提取列长度,如果是int、float则固定列长度为4,double则固定列长度为8for(;sql[i]==' ';i++);for(;sql[i]!=' ';i++)//提取长度{if(sql[i]<48||sql[i]>58)//列长度必须是数字{flag=0;break;}elsefield_lengtf=field_lengtf*10+(sql[i]-48);}if(flag==0){error=0;printf("\n你输入的列长度有误,请重新创建!\n");break;}elserec.field[num].fieldLen=field_lengtf;}else if(rec.field[num].fieldType=='N'||rec.field[num].fieldType=='F') rec.field[num].fieldLen=4;else if(rec.field[num].fieldType=='D')rec.field[num].fieldLen=8;else if(rec.field[num].fieldType=='S')rec.field[num].fieldLen=2;field_lengtf=0,flag=1;for(;sql[i]==' ';i++);for(j=0;sql[i]!=' '&&sql[i]!=','&&sql[i]!=';';i++,j++)//提取列完整性约束condition[j]=tolower(sql[i]);condition[j]='\0';if(condition[0]!=','&&condition[0]!='\0'){if(strcmp(condition,"primary")==0)//判断是否为主键{for(;sql[i]==' ';i++);for(j=0;sql[i]!=' '&&sql[i]!=',';i++,j++)temp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,"key")==0)con[num].flag=1;else{error=0;printf("\n你输入的'KEY'有误,请重新创建!\n");break;}}else if(strcmp(condition,"unique")==0)//判断是否取唯一值con[num].flag=2;else if(strcmp(condition,"not")==0)//判断是否非空{for(;sql[i]==' ';i++);for(j=0;sql[i]!=' '&&sql[i]!=',';i++,j++)condition[j]=tolower(sql[i]);condition[j]='\0';if(strcmp(condition,"null")==0)con[num].flag=3;else{error=0;printf("\n你输入的约束条件'NOT NULL'有误,请重新创建!\n");break;}}else//列完整性约束出错{error=0;printf("\n你输入的约束条件有误(必须PARIMARY KEY,UNIQUE,NOT NULL),请重新创建!\n");break;}}}rec.field[num].fieldDcis=0;rec.field[num].offset=0;rec.realCounts=num+1;num++;//下一字段}while(sql[i]!=';');if((fp=fopen("d:\\student.dat","w+b"))==NULL)//将列完整性约束条件写入文件中{printf("打开文件失败!\n");error=0;}fwrite(&con,sizeof(Condition)*num,1,fp);fclose(fp);fp=NULL;}}if(error==0){memset(sql,0,sizeof(char)*300);//出错时清空输入的语句return 0;}else{if(f!=CreateDbf(rec))//建立.dbf文件return 1;elsereturn 0;}return 1;}int ALTER(){char type[6][10]={"char","int","short","float","double"};char Type1[6]={'C','N','S','F','D'};FILE *fp;int i,j,num,error=1;char T; //列数据类型的简写Condition con[MAX];//约束条件结构体char temp[10],field_name[10],f_type[10],condition[10];// field_name 列名f_type 列数据类型memset(&con,0,sizeof(con));//结构体中各值初始化为空for(i=0;sql[i]==' ';i++);for(j=0;sql[i]!=' ';i++,j++)temp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,"alter")!=0)//判断alter是否写错{error=0;printf("\n你输入的'alter'有误,请重新输入SQL语句!\n");}else{for(;sql[i]==' ';i++);for(j=0;sql[i]!=' ';i++,j++)temp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,"table")!=0)//判断table是否写错{error=0;printf("\n你输入的'table'有误,请重新输入SQL语句!\n");}else{for(;sql[i]==' ';i++);for(j=0;sql[i]!=' ';i++,j++)//提取表名table_name[j]=tolower(sql[i]);table_name[j]='.'; //加后缀.dbftable_name[j+1]='d';table_name[j+2]='b';table_name[j+3]='f';table_name[j+4]='\0';if((f=OpenDbf())==0) //判断该表是否存在{printf("\n你输入的表%s不存在,请重新输入SQL语句!\n",table_name);error=0;}else{for(;sql[i]==' ';i++);if(tolower(sql[i])=='a')//增加属性{for(j=0;sql[i]!=' ';i++,j++)//判断add是否写错temp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,"add")==0){int field_lengtf=0,flag=1; //field_lengtf 列长度for(;sql[i]==' ';i++);for(j=0;sql[i]!=' ';i++,j++)//提取添加的列名field_name[j]=sql[i];field_name[j]='\0';for(;sql[i]==' ';i++);for(j=0;sql[i]!=' '&&sql[i]!=';';i++,j++)//提取列数据类型f_type[j]=tolower(sql[i]);f_type[j]='\0';for(j=0;j<5;j++){if(strcmp(f_type,type[j])==0){T=Type1[j];break;}}。