📄 main.c
字号:
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);
// getchar();
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;
case 'd':
CheckDouble(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";
// puts(strFileName);
printf("\n请输入新建数据库的各字段信息:\n");
CreateTable(file->pTable);
SubMenu(file);
// printf("文件新建成功!\n");
}
void GetChar(char *pChar)
{
char ch;
while( ch=='\n'|| ch=='?')
{
scanf("%c",&ch);
}
*pChar=ch;
}
/*
* 函数功能: 追加记录
* 输入参数:文件指针
* 输出参数:无
* 返 回 值:无
*/
void AddData(struct File *file)
{
struct Table table=*(file->pTable);
struct Node *pCur;
int i;
int nCount=0;
char strChoice[20];
char type;
int nLen=file->pTable->nLen;
printf("\n\n文件共有%d个字段\n\n",nLen);
if( (!file) || (!file->pHead) )
{
printf("文件加载数据失败,请确认文件是否受损!\n");
return;
}
/*
printf("请依次输入数据:\n");
pCur=(struct Node *)malloc( sizeof(struct Node) );
for( i=0; i<nLen ;i++ )
{
printf("请输入%s:\n",table.Head[i].strName);
// getchar();
type=table.Head[i].cType;
switch (type)
{
case 'c':
// getchar();
scanf( "%c", &(( pCur->arr[i]).CHAR));
getchar();
// printf("\n已添加数据项:%c\n",pCur->arr[i].CHAR);
break;
case 's':
GetString(pCur->arr[i].STRINR);
break;
case 'd':
scanf( "%d", &(( pCur->arr[i]).DOUBLE));
break;
case 'i':
scanf( "%d", &(( pCur->arr[i]).INT));
break;
default:
printf("无法识别的数据类型!\n");
}
}
pCur->next=file->pHead->next;
file->pHead->next=pCur;
file->nCount++;
*/
while(1)
{
printf("添加数据('a')\n退出(any key)\n");
// GetString(strChoice);
gets(strChoice);
// getchar();
if( 'a' != 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:\n",table.Head[i].strName);
type=table.Head[i].cType;
// getchar();
switch (type)
{
case 'c':
/* while( '\n' != ( ch=getchar()))
{
}
*/
scanf( "%s", strChoice);
// GetChar(&(pCur->arr[i].CHAR));
pCur->arr[i].CHAR=strChoice[0];
// printf("\n已添加数据项:%c\n",pCur->arr[i].CHAR);
getchar();
break;
// pCur->arr[i].CHAR=ch;
case 's':
GetString(pCur->arr[i].STRINR);
// gets(pCur->arr[i].STRINR);
// puts(pCur->arr[i].STRINR);
// getchar();
break;
case 'd':
scanf( "%lf", &(( pCur->arr[i]).DOUBLE));
printf("\n已添加数据项:%.2lf\n",pCur->arr[i].DOUBLE);
getchar();
break;
case 'i':
scanf( "%d", &(( pCur->arr[i]).INT));
// printf("\n已添加数据项:%d\n",pCur->arr[i].INT);
getchar();
break;
default:
printf("无法识别的数据类型!\n");
}
}
pCur->next=file->pHead->next;
file->pHead->next=pCur;
file->nCount++;
nCount++;
}
}
/*
* 函数功能:从文件中读取数据,并存入文件结构体中
* 输入参数: 文件指针,文件名
* 输出参数:文件指针
* 返 回 值:无
*/
void Read(struct File *file,char *strFileName)
{
// printf("\n正在解读文件%s的数据\n",strFileName);
/*读取文件表头*/
ReadTable(file,strFileName);
ShowTable(file->pTable);
/*读取文件数据区*/
ReadData(file,strFileName);
// printf("\n成功解读文件%s\n",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[20];
char strFileName[MAX_LEN_FILE_NANE];
int flag =OK;
int isSave=ERROR;
while( flag )
{
printf("追加('a')\n");
printf("查询('f')\n");
printf("更新('u')\n");
printf("删除('d')\n");
printf("保存('s')\n");
printf("退出(any key)\n");
gets(strChoice);
switch (strChoice[0])
{
case 'a':
Add(file);
break;
case 'f':
Find(file);
break;
case 'u':
Update(file);
break;
case 'd':
Delete(file);
break;
case 's':
printf("请输入文件名\n");
gets(strFileName);
strcat(strFileName,".txt");
getchar();
Save(*file,strFileName);
isSave=OK;
break;
default:
printf("你真的退出子菜单?('Y'/any key)");
GetString(strChoice);
if('Y'==strChoice[0])
{
flag=ERROR;
if(!isSave)
{
printf("你尚未存盘\n");
printf("请输入文件名\n");
gets(strFileName);
strcat(strFileName,".txt");
getchar();
Save(*file,strFileName);
}
break;
}
else
{
continue;
}
}
}
}
void SetSystem()
{
int nFlag =1;
// SUPER_MARKET_MODEL 超市模式
// OFFICE_MODEL 办公模式
char ch[20];
printf("\n 本系统的存储模式:\n 超市模式\t 适合于频繁添加,删除记录的场合\n 办公模式\t 适合于排序查询频繁的场合\n当前模式为:");
if ( SUPER_MARKET_MODEL == nModel )
{
printf("超市模式\n");
}
else
{
printf("办公模式\n");
}
printf("请选择模式:\n 超市模式('S ')\n办公模式('O')\n 其他键退出\n ");
while( nFlag )
{
// scanf("%c",&ch);
gets(ch);
// printf("%c\n",ch[0]);
// getchar();
switch (ch[0])
{
case 'S':
case 's':
nModel=SUPER_MARKET_MODEL;
printf("你选择了超市模式\n ");
nFlag=0;
break;
case 'O':
case 'o':
nModel= OFFICE_MODEL;
printf ("你选择了办公模式\n ");
nFlag=0;
break;
default :
nFlag=0;
}
// getchar();
}
}
/*
* 函数功能:显示菜单,进入消息循环
* 输入参数:无
* 输出参数:无
* 返 回 值:无
*/
void MainMenu()
{
char strChoice[20];
int flag=OK;
struct File file;
InitFile(&file);
printf("欢迎使用LiuDataBase通用数据库软件\n\n\n");
printf("Copyright (c) 2006,华中科技大学Dian团队\n");
printf("All rights reserved.\n\n");
printf("文件名称:LiuDataBase\n");
printf("摘 要:模拟交互式的SOL的基本功能:动态建表,组合条件查询\n");
printf(" 支持的数据类型为:char(字符型)int(整数型)double(浮点型)string(字符串型)\n");
// printf(" double(浮点型) string(字符串型)\n");
printf("当前版本:清级\n");
printf("作 者:电信0401 刘焱\n\n\n");
/*输出内容:
* Copyright (c) 2006,华中科技大学Dian团队
* All rights reserved.
*
* 文件名称:LiuDataBase
* 摘 要:模拟交互式的SOL的基本功能:建表,查询,删除,添加,更新
* 支持的数据类型为:char(字符型) int(整数型) double(浮点型) string(字符串型)
*
* 当前版本:清级(后继版本依次为明级,元级....)
* 作 者:刘焱
* 完成日期:
*
* 修改日期:
* 修改内容:
*
*/
while( flag )
{
printf("\t主菜单\n");
printf("打开('O')\n");
printf("新建('N')\n");
printf("帮助('H')\n");
printf("设置('S')\n");
printf("退出('Q')\n");
GetString(strChoice);
switch (strChoice[0])
{
case 'O':
case 'o':
Open(&file);
continue;
case 'N':
case 'n':
New(&file);
continue;
case 'H':
case 'h':
Help();
continue;
case 'S':
case 's':
SetSystem();
continue;
case 'Q':
case 'q':
printf("你真的退出主菜单?('Y'/any key)");
GetString(strChoice);
if('Y'==strChoice[0])
{
flag=ERROR;
break;
}
else
{
continue;
}
default:
printf("你按了无效键,建议使用帮助('H')\n");
continue;
}
}
}
/*
* 函数功能:驱动程序,检测个函数的功能以及接口
* 输入参数:无
* 输出参数:无
* 返 回 值:无
*/
void Driver()
{
struct Node *pCur;
struct File file1,file2;
int i;
InitFile( &file1 );
CreateTable(file1.pTable);
ShowTable(file1.pTable);
// AddData( &file1 );
// Save( file1, "data.txt" );
// InitFile(&file2);
// Read(&file2,"hudie.txt");
// for( i=0 , pCur=file2.pHead->next ; i <file2.nCount ; pCur=pCur->next ,i++)
// {
// ShowData(pCur->arr,&file2);
// }
}
void main()
{
// Driver();
MainMenu();
getchar();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -