#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#define TABLE_MAX_LENGH 1000
#define FIELD_MAX_LENGH 256
#define NAME_MAX_LENGH 100
#define DATA_MAX_LETH 1000
#define MAX_LINE 2048
#define MAX_SUBSTRINGS 100
typedef struct field field;
typedef struct record record;
typedef struct table table;
typedef struct fieldList fieldList;
typedef struct recordList recordList;
typedef struct tableArray tableArray;
enum item_type {
INTEGER = 0,
STRING = 1
};
typedef struct field {
enum item_type type;
char name[NAME_MAX_LENGH];
char data[DATA_MAX_LETH];
bool is_key;
field * next;
};
typedef struct record {
fieldList * field_list;
record* next;
};
typedef struct table {
char key[NAME_MAX_LENGH];
recordList* record_list;
};
typedef struct fieldList {
field head;
int length;
};
typedef struct recordList {
record head;
int length;
};
typedef struct tableArray {
table* Table;
int length;
};
void printFixedLengthString(const char* str) {
size_t len = strlen(str);
if (len <= 10) {
// 字符串长度小于等于最大长度,直接输出并在末尾填充
printf("%.*s%*c\t", (int)len, str, 10 - len, ' ');
}
else {
// 字符串长度大于最大长度,截取前maxLength个字符并输出
printf("%.10s\t", str);
}
}
fieldList* fieldList_init(void) {
fieldList* pList = (fieldList*)malloc(sizeof(fieldList));
if (pList == NULL) {
return NULL;
}
pList->head.next = NULL;
pList->length = 0;
return pList;
}
void fieldList_add(fieldList* pList,int index, field* pField) {
if(pList==NULL||pField==NULL){
return;
}
if (index < 0 || index > pList->length) {
index = pList->length;
}
//
field* tmp = &pList->head;
for (int i = 0; i < index; i++)
{
tmp = tmp->next;
}
pField->next = tmp->next;
tmp->next = pField;
pList->length++;
}
//遍历
void forEach_fieldList(fieldList* list) {
if (list == NULL) {
return;
}
field* tmp = list->head.next;
for (size_t i = 0; i < list->length; i++)
{
//printf("%s\n", tmp->data);
printFixedLengthString(tmp->data);
tmp = tmp->next;
}
}
//按位置删
void fieldList_removeByindex(fieldList* list, int index) {
if (list == NULL) {
return;
}
if (index<0 || index>list->length) {
return;
}
field* tmp = &list->head;
for (int i = 0; i < index; i++)
{
tmp = tmp->next;
}
//记录待删除节点
field* pDel = tmp->next;
//重新建立关系
tmp->next = tmp->next->next;
//删除节点
free(pDel);
pDel = NULL;
//更新链表长度
list->length--;
}
void fieldList_clear(fieldList* list) {
size_t length = list->length;
for (size_t i = 0; i < length; i++)
{
fieldList_removeByindex(list, 0);
}
free(list);
list = NULL;
}
recordList* recordList_init(void) {
recordList* pList = (recordList*)malloc(sizeof(recordList));
if (pList == NULL) {
return NULL;
}
pList->head.next = NULL;
pList->length = 0;
return pList;
}
void recordList_add(recordList* pList, int index, record* pRecord) {
if (pList == NULL || pRecord == NULL) {
return;
}
if (index < 0 || index > pList->length) {
index = pList->length;
}
//
record* tmp = &pList->head;
for (int i = 0; i < index; i++)
{
tmp = tmp->next;
}
pRecord->next = tmp->next;
tmp->next = pRecord;
pList->length++;
}
//遍历
void forEach_recordList(recordList* list) {
if (list == NULL) {
return;
}
record* tmp = list->head.next;
for (size_t i = 0; i < list->length; i++)
{
printf_s("record %d\n", i);
forEach_fieldList(tmp->field_list);
printf_s("\n");
tmp = tmp->next;
}
}
void recordList_removeByindex(recordList* list, int index) {
if (list == NULL) {
return;
}
if (index<0 || index>list->length) {
return;
}
record* tmp = &list->head;
for (int i = 0; i < index; i++)
{
tmp = tmp->next;
}
//记录待删除节点
record* pDel = tmp->next;
//重新建立关系
tmp->next = tmp->next->next;
//删除子节点
fieldList_clear(pDel->field_list);
//删除节点
free(pDel);
pDel = NULL;
//更新链表长度
list->length--;
}
tableArray* tableArray_init() {
tableArray* TableArray = (tableArray*)malloc(sizeof(tableArray));
TableArray->length = 0;
TableArray->Table = (table*)malloc(TABLE_MAX_LENGH * sizeof(table));
return TableArray;
}
void tableArray_add(tableArray* TableArray,table* Table) {
if (TableArray == NULL) {
return;
}
if (TableArray->length >= TABLE_MAX_LENGH) {
return;
}
TableArray->Table[TableArray->length] = *Table;
TableArray->length++;
}
void printTableTitle(table* Table) {
fieldList* field_list= Table->record_list->head.next->field_list;
if (field_list == NULL) {
return;
}
size_t length = field_list->length;
field* tmp = field_list->head.next;
for (size_t i = 0; i < length; i++)
{
printFixedLengthString(tmp->name);
tmp = tmp->next;
}
}
//遍历
void forEach_tableArray(tableArray* TableArray) {
if (TableArray == NULL) {
return;
}
for (size_t i = 0; i < TableArray->length; i++)
{
printf_s("table %d\n", i);
printTableTitle(&TableArray->Table[i]);
printf_s("\n");
forEach_recordList(TableArray->Table[i].record_list);
}
}
void test_api() {
printf_s("--------------test_field--------------\n");
////field
fieldList* pList = fieldList_init();
//add 1
field* newField = (field*)malloc(sizeof(field));
strcpy_s(newField->data, 100, "写代码");
newField->next = NULL;
strcpy_s(newField->name, 100, "爱好");
newField->type = INTEGER;
fieldList_add(pList, 0, newField);
//add 2
newField = (field*)malloc(sizeof(field));
strcpy_s(newField->data, 100, "软件学院");
newField->next = NULL;
strcpy_s(newField->name, 100, "院系");
newField->type = INTEGER;
fieldList_add(pList, 0, newField);
forEach_fieldList(pList);
printf_s("--------------insert field on 0 index end \n");
//remove 2
fieldList_removeByindex(pList, 0);
forEach_fieldList(pList);
printf_s("--------------remove field on 0 index end\n");
//add 2
newField = (field*)malloc(sizeof(field));
strcpy_s(newField->data, 100, "软件学院");
newField->next = NULL;
strcpy_s(newField->name, 100, "院系");
newField->type = INTEGER;
fieldList_add(pList, 0, newField);
forEach_fieldList(pList);
printf_s("--------------insert field on 0 index end \n");
printf_s("--------------test_record--------------\n");
//record 1
recordList* record_list = recordList_init();
record* pRecord=(record*)malloc(sizeof(record));
pRecord->next = NULL;
pRecord->field_list = pList;
recordList_add(record_list, 0, pRecord);
forEach_recordList(record_list);
printf_s("--------------add 1 record end\n");
//record 2
fieldList* pList_2 = fieldList_init();
//add 1
field* newField_2 = (field*)malloc(sizeof(field));
strcpy_s(newField_2->data, 100, "备课");
newField_2->next = NULL;
strcpy_s(newField_2->name, 100, "爱好");
newField_2->type = INTEGER;
fieldList_add(pList_2, 0, newField_2);
//add 2
newField_2 = (field*)malloc(sizeof(field));
strcpy_s(newField_2->data, 100, "技术科学实验班");
newField_2->next = NULL;
strcpy_s(newField_2->name, 100, "院系");
newField_2->type = INTEGER;
fieldList_add(pList_2, 0, newField_2);
record* pRecord_2 = (record*)malloc(sizeof(record));
pRecord_2->next = NULL;
pRecord_2->field_list = pList_2;
recordList_add(record_list, 0, pRecord_2);
forEach_recordList(record_list);
printf_s("--------------insert 1 record on index 0 end\n");
//remove record 1
recordList_removeByindex(record_list, 0);
forEach_recordList(record_list);
printf_s("--------------remove 1 record on index 0 end\n");
//add record 2 again
//record 2
pList_2 = fieldList_init();
//add 1
newField_2 = (field*)malloc(sizeof(field));
strcpy_s(newField_2->data, 100, "备课");
newField_2->next = NULL;
strcpy_s(newField_2->name, 100, "爱好");
newField_2->type = INTEGER;
fiel
聚财猫猫
- 粉丝: 248
- 资源: 221
最新资源
- 基于Django Web框架的母婴商城实践项目设计源码
- 一个使用 Go 编程语言和 WebAssembly 构建渐进式 Web 应用程序的包 .zip
- 基于Python桌面画笔的自动画图设计源码
- 基于Java语言的中医通病例问询子系统设计源码
- 基于Java语言的云南旅游主题设计源码
- 基于Java的ExamManageSystem软件详细设计课程设计源码
- 基于Java开发的简洁方便ORM工具BeetlSQL设计源码
- 基于Java语言的Reactor-QL:用SQL简化Reactor API实时数据处理设计源码
- 基于Java的tio-http-server演示学习源码
- 基于Java和C#的C#课程实验与Winform学习及Android实验设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈