📄 rdbsm.c
字号:
/**
* Title: RDBMS
* Description: 〈数据库〉实验
* Copyright: Copyright (c) 2006
* Company: BJTU
* @author HuangHan NO.03281159 JK0306
* @version 1.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <time.h>
#include <ctype.h>
#include <math.h>
#include <io.h>
#define FIELD_NAME_LENGTH 8 /*最长字段名*/
#define TABLE_NAME_LENGTH 8 /*最长表名*/
#define MAX_COMMOND_LENGTH 100 /*一行命令最多字符数*/
#define MAX_COMMOND_COUNT 20 /*最大命令行数*/
#define MAX_FILED_COUNT 128 /*最多字段数*/
#define MAX_OPENED_COUNT 10 /*可以打开的最多表数*/
#define Int 'a'
#define String 'b'
#define Float 'c'
#define TRUE 1
#define FALSE 0
#define FAILED 0
#define ERROR -1
#define SUCCESS 1
#define FREE -1
#define HELP 0
#define EXIT 1
#define CREATE 2
#define OPEN 3
#define CLOSE 4
#define USE 5
#define INSERT 6
#define DELETE 7
#define SELECT 8
#define UPDATE 9
#define SEEK 10
#define LT 0
#define LE 1
#define EQ 2
#define GT 3
#define GE 4
#define NE 5
typedef struct
{
char dhFieldName[FIELD_NAME_LENGTH];
char dhFieldType;
char dhFieldLen;
int dhFieldPos;
} DBF_STRUCT;
typedef struct dhDate
{
unsigned char year;
unsigned char month;
unsigned char day;
};
typedef struct
{
char dhName[TABLE_NAME_LENGTH];//文件名
int dhHandle;
int dhID;/*文件打开后的对应打开表数组中的号码*/
//struct dhDate dhModifyDate;//最后修改日期
long dhRecCount;/*记录数*/
unsigned dhSturLen;/*字段长度*/
int dhFieldCount;/*字段数*/
long dhRecLen;/*记录长度*/
long dhRecNO;/*当前记录号*/
int dhModify;/*修改标志*/
//int * dhEof;//文件尾标志
DBF_STRUCT dbf_struct[MAX_FILED_COUNT];/*记录结构*/
} DBF;
typedef union {
int intType;
float floatType;
char * string;
} RecUnion;
void welcome();
#include "ShowRec.c"
#include "ReadRec.c"
#include "WriteRec.c"
#include "Compare.c"
#include "InitializeDbf.c"
#include "decipher.c"
#include "Help.c"
#include "Exit.c"
#include "Create.c"
#include "Open.c"
#include "Close.c"
#include "Use.c"
#include "Insert.c"
#include "Delete.c"
#include "Select.c"
#include "Update.c"
#include "Seek.c"
main()
{
DBF dbf[MAX_OPENED_COUNT];/*存放已打开的表信息,即DBF结构*/
DBF * cur_dbf;/*当前表指针*/
DBF * tempDbf = (DBF *)malloc(sizeof(DBF));/*临时表,用于创建表*/
DBF * dbfPtr = NULL;
int * opened_dbf_count = (int *)malloc(sizeof(int));/*打开的表数*/
int i;
char * commond = (char *)malloc(MAX_COMMOND_COUNT*MAX_COMMOND_LENGTH * sizeof(char));
char open_style[MAX_OPENED_COUNT];/*记录各个打开的表的打开方式*/
FILE * opened_fp[MAX_OPENED_COUNT];/*记录表的文件指针*/
InitializeDbf(tempDbf);
* opened_dbf_count = 0;
cur_dbf = tempDbf;
for (i=0; i<MAX_OPENED_COUNT; i++)
{
dbfPtr = &(dbf[i]);
InitializeDbf(dbfPtr);
open_style[i] = 'r';
opened_fp[i] = NULL;
}
welcome();
while (1) {
printf("RDBSM>");
switch (decipher(commond)) {
case ERROR:
break;
case HELP:
Help();
break;
case EXIT:
Exit(dbf);
break;
case CREATE:
if (Create(tempDbf,commond) != SUCCESS)
{
puts(" 创建失败!");
}
break;
case OPEN:
/*判断已打开的关系数是否超过规定的最大数目*/
if ((*opened_dbf_count) >= MAX_OPENED_COUNT)
{
puts(" 错误,打开的关系已超过MAX_OPENED_COUNT个!!!");
puts(" 请先关闭一个");
break;
}
i = Open(dbf,cur_dbf,commond,open_style);
if (i >= 0)
{/*设置当前表指针和打开方式*/
cur_dbf = &(dbf[i]);
switch (open_style[i]) {
case 'r':
opened_fp[i] = fopen(cur_dbf->dhName,"rb");
cur_dbf->dhHandle = fileno(opened_fp[i]);
fseek(opened_fp[i],sizeof(DBF),0);
break;
case 'w':
opened_fp[i] = fopen(cur_dbf->dhName,"rb+");
cur_dbf->dhHandle = fileno(opened_fp[i]);
fseek(opened_fp[i],sizeof(DBF),0);
break;
default :
opened_fp[i] = fopen(cur_dbf->dhName,"rb+");
cur_dbf->dhHandle = fileno(opened_fp[i]);
fseek(opened_fp[i],sizeof(DBF),0);
break;
}
(*opened_dbf_count)++;
cur_dbf->dhRecNO = 0;
}
else
puts(" 打开失败!!!");
break;
case CLOSE:
i = Close(dbf,commond);
if (i >= 0)
{
opened_fp[i] = NULL;
open_style[i] = 'r';
(*opened_dbf_count)--;
for (i=0; i<MAX_OPENED_COUNT; i++)
if (dbf[i].dhID != -1)
{
cur_dbf = &(dbf[i]);
printf(" 当前关系为%s\n",cur_dbf->dhName);
}
}
else
puts(" 关闭失败!!!");
break;
case USE:
i = Use(dbf, commond);
if (i != -1)
{
cur_dbf = &(dbf[i]);
printf(" \n使用关系 %s\n",cur_dbf->dhName);
}
else
{
puts(" 未找到该关系,该关系可能还未打开,请用open命令打开");
}
break;
case INSERT:
if (open_style[cur_dbf->dhID] == 'r')
{
puts(" the table is opened with r style, can not be inserted records!!");
break;
}
Insert(cur_dbf,commond,opened_fp);
break;
case DELETE:
if (open_style[cur_dbf->dhID] == 'r')
{
puts(" the table is opened with r style, can not delete a record!!");
break;
}
Delete(cur_dbf,commond,opened_fp);
break;
case SELECT:
if (open_style[cur_dbf->dhID] == 'w')
{
puts(" the table is opened with w style, can not be select!!");
break;
}
Select(cur_dbf,commond,opened_fp);
break;
case UPDATE:
if (open_style[cur_dbf->dhID] == 'r')
{
puts(" the table is opened with r style, can not update a record!!");
break;
}
Update(cur_dbf,commond,opened_fp);
case SEEK:
Seek(cur_dbf,commond,opened_fp);
default:
break;
}
}
}
void welcome()
{
puts(" 欢迎使用!!!");
puts("****************************RDBMS*************************************");
puts("|* Description: 〈数据库〉实验 |");
puts("|* Copyright: Copyright (c) 2006 |");
puts("|* Company: BJTU |");
puts("|* @author HuangHan NO.03281159 JK0306 |");
puts("|* @version 1.0 |");
puts("| |");
puts("| |");
puts("|* Enter \"HELP\" to get commonds help |");
puts("**********************************************************************");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -