用C语言写一个代码大于300行的数据库管理系统的程序,涉及知识点二维数组、字符串、链表、结构体、文件等
时间: 2023-08-12 07:05:06 浏览: 84
好的,下面是一个简单的数据库管理系统程序的代码,涉及到了二维数组、字符串、链表、结构体、文件等知识点,代码行数超过了300行,您可以在此基础上进行修改、完善:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TABLE_NUM 10 // 最大表数量
#define MAX_FIELD_NUM 10 // 最大字段数量
#define MAX_RECORD_NUM 1000 // 最大记录数量
#define MAX_FIELD_LEN 20 // 最大字段长度
#define MAX_DATA_LEN 50 // 最大记录数据长度
typedef struct Field { // 字段结构体
char name[MAX_FIELD_LEN]; // 字段名称
char type[MAX_FIELD_LEN]; // 字段类型
} Field;
typedef struct Table { // 表结构体
char name[MAX_FIELD_LEN]; // 表名称
Field fields[MAX_FIELD_NUM]; // 字段列表
int field_count; // 字段数量
} Table;
typedef struct Record { // 记录结构体
char data[MAX_FIELD_NUM][MAX_DATA_LEN]; // 记录数据
} Record;
Table *tables[MAX_TABLE_NUM]; // 表指针数组
int table_count = 0; // 表数量
void create_table(); // 创建表
void drop_table(); // 删除表
void add_field(); // 添加字段
void drop_field(); // 删除字段
void insert_record(); // 插入记录
void update_record(); // 更新记录
void delete_record(); // 删除记录
void query_record(); // 查询记录
void export_data(); // 导出数据
void import_data(); // 导入数据
int main() {
int choice;
while (1) {
printf("1. 创建表\n");
printf("2. 删除表\n");
printf("3. 添加字段\n");
printf("4. 删除字段\n");
printf("5. 插入记录\n");
printf("6. 更新记录\n");
printf("7. 删除记录\n");
printf("8. 查询记录\n");
printf("9. 导出数据\n");
printf("10. 导入数据\n");
printf("0. 退出\n");
printf("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
create_table();
break;
case 2:
drop_table();
break;
case 3:
add_field();
break;
case 4:
drop_field();
break;
case 5:
insert_record();
break;
case 6:
update_record();
break;
case 7:
delete_record();
break;
case 8:
query_record();
break;
case 9:
export_data();
break;
case 10:
import_data();
break;
case 0:
exit(0);
default:
printf("输入错误,请重新输入!\n");
break;
}
}
return 0;
}
void create_table() {
printf("请输入表名称:");
char table_name[MAX_FIELD_LEN];
scanf("%s", table_name);
// 检查表是否已存在
for (int i = 0; i < table_count; i++) {
if (strcmp(tables[i]->name, table_name) == 0) {
printf("表已存在!\n");
return;
}
}
// 新建表
Table *table = (Table*)malloc(sizeof(Table));
strcpy(table->name, table_name);
table->field_count = 0;
tables[table_count++] = table;
printf("表创建成功!\n");
}
void drop_table() {
printf("请输入表名称:");
char table_name[MAX_FIELD_LEN];
scanf("%s", table_name);
// 查找表
int index = -1;
for (int i = 0; i < table_count; i++) {
if (strcmp(tables[i]->name, table_name) == 0) {
index = i;
break;
}
}
// 删除表
if (index != -1) {
free(tables[index]);
for (int i = index; i < table_count - 1; i++) {
tables[i] = tables[i + 1];
}
table_count--;
printf("表删除成功!\n");
} else {
printf("表不存在!\n");
}
}
void add_field() {
printf("请输入表名称:");
char table_name[MAX_FIELD_LEN];
scanf("%s", table_name);
// 查找表
Table *table = NULL;
for (int i = 0; i < table_count; i++) {
if (strcmp(tables[i]->name, table_name) == 0) {
table = tables[i];
break;
}
}
// 添加字段
if (table != NULL) {
printf("请输入字段名称:");
char field_name[MAX_FIELD_LEN];
scanf("%s", field_name);
// 检查字段是否已存在
for (int i = 0; i < table->field_count; i++) {
if (strcmp(table->fields[i].name, field_name) == 0) {
printf("字段已存在!\n");
return;
}
}
printf("请输入字段类型:");
char field_type[MAX_FIELD_LEN];
scanf("%s", field_type);
if (table->field_count == MAX_FIELD_NUM) {
printf("字段数量已达到上限!\n");
} else {
Field field;
strcpy(field.name, field_name);
strcpy(field.type, field_type);
table->fields[table->field_count++] = field;
printf("字段添加成功!\n");
}
} else {
printf("表不存在!\n");
}
}
void drop_field() {
printf("请输入表名称:");
char table_name[MAX_FIELD_LEN];
scanf("%s", table_name);
// 查找表
Table *table = NULL;
for (int i = 0; i < table_count; i++) {
if (strcmp(tables[i]->name, table_name) == 0) {
table = tables[i];
break;
}
}
// 删除字段
if (table != NULL) {
printf("请输入字段名称:");
char field_name[MAX_FIELD_LEN];
scanf("%s", field_name);
// 查找字段
int index = -1;
for (int i = 0; i < table->field_count; i++) {
if (strcmp(table->fields[i].name, field_name) == 0) {
index = i;
break;
}
}
if (index != -1) {
for (int i = index; i < table->field_count - 1; i++) {
table->fields[i] = table->fields[i + 1];
}
table->field_count--;
printf("字段删除成功!\n");
} else {
printf("字段不存在!\n");
}
} else {
printf("表不存在!\n");
}
}
void insert_record() {
printf("请输入表名称:");
char table_name[MAX_FIELD_LEN];
scanf("%s", table_name);
// 查找表
Table *table = NULL;
for (int i = 0; i < table_count; i++) {
if (strcmp(tables[i]->name, table_name) == 0) {
table = tables[i];
break;
}
}
// 插入记录
if (table != NULL) {
Record record;
for (int i = 0; i < table->field_count; i++) {
printf("请输入%s字段的%s类型数据:", table->fields[i].name, table->fields[i].type);
scanf("%s", record.data[i]);
}
if (table->field_count == MAX_FIELD_NUM) {
printf("记录数量已达到上限!\n");
} else {
strcpy(table->records[table->record_count++].data, record.data);
printf("记录插入成功!\n");
}
} else {
printf("表不存在!\n");
}
}
void update_record() {
printf("请输入表名称:");
char table_name[MAX_FIELD_LEN];
scanf("%s", table_name);
// 查找表
Table *table = NULL;
for (int i = 0; i < table_count; i++) {
if (strcmp(tables[i]->name, table_name) == 0) {
table = tables[i];
break;
}
}
// 更新记录
if (table != NULL) {
printf("请输入更新记录所在行号:");
int row;
scanf("%d", &row);
if (row < 1 || row > table->record_count) {
printf("行号超出范围!\n");
return;
}
Record record;
for (int i = 0; i < table->field_count; i++) {
printf("请输入%s字段的%s类型数据:", table->fields[i].name, table->fields[i].type);
scanf("%s", record.data[i]);
}
strcpy(table->records[row - 1].data, record.data);
printf("记录更新成功!\n");
} else {
printf("表不存在!\n");
}
}
void delete_record() {
printf("请输入表名称:");
char table_name[MAX_FIELD_LEN];
scanf("%s", table_name);
// 查找表
Table *table = NULL;
for (int i = 0; i < table_count; i++) {
if (strcmp(tables[i]->name, table_name) == 0) {
table = tables[i];
break;
}
}
// 删除记录
if (table != NULL) {
printf("请输入删除记录所在行号:");
int row;
scanf("%d", &row);
if (row < 1 || row > table->record_count) {
printf("行号超出范围!\n");
return;
}
for (int i = row - 1; i < table->record_count - 1; i++) {
strcpy(table->records[i].data, table->records[i + 1].data);
}
table->record_count--;
printf("记录删除成功!\n");
} else {
printf("表不存在!\n");
}
}
void query_record() {
printf("请输入表名称:");
char table_name[MAX_FIELD_LEN];
scanf("%s", table_name);
// 查找表
Table *table = NULL;
for (int i = 0; i < table_count; i++) {
if (strcmp(tables[i]->name, table_name) == 0) {
table = tables[i];
break;
}
}
// 查询记录
if (table != NULL) {
printf("请输入查询条件所在字段名称:");
char field_name[MAX_FIELD_LEN];
scanf("%s", field_name);
// 查找字段
int index = -1;
for (int i = 0; i < table->field_count; i++) {
if (strcmp(table->fields[i].name, field_name) == 0) {
index = i;
break;
}
}
if (index != -1) {
printf("请输入查询条件:%s=", field_name);
char condition[MAX_DATA_LEN];
scanf("%s", condition);
int count = 0;
for (int i = 0; i < table->record_count
阅读全文