#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_DATA_LEN 20 //最大字段數
#define MAX_NAME_LENTH 20 //最大字段名長度
#define MAX_LEN_FILE_NANE 127 //最大表名長度
#define MAX_STRING_LENGTH 127 //最大字符串長度
#define OK 1
#define ERROR 0
#define INITSTATUS -1
union Elemtype//定義元素類型
{
char CHAR; //字符型
char STRING[MAX_STRING_LENGTH]; //字符串型
int INT;//整數型
};
struct Node//定義元素節點類型
{
union Elemtype arr[MAX_DATA_LEN];
struct Node *next;
};
struct Data//字段基本信息
{
char strName[MAX_NAME_LENTH]; //字段名
int nLen; //字段長度
char cType; //字段類型
int key;//是否是主碼
};
struct DataNode//記錄字段基本信息的節點
{
struct Data data;//字段基本信息結構體
struct DataNode *pNext;//指針
};
struct Table//記錄所有字段信息的表結構體
{
int nLen;//表的長度
struct Data Head[MAX_NAME_LENTH];//記錄字段全部信息的數組
int nCount;//記錄數據容量
};
struct File//定義文件結構
{
struct Table *pTable;//表頭指針
struct Node *pHead;//數據區指針
int nCount;//數據區容量
};
struct USER
{
char name[MAX_STRING_LENGTH];
char password[MAX_STRING_LENGTH];
};
void GetString(char *str)//獲取字段類型
{
while(1)
{
gets(str);
if(strlen(str)) break;
printf("輸入不可為空\n請重新輸入\n");
}
}
void InitFile(struct File *file)//初始化文件,為表頭,數據區分配空間
{
struct Node *p;
p=( struct Node * )malloc( sizeof(struct Node ) );
p->next=NULL;
file->pHead=p;
file->nCount=0;
file->pTable=( struct Table * )malloc( sizeof( struct Table ) );
file->pTable->nCount=0;
file->pTable->nLen=0;
}
int GetStrName(char *str)//獲取字段名
{
char strChoice[20];
char ch;
while(1)
{
gets(str);
if( strlen( str ) )
{
return OK;
}
else
{
printf("退出建庫?(Y/any key)");
gets(strChoice);
if('Y'==strChoice[0])
{
return ERROR;
}
else
{
printf("請輸入數據名:\n");
continue;
}
}
}
}
int IsRight( struct Table *pTable, char *str )
{
int i;
int nCount;
for( i=0 ,nCount=MAX_DATA_LEN; i < nCount ; i++ )
{
if( 0 == strcmp( pTable->Head[i].strName, str))
{
return ERROR;
}
}
return OK;
}
void CreateTable(struct Table *pTable)//建表并加載信息
{
int len=0,C=0,D=0;
char string[20];
char strChoice[20];
struct Data data;
printf("請輸入數據名:\n");
gets(data.strName);
if(!strlen(data.strName))
{
printf("退出建庫?(Y/any key)");
gets(strChoice);
if('Y'==strChoice[0])
{
pTable->nLen=0;
return ;
}
else
{
printf("請輸入數據名:\n");
gets(data.strName);
puts(data.strName);
}
}
printf("請輸入數據類型(支持string,char,int):\n");
while(1)
{
GetString(string);
if( string[0]=='i' || string[0]=='s' ||string[0]=='c' || string[0]=='d' )
{
break;
}
else
{
printf("不支持該數據類型,請重新輸入:\n");
continue;
}
}
data.cType=string[0];
switch (string[0])
{
case 's':
printf("請輸入數據長度:\n");
scanf("%d",&data.nLen);
break;
case 'c':
data.nLen=1;
break;
case 'i':
data.nLen=2;
break;
case 'd':
data.nLen=4;
break;
}
printf("是否為主碼或外碼?\n1.主碼\n2.外碼\n0.不是\n");
scanf("%d",&C);
data.key=C;
if(data.key==1) D++;
strChoice[0]=getchar();
pTable->Head[len++]=data;
while(len<20)
{
printf("請輸入數據名:\n");
if( ERROR == GetStrName(data.strName))
{
puts(data.strName);
pTable->nLen=len;
printf("成功建立表頭!\n");
return;
}
else if( ERROR ==IsRight( pTable , data.strName ) )
{
printf("%s已存在,請重命名\n ",data.strName );
continue;
}
else
{
printf("請輸入數據類型:\n");
while(1)
{
GetString(string);
if( string[0]=='i' || string[0]=='s' ||string[0]=='c' || string[0]=='d' )
{
break;
}
else
{
printf("不支持該數據類型,請重新輸入:\n");
continue;
}
}
data.cType=string[0];
switch (string[0])
{
case 's':
printf("請輸入數據長度:\n");
scanf("%d",&data.nLen);
break;
case 'c':
data.nLen=1;
break;
case 'i':
data.nLen=2;
break;
case 'd':
data.nLen=4;
break;
}
printf("是否為主碼或外碼?\n1.主碼\n2.外碼\n0.不是\n");
scanf("%d",&C);
while(C==1&&D>0)
{
printf("錯誤!已有主碼!請重新輸入:\n");
scanf("%d",&C);
}
data.key=C;
if(data.key==1) D++;
strChoice[0]=getchar();
pTable->Head[len++]=data;
}
}
pTable->nLen=len;
}
void ReadTable(struct File *file,char *strFileName)//打開文件,獲取表頭
{
FILE *pFILE;
struct Table *pTable=file->pTable;
if( ERROR== (pFILE=fopen(strFileName,"rb+")))
{
printf("文件%s無法打開!\n",strFileName);
return;
}
else
{
fread(pTable, sizeof( struct Table),1,pFILE);
file->nCount=file->pTable->nCount;
fclose(pFILE);
printf("\n成功解讀文件%s的表頭!\n",strFileName);
}
}
void SaveTable(struct Table *pTable,char *strFileName)//將表存入文件
{
FILE *pFILE;
if(!pTable)
{
printf("表指針為空,無法保存!\n");
return;
}
if( ERROR== ( pFILE = fopen( strFileName ,"wb+") ) )
{
printf("文件%s無法打開!\n",strFileName);
return;
}
else
{
fwrite( pTable, sizeof( struct Table ),1,pFILE);
fclose(pFILE);
printf("成功在文件%s中保存表頭!\n",strFileName);
}
}
void ShowTable(struct Table *pTable)//顯示文件表頭
{
int i;
if(!pTable)
{
printf("表指針為空,無法解讀!\n");
return;
}
printf("\n\n本數據文件各字段信息如下:\n");
printf("字段名\t\t數據類型\t主碼/外碼\n");
for(i=0;i<pTable->nLen;i++)
{
printf("%s\t\t%c\t\t",pTable->Head[i].strName,pTable->Head[i].cType);
if(pTable->Head[i].key==1) printf("主碼\n");
else if(pTable->Head[i].key==2) printf("外碼\n");
else printf("不是\n");
}
printf("\n\n");
}
int USERS()
{
struct USER use;
printf("\n\n請輸入用戶名:\n");
scanf("%s",use.name);
printf("請輸入用戶密碼:\n");
scanf("%s",use.password);
if((strcmp(use.password,"pass")==0)&&(strcmp(use.name,"admin")==0)) return OK;
else return ERROR;
}
void SaveData(struct File file,char *strFileName)//在文件的數據區追加數據
{
FILE *pFILE;
struct Table *pTable=file.pTable;
struct Node *pCur=file.pHead->next;
int nCount;
if(!pTable)
{
printf("表指針為空,無法解讀文件結構!\n");
return;
}
else
{
if( ERROR == ( pFILE = fopen(strFileName,"ab" ) ) )
{
printf("文件%s無法打開!\n",strFileName);
return;
}
else
{
if(!pCur||!pCur->next)
{
printf("文件為空,請確認數據已加載!\n");
return;
}
for( nCount=0; nCount<file.nCount ;nCount++,pCur=pCur->next)
{
fwrite( pCur->arr, sizeof( union Elemtype )*MAX_DATA_LEN , 1 , pFILE );
}
printf("已成功將%d個記錄保存在文件%s\n",nCount,strFileName);
fclose(pFILE);
}
}
}
void ShowData( union Elemtype *arr ,struct File *file )//顯示元組內容
{
struct Table table=*(file->pTable);//獲取文件表頭
int nLen=file->pTable->nLen;//獲取文件字段總數
int i;
char type;
for( i=0 ; i< nLen ; i++ )
{
type=table.Head[i].cType;
printf("\n%s:",table.Head[i].strName);
switch (type)
{
case 'c':
printf("%c\t",arr[i].CHAR);
break;
case 's':
printf("%s\t",arr[i].STRING);
break;
case 'i':
printf("%d\t",arr[i].INT);
break;
default:
printf("無法識別的數據類型!\n");
}
}
printf("\n");
}
void ReadData(struct File *file,char *strFileName)//讀取文件數據區
{
FILE *pFILE;
char type;
struct Table t;
struct Table *pTable=file->pTable;
struct Node *pCur;
struct Node data;
int i,j;
if(!pTable)
{
printf("表指針為空,無法解讀文件結構!\n");
return;
}
if( ERROR == ( pFILE = fopen(strFileName,"rb" ) ) )
{
printf("文件%s無法打開!\n",strFileName);
return;
}
fread(&t,sizeof(struct Table),1,pFILE);
for( i=0 ; i<file->pTable->nCount ; i++ )
{
pCur = ( struct Node * )malloc( sizeof( struct Node ) );
fread(data.arr,sizeof(union Elemtype)*MAX_DATA_LEN,1,pFILE);
for(j=0;j<file->pTable->nLen;j++)
{
type=pTable->Head[j].cType;
switch (type)
{
case 'c':
pCur->arr[j].CHAR=data.arr[j].CHAR;
//printf("%c\n", pCur->arr[j].CHAR);
break;
case 's':
strcpy(pCur->arr[j].STRING,data.arr[j].STRING);
// puts( pCur->arr[j].STRING);
break;
case 'i':
pCur->arr[j].INT=data.arr[j].INT;
// printf("%d\n", pCur->arr[j].INT);
break;
default:
printf("無法識別的數據類型!\n");
}
}
pCur->next=file->pHead->next;
file->pHead->next=pCur;
}
file->nCount=file->pTable->nCount;
fclose(pFILE);
}
void Open(struct File *file)//打開已有文件
{
struct File file2;
struct Node *pCur=file->pHead->next;
char strFileName[MAX_STRING_LENGTH],str[20],str1[20];
int i;
printf("請輸入表名:\n");
gets(strFileName);
strcat(strFileName,".txt");
getchar();
Read(file,strFileName);
printf("查看表頭('help database')\n");
printf("查看表數據('help table')\n");
scanf("%s%s",str1,str);
if(str[0]=='d')
ShowTable(file->pTable);
else if(str[0]=='t')
for( ; pCur ; pCur = pCur->next)
ShowData(pCur->arr,file);
SubMenu(file);
}
void Add(struct File *file)//添加數據
{
AddData(file);
}
void CheckInt( struct File *file , int *bIsOK, int nIndex )//檢索出滿足條件的int型數據
{
int n;
int i,j,k;
int bFlag=OK;
int nCount=file->nCount;
char strChoice[20];
struct Node *pCur=file->pHead->next;
printf("請輸入匹配數據:\n");
scanf("%d",&n);
while( bFlag )
{
for( i=0 ; ( i < nCount ) && ( pCur ) ; i++, pCur = pCur->next )
{
if( ( (INITSTATUS==bIsOK[i]) && (n=pCur->arr[nIndex].INT))
|| ( (OK==bIsOK[i]) &&(n!=pCur->arr[nIndex].INT) ) )
{
bIsOK[i]=ERROR;
}
else if( (INITSTATUS==bIsOK[i]) && (pCur->arr[nIndex].INT==n) )
{
bIsOK[i]=OK;
ShowData(pCur->arr,file);
}
}
while(!bIsOK)
{
printf("錯誤!請重新輸入:\n");
printf("請輸入匹配數據:\n");
scanf("%d",&n);
}
break;
}
}
void CheckChar( struct File *file , int *bIsOK , int nIndex )//檢索出滿足條件的char型數據
{
char n;
int i,j,k;
int bFlag=OK;
int nCount=file->nCount;
char strChoice[20];
struct Node *pCur=file->pHead->next;
printf("請輸入匹配數據:\n");
scanf("%c",&n);
for( i=0 ; ( i < nCount ) && ( pCur ) ; i++, pCur = pCur->next )
{
if( ( (INITSTATUS==bIsOK[i]) && (n!=pCur->arr[nIndex].CHAR))
|| ( (OK==bIsOK[i]) &&(n!=pCur->arr[nIndex].CHAR) ) )
{
bIsOK[i]=ERROR;
}
else if( (INITSTATUS==bIsOK[i]) && (pCur->arr[nIndex].CHAR==n) )
{
bIsOK[i]=OK;
ShowData(pCur->arr,file);
}
}
while(!bIsOK)
{
printf("錯誤!請重新輸入:\n");
printf("請輸入匹配數據:\n");
scanf("%c",&n);
}
}
void CheckString( struct File *file , int *bIsOK , int nIndex )//檢索出滿足條件的string型數據
{
char n[MAX_STRING_LENGTH];
int i,j,k;
int bFlag=OK;
int nCount=file->nCount;
char strChoice[20];
struct Node *pCur=file->pHead->next;
printf("請輸入匹配數據:\n");
scanf("%s",&n);
getchar();
for( i=0 ; ( i < nCount ) && ( pCur ) ; i++, pCur = pCur->next )
{
if( ( (INITSTATUS==bIsOK[i]) && (strcmp(pCur->arr[nIndex].STRING,n)))
|| ( (OK==bIsOK[i]) &&(strcmp(pCur->arr[nIndex].STRING,n)) ) )
{
bIsOK[i]=ERROR;
}
else if( (INITSTATUS==bIsOK[i]) && (!strcmp(pCur->arr[nIndex].STRING,n)) )
{
bIsOK[i]=OK;
ShowData(pCur->arr,file);
}
}
while(!bIsOK)
{
printf("錯誤!請重新輸入:\n");
printf("請輸入匹配數據:\n");
scanf("%d",&n);
}
}
void MyDeletData(struct Node *pCur)//刪除記錄
{
struct Node *pNext;
if( (!pCur) || (!pCur->next) )
{
printf("指針為空,刪除失敗!\n");
return ;
}
pNext=pCur->next;
pCur->next=pNext->next;
free(pNext);
}
void Find(struct File *file)//條件查詢顯示
{
struct Table table = *( file->pTable );//獲取文件表頭
struct Node *pCur;
int i,j,k;
int nLen=file->pTable->nLen;// 獲取字段總數
int nCount=file->nCount;// 獲取數據區數據容量
int bFlag=OK;
int *bIsOK;// 記錄各數據項是否滿足要求:OK表示滿足,ERROR表示不滿足
char type;//字段類型
char strDataName[MAX_LEN_FILE_NANE];
char strChoice[20];
bIsOK=(int *)malloc( sizeof( int )*nCount );//分配空間
for( i=0; i < nCount;i++)
{
bIsOK[i]=INITSTATUS;//初始化各項為不滿足
}
ShowTable(&table);//顯示文件各字段信息
printf("請輸入需滿足條件的字段名:\n");
while( bFlag )
{
//printf("請輸入需滿足條件的字段名:\n");
gets(strDataName);
if(!strlen(strDataName))
{
printf("結束查詢?(Y/any key)\n");
gets(strChoice);
if('Y'==strChoice[0])
{
bFlag=ERROR;
continue;
}
printf("請輸入需滿足條件的字段名:\n");
}
for( j=0 ; j < nLen ; j++ )
{
if( !strcmp( table.Head[j].strName, strDataName ))
{
break;
}
}
if(j>nLen)
{
printf("無此字段名,請再輸入\n");
continue;
}
type=table.Head[j].cType;
switch(type)
{
case 'c':
CheckChar(file,bIsOK,j);getchar();
break;
case 's':
CheckString(file,bIsOK,j);
break;
case 'i':
CheckInt(file,bIsOK,j);
break;
default:
break;
}
}
for(k=0,i=0,pCur=file->pHead->next; pCur &&i<nCount;i++,pCur=pCur->next)
{
if(OK==bIsOK[i])
{
ShowData(pCur->arr,file);
k++;
}
}
if( !k)
{
printf("對不起,沒有滿足條件的數據\n");
}
}
void MyUpdateData(struct Node *pCur,struct File *file)
{
struct Table table = *( file->pTable );//獲取文件表頭
int i,j,k;
int nLen=file->pTable->nLen;// 獲取字段總數
int nCount=file->nCount;// 獲取數據區數據容量
int bFlag=OK;
int *bIsOK;// 記錄各數據項是否滿足要求:OK表示滿足,ERROR表示不滿足
char type;//字段類型
char strDataName[MAX_LEN_FILE_NANE];
char strChoice[20];
bIsOK=(int *)malloc( sizeof( int )*nCount );//分配空間
for( i=0; i<nCount;i++)
{
bIsOK[i]=INITSTATUS;//初始化各項為不滿足
}
printf("請輸入需滿足更新的字段名:\n");
while( bFlag )
{
gets(strDataName);
if(!strlen(strDataName))
{
printf("你確定結束更新?(Y/any key)\n");
gets(strChoice);
if('Y'==strChoice[0])
{
bFlag=ERROR;
continue;
}
}
for(j=0;j<nLen;j++)
{
if(!strcmp(table.Head[j].strName,strDataName))
{
break;
}
}
if(j>nLen)
{
printf("無此字段名,請再輸入\n");
continue;
}
type=table.Head[j].cType;
printf("請輸入更新內容:\n");
switch(type)
{
case 'c':
scanf("%c",&(pCur->arr[j].CHAR));
break;
case 's':
scanf("%s",pCur->arr[j].STRING);
break;
case 'i':
scanf("%d",&(pCur->arr[j].INT));
break;
default:
break;
}
}
}
void Update( struct File *file)//更新數據
{
struct Table table = *( file->pTable );//獲取文件表頭
struct Node *pCur;
int i,j,k;
int nLen=file->pTable->nLen;// 獲取字段總數
int nCount=file->nCount;// 獲取數據區數據容量
int bFlag=OK;
int *bIsOK;// 記錄各數據項是否滿足要求:OK表示滿足,ERROR表示不滿足
char type;//字段類型
char strDataName[MAX_LEN_FILE_NANE];
char strChoice[20];
bIsOK=(int *)malloc( sizeof( int )*nCount );//分配空間
for( i=0; i<nCount;i++)
{
bIsOK[i]=INITSTATUS;//初始化各項為不滿足
}
ShowTable(&table);//顯示文件各字段信息
printf("請輸入需滿足條件的字段名:\n");
while( bFlag )
{
//printf("請輸入需滿足條件的字段名:\n");
gets(strDataName);
if(!strlen(strDataName))
{
printf("你確定結束更新記錄前篩選?(Y/any key)\n");
gets(strChoice);
if('Y'==strChoice[0])
{
bFlag=ERROR;
continue;
}
}
for( j=0 ; j < nLen ; j++ )
{
if(!strcmp(table.Head[j].strName,strDataName))
{
break;
}
}
if( j >= nLen)
{
printf("無此字段名,請再輸入\n");
gets(strChoice);
}
type=table.Head[j].cType;
switch(type)
{
case 'c':
CheckChar(file,bIsOK,j);
break;
case 's':
CheckString(file,bIsOK,j);
break;
case 'i':
CheckInt(file,bIsOK,j);
break;
default:
break;
}
}
for(i=0,k=0,pCur=file->pHead->next; pCur &&i<nCount;i++,pCur=pCur->next)
{
if(OK==bIsOK[i])
{
ShowData(pCur->arr,file);
MyUpdateData(pCur,file);
ShowData(pCur->arr,file);
k++;
}
}
if(!k)
{
printf("沒有滿足要求的數據:\n");
return;
}
printf("數據更新成功\n");
}
void Delete(struct File *file)//刪除數據
{
struct Table table = *( file->pTable );//獲取文件表頭
struct Node *pCur;
int i,j,k;
int nLen=file->pTable->nLen;// 獲取字段總數
int nCount=file->nCount;// 獲取數據區數據容量
int bFlag=OK;
int *bIsOK;// 記錄各數據項是否滿足要求:OK表示滿足,ERROR表示不滿足
char type;//字段類型
char strDataName[MAX_LEN_FILE_NANE];
char strChoice[20];
bIsOK=(int *)malloc( sizeof( int )*nCount );//分配空間
for( i=0; i<nCount;i++)
{
bIsOK[i]=INITSTATUS;//初始化各項為不滿足
}
ShowTable(&table);//顯示文件各字段信息
while( bFlag )
{
printf("請輸入需滿足條件的字段名:\n");
gets(strDataName);
if(!strlen(strDataName))
{
printf("結束查詢?(Y/any key)\n");
gets(strChoice);
if('Y'==strChoice[0])
{
bFlag=ERROR;
continue;
}
}
for( j=0 ; j < nLen ; j++ )
{
if( !strcmp( table.Head[j].strName, strDataName ))
{
break;
}
}
if( j >= nLen)
{
printf("無此字段名,請再輸入\n");
continue;
}
type=table.Head[j].cType;
switch(type)
{
case 'c':
CheckChar(file,bIsOK,j);
break;
case 's':
CheckString(file,bIsOK,j);
break;
case 'i':
CheckInt(file,bIsOK,j);
break;
default:
break;
}
}
for(i=0,k,pCur=file->pHead; pCur->next &&i<nCount;i++,pCur=pCur->next)
{
if(OK==bIsOK[i])
{
printf("正在刪除以下數據:\n");
ShowData(pCur->next->arr,file);
MyDeletData(pCur);
printf("刪除完畢\n");
}
}
file->nCount-=k;
file->pTable->nCount-=k;
if( !k)
printf("對不起,沒有滿足條件的數據\n");
}
void New(struct File *file)//新建文件
{
char strFileName[MAX_LEN_FILE_NANE]="NONAME.txt";
printf("\n請輸入新建數據庫的各字段信息:\n");
CreateTable(file->pTable);
SubMenu(file);
}
void AddData(struct File *file)//追加記錄
{
struct Table table=*(file->pTable);
struct Node *pCur;
int i,j,h,l;
int nCount=0,strchoice=0;
char strChoice[20];
char type,y;
int nLen=file->pTable->nLen;
printf("\n\n文件共有%d個字段\n\n",nLen);
if( (!file) || (!file->pHead) )
{
printf("文件加載數據失敗,請確認文件是否受損!\n");
return;
}
while(1)
{
printf("1.添加數據\n2.退出\n");
gets(strChoice);
if( '1' != strChoice[0] )
{
printf("\n已成功添加%d個數據\n\n",nCount);
file->pTable->nCount=file->nCount;
return ;
}
pCur= ( struct Node * )malloc( sizeof( struct Node ) );
printf("請依次輸入數據:\n");
for(i=0;i<nLen;i++)
{
printf("請輸入%s(若為空則輸入null):\n",table.Head[i].strName);
type=table.Head[i].cType;
scanf("%s",strChoice);
y=getchar();
switch (type)
{
case 'c':
if(table.Head[i].key!=0)//如該屬性是主碼或外碼
{
while(strcmp(strChoice,"null")==0)
{
printf("該屬性不能為空!請重新輸入:\n");
scanf("%s",strChoice);
}
for(j=i-1;j>=0;j--)//檢索是否已有相同數據
{
if(strChoice[0]==pCur->arr[j].CHAR)
{printf("已有該數據!請重新輸入:");
break;
}
}
}
pCur->arr[i].CHAR=strChoice[0];
getchar();
break;
case 's':
//GetString(strChoice);
if(table.Head[i].key!=0)//如該屬性是主碼或外碼
{
while(strcmp(strChoice,"null")==0)
{
printf("該屬性不能為空!請重新輸入:\n");
GetString(strChoice);
}
for(j=i-1;j>=0;j--)//檢索是否已有相同數據
{
if(strcmp(strChoice,pCur->arr[j].STRING)==0)
{
printf("已有該數據!");
break;
}
}
}
strcpy(pCur->arr[i].STRING,strChoice);
break;
case 'i':
if(table.Head[i].key!=0)//如該屬性是主碼或外碼
{
while(strcmp(strChoice,"null")==0)
{
printf("該屬性不能為空!請重新輸入:\n");
scanf("%s",strChoice);
}
for(j=i-1;j>=0;j--)//檢索是否已有相同數據
{
if(strChoice[0]-'0'==pCur->arr[j].INT)
{printf("已有該數據!請重新輸入:");
break;
}
}
}
pCur->arr[i].INT=strChoice[0];
getchar();
break;
default:
printf("無法識別的數據類型!\n");
}
}
pCur->next=file->pHead->next;
file->pHead->next=pCur;
file->nCount++;
nCount++;
}
}
void Read(struct File *file,char *strFileName)//從文件中讀取數據,并存入文件結構體中
{
ReadTable(file,strFileName);
//ShowTable(file->pTable);
ReadData(file,strFileName);
}
void DelteFile( struct File *file )//釋放文件結構體的空間
{
free(file);
}
void Save(struct File file,char *strFileName)//在文件中儲存數據
{
printf("\n\n正在儲存數據\n\n");
SaveTable(file.pTable,strFileName);
SaveData(file,strFileName);
printf("\n\n已儲存數據\n\n");
}
void SubMenu(struct File *file)//顯示子菜單
{
char strChoice,str1[20],str[20];
char strFileName[MAX_LEN_FILE_NANE],C[20];
int flag =OK;
int isSave=ERROR;
struct Node *pCur=file->pHead->next;
while( flag )
{
//printf("查看('help')\n");
printf("追加('insert')\n");
printf("查詢('select')\n");
printf("更新('update')\n");
printf("刪除('delete')\n");
printf("保存('save')\n");
printf("退出('exit')\n");
scanf("%s",C);
strChoice=getchar();
switch (C[0])
{
case 'i':
Add(file);
break;
case 's':
if(C[1]=='e') Find(file);
else if(C[1]=='a')
{
printf("請輸入表名\n");
gets(strFileName);
strcat(strFileName,".txt");
getchar();
Save(*file,strFileName);
isSave=OK;
}
break;
/*case 'h':
printf("查看表頭('help database')\n");
printf("查看表數據('help table')\n");
scanf("%s%s",str1,str);
if(str[0]=='d')
ShowTable(file->pTable);
else if(str[0]=='t')
ShowData(pCur->arr,file);
break;*/
case 'u':
Update(file);
break;
case 'd':
Delete(file);
break;
default:
printf("退出子菜單?('Y'/any key)");
strChoice=getchar();
if('Y'==strChoice)
{
flag=ERROR;
if(!isSave)
{
printf("你尚未存盤\n");
printf("請輸入表名\n");
gets(strFileName);
strcat(strFileName,".txt");
getchar();
Save(*file,strFileName);
}
break;
}
else
{
continue;
}
}
}
}
void MainMenu()//顯示菜單,進入消息循環
{
char strChoice,C[20],A[100][100][100];
int flag=OK;
struct File file;
InitFile(&file);
while( flag )
{
printf("\n\n\n\t\t\t\t主菜單\n\n\n");
printf("\t\t\t\t新建表('create')\n");
printf("\t\t\t\t打開表('open')\n");
//printf("\t\t\t\t分析語句('analysis')\n");
printf("\t\t\t\t退出菜單('exit')\n");
scanf("%s",C);
strChoice=getchar();
switch (C[0])
{
case 'c':
New(&file);
continue;
case 'o':
Open(&file);
continue;
//case 'a':
//AnalySQL();
//continue;
case 'e':
printf("退出主菜單?('Y'/any key)\n");
strChoice=getchar();
if('Y'==strChoice)
{
flag=ERROR;
break;
}
else
{
continue;
}
default:
printf("錯誤!\n");
continue;
}
}
}
int main()
{
int i;
i=USERS();
if(i==1) {MainMenu();getchar();}
else {printf("用戶沒有權限!\n");return 0;}
}
|