diff --git a/function_add.cpp b/function_add.cpp new file mode 100644 index 0000000..68c6e13 --- /dev/null +++ b/function_add.cpp @@ -0,0 +1,365 @@ +#include"head.h" + +std::string Upper(std::string input) +{ + //std::string x;//作为最后返回的大写模式 + + for (int i = 0; i < input.size(); i++) + { + if (input[i] >= 'a' && input[i] <= 'z') + input[i] = input[i] + 'A' - 'a'; + } + + return input; +} + +void MyCorrect(std::string input) +{ + //当已经确定输入有这个judge点错误的时候 + std::string standard[STANDARD] = {"CREATE","DROP","TABLE","INSERT","DELETE", + "UPDATE","SELECT","QUIT","FROM","WHERE", + "ORDER","BY","SET","ASC","DESC", + "DISTINCT","INTO","VALUES"}; + + for (int which_standard = 0; which_standard < STANDARD; which_standard++)//将所有的标准进行遍历 + { + int weight = 0; + for (int i = 0; i < input.length() && i < standard[which_standard].length(); i++)//将这个作为input遍历的点 + { + if (input[i] == standard[which_standard][i]) + weight++; + } + //这时候要对权数进行计算 + if ((double)weight >= (double)(standard[which_standard].length() * 0.5))//这时候有超过50%的相似度 + { + std::cout<<"您输入的“"< mode; + Table *p = HEAD; + + order1 = OrderDivide(input, ' ', 1); + which_get = input;//将属性写入 + order2 = OrderDivide(order1, ' ', 1); + judge = order1; + judge = Upper(judge); + + if (judge != "FROM") + { + std::cout<<"语法错误"<next) + if (p->table_name == name) + break; + if (p == NULL) + { + std::cout<<"表“"<table_head.size(); i++) + { + if (p->table_head[i] == which_get) + { + which = i; + break; + } + } + if (which == -1)//还是初始化的值 + { + std::cout<<"属性“"< data;//作为储存数据的容器 + double mid;//作为中间的变量进行操作 + + for (int i = 0; i < p->all_data.size(); i++) + { + int point_count = 0; + int j = 0; + for (; j < p->all_data[i][which].length(); j++) + { + if (p->all_data[i][which][j] >= '0' && p->all_data[i][which][j] <= '9') + continue; + else if (p->all_data[i][which][j] == '.') + point_count++; + else + break; + } + if (point_count > 1 || j < p->all_data[i][which].length()) + { + std::cout<<"该属性下的数据不全为数字!"<all_data[i][which].c_str());//将这一个数据变成double + data.push_back(mid); + } + } + + //下面进行模式的判断 + + + for (int t = 0; t < mode.size(); t++) + { + if (mode[t] == "MAX") + { + double max = -10000000; + for (int i = 0; i < data.size(); i++) + { + if (data[i] > max) + max = data[i]; + } + std::cout< max) + max = data[i]; + } + + double min = 10000000; + for (int i = 0; i < data.size(); i++) + { + if (data[i] < min) + min = data[i]; + } + + double range = max - min; + std::cout< which_to_search;//捕获最后的所有属性 + Table *p = HEAD; + + order1 = OrderDivide(input, ' ', 1); + key = input; + key = Upper(key);//强制转换为大写 + + order2 = OrderDivide(order1, ' ', 1); + judge = order1; + judge = Upper(judge); + if (judge != "FROM") + { + std::cout<<"语法错误"<next) + if (p->table_name == name) + break; + if (!p) + { + std::cout<<"表“"< which;//作为查找哪些属性的标记,int型 + for (int i = 0; i < which_to_search.size(); i++) + { + for (int j = 0; j < p->table_head.size(); j++) + { + if (p->table_head[j] == which_to_search[i]) + which.push_back(j);//将这个属性的位置压入 + } + } + if (which.size() < which_to_search.size()) + { + std::cout<<"属性不对等"< which_to_print;//作为打印数据标记 + + for (int condition = 10; which_to_print.size() < num && condition > 0; condition--) + //这就是标记权的限度以及决定函数终止与否的函数 + { + for (int i = 0; i < p->all_data.size(); i++)//对于原数据行的遍历 + { + //std::cout<all_data[i][which[j]]);//将数据全部转化为大写 + std::vector record;//记录原来数据的哪些位次被记录了 + for (int key_i = 0; key_i < key.length(); key_i++)//对关键词的每一个字符进行遍历 + { + for (int data_i = 0; data_i < p->all_data[i][which[j]].length(); data_i++)//作为对原来的数据每一位进行的遍历 + { + bool x = true; + for (int record_i = 0; record_i < record.size(); record_i++) + //观察这一位的字符是否经过了遍历 + { + if (data_i == record[record_i]) + { + x = false; + break;//这种情况就是说明已经重复 + } + } + if (!x) + continue; + //下面的操作就是说明已经确定了这个字母没有被选中过 + else + { + if (mid[data_i] == key[key_i])//如果这时候的这一位等于key中的某一位 + { + record.push_back(data_i); + break; + } + } + } + } + like = record.size(); + if ((double)like / (double)key.length() >= (double)condition / 10.0)//计算权比例是否超过预期 + { + //std::cout<<(double)like / (double)key.length()<<" "<<(double)condition / 10.0<<"|"; + which_to_print.push_back(i); + } + } + } + } + } + + std::vector > data_to_print;//作为最后进行输出的数据表 + for (int i = 0; i < which_to_print.size(); i++) + { + data_to_print.push_back(p->all_data[which_to_print[i]]); + } + + this->print_data(p->table_head, data_to_print); + + + + return; +} \ No newline at end of file diff --git a/function_build.cpp b/function_build.cpp new file mode 100644 index 0000000..863e67b --- /dev/null +++ b/function_build.cpp @@ -0,0 +1,1736 @@ +#include"head.h" + + +Data_Table::Data_Table() +{ + HEAD = NULL;//将头指针设定为0,已满足之后的操作 + filename_head = "./DATA/"; + condition_filename = filename_head + "condition.txt"; + +} + +void Data_Table::ini()//这是初始化数据库的操作 +{ + Table *TAIL = HEAD; + if (TAIL) + for (; TAIL->next != NULL; TAIL = TAIL->next); + FILE *file = fopen(condition_filename.c_str(), "r"); + if (file == NULL) + { + std::cout<<"维护文件打开失败"< mid; + //作为中转储存 + + fgets(str, MAXLEN, file); + while (!feof(file)) + { + mid = ListDivide(str, '|'); + condition.push_back(mid);//压入其中 + if (mid.size() < 2) + { + std::cout<<"维护文件损坏"<table_file_name = condition[i][1]; + p->table_name = condition[i][0]; + p->next = NULL; + + char file_get[MAXLEN];//将作为所有的文件读取的操作 + fgets(file_get, MAXLEN, file); + //p->table_head.assign(ListDivide(file_get, ' ').begin(), ListDivide(file_get, ' ').end());//将第一行的表头写入节点 + p->table_head = ListDivide(file_get, ' '); + p->head_num = p->table_head.size(); + + std::vector data_get; + fgets(file_get, MAXLEN, file); + while (!feof(file)) + { + + //data_get.assign(ListDivide(file_get, ' ').begin(),ListDivide(file_get, ' ').end()); + data_get = ListDivide(file_get, ' '); + + p->all_data.push_back(data_get);//将新得到的数据压入容器 + + data_get.clear();//清楚捕获区 + + fgets(file_get, MAXLEN, file); + + + } + + + fclose(file); + + + if (!HEAD) + HEAD = p; + else + TAIL->next = p; + TAIL = p; + //将节点进行挂载 + this->print_head(p); + //std::cout<table_head[5]; + } + return; +} + +void Data_Table::condition_save() +{ + FILE *file = fopen(condition_filename.c_str(), "w"); + + if (file == NULL) + { + std::cout<<"维护文件损坏,请检查"< every_max;//作为每一列的最大长度的储存 + + for (int i = 0; i < p->head_num; i++) + { + every_max.push_back(p->table_head[i].length());//作为初始长度 + } + + /* + for (int i = 0; i < p->all_data.size(); i++) + { + for (int j = 0; j < p->head_num; j++) + { + if (p->all_data[i][j].length() > every_max[j]) + every_max[j] = p->all_data[i][j].length(); + } + } + */ + + //已经知道了每一列的最大长度 + + //下面打算先生成表的第一行边框 + + std::cout<<"+--"; + std::cout<<"+"; + for (int i = 0; i < p->head_num; i++) + { + for (int j = 0; j < every_max[i]; j++) + std::cout<<"-"; + std::cout<<"+"; + } + std::cout<head_num; i++) + { + std::cout<table_head[i]; + std::cout<<"|"; + } + std::cout<head_num; i++) + { + for (int j = 0; j < every_max[i]; j++) + std::cout<<"-"; + std::cout<<"+"; + } + std::cout<table_file_name.c_str(), "w"); + if (!file) + { + std::cout<<"文件打开失败!"<head_num; i++) + { + fprintf(file, "%s ", p->table_head[i].c_str()); + } + fprintf(file, "%s", "\n");//加入回车符 + + + for (int i = 0; i < p->all_data.size(); i++) + { + for (int j = 0; j < p->head_num; j++) + { + fprintf(file, "%s ", p->all_data[i][j].c_str()); + } + fprintf(file, "%s", "\n");//补充回车符 + } + + fclose(file); + + + return; +} + +void Data_Table::create(char *input) +{ + //std::cout<<"what"<next != NULL; TAIL = TAIL->next); + char *order1; + char *order2;//使用两个指针进行交替的储存 + std::string judge;//用于一些关键词语句的判定 + + order1 = OrderDivide(input, ' ', 1); + judge = input; + judge = Upper(judge); + //std::cout< table_head_get;//从操作中得到的表头 + order2 = OrderDivide(order1, ' ', 1); + name = order1;//将截取下来的片段进行记录 + + //下面就要进行两种打开方式的分类 + + if (*order2 == '(')//这就是新建数据表的形式 + { + order2++;//将order2移过一个位置 + + order1 = OrderDivide(order2, ')', 1);//将整个需要分析的语句截取出来 + if (*order1 != ' ') + { + std::cout<<"语法错误"<table_file_name = filename_head + filename; + p->table_name = name; + p->table_head = table_head_get; + p->head_num = table_head_get.size(); + p->next = NULL; + //将前4个可以进行初始化的数据进行了初始化 + + //这就完成了一个节点的搭建 + + //下面还需要写入一个文件 + + FILE *file = fopen(p->table_file_name.c_str(), "w+"); + if (!file) + { + std::cout<<"文件打开失败!"<table_head.size(); i++) + fprintf(file, "%s ", p->table_head[i].c_str()); + fprintf(file, "%c", '\n'); + + fclose(file); + + if (!HEAD) + HEAD = p; + else + TAIL->next = p; + TAIL = p; + + this->print_head(p); + + } + + else//已有的数据表的挂载,这时候需要对name进行抓取并且进行之后的操作 + { + //order1 = OrderDivide(order2, ' ', 1); + name = order1;//将表的名字抓取成功 + + order1 = OrderDivide(order2, ' ', 1); + judge = order2;//判定是否为FROM + judge = Upper(judge); + //std::cout<table_file_name = filename_head + filename; + p->table_name = name; + p->next = NULL; + + + char file_get[MAXLEN];//将作为所有的文件读取的操作 + fgets(file_get, MAXLEN, file); + //p->table_head.assign(ListDivide(file_get, ' ').begin(), ListDivide(file_get, ' ').end());//将第一行的表头写入节点 + p->table_head = ListDivide(file_get, ' '); + + + p->head_num = p->table_head.size(); + + + std::vector data_get; + fgets(file_get, MAXLEN, file); + while (!feof(file)) + { + + //data_get.assign(ListDivide(file_get, ' ').begin(),ListDivide(file_get, ' ').end()); + data_get = ListDivide(file_get, ' '); + + p->all_data.push_back(data_get);//将新得到的数据压入容器 + + data_get.clear();//清楚捕获区 + + fgets(file_get, MAXLEN, file); + + + } + + fclose(file); + + //将该链表的构造完成,除了基本数据 + + //std::cout<table_head[2]; + + if (!HEAD) + HEAD = p; + else + TAIL->next = p; + TAIL = p; + //将节点进行挂载 + + this->print_head(p); + + //std::cout<table_head[5]; + + } + + condition.clear(); + + for (Table *a = HEAD; a != NULL; a = a->next) + { + std::vector mid; + mid.push_back(a->table_name); + mid.push_back(a->table_file_name); + condition.push_back(mid); + mid.clear(); + } + + this->condition_save(); + + return; +} + +void Data_Table::drop(char *input) +{ + //在原来的数据中已经有了定义了的HEAD和TAIL两个指针分别标记头尾 + + std::string name, judge; + char *order = OrderDivide(input, ' ', 1); + judge = input; + judge = Upper(judge); + name = order; + //分别判定语法和将表名进行抓取 + + if (judge != "TABLE") + { + std::cout<<"语法错误"<table_name == name)//如果是头节点,进行一种特别的操作 + { + HEAD = current->next; + delete current;//对节点进行必要的释放 + } + + //下面就是非头结点的操作 + + else + { + current = current->next; //previous = previous->next;//将头结点过掉 + for (;current != NULL;current = current->next, previous = previous->next) + { + if (current->table_name == name) + { + previous->next = current->next; + delete current; + break; + } + } + } + + if (current == NULL) + { + std::cout<<"表“"< mid; + mid.clear(); + Table *p = HEAD; + for (; p != NULL; p = p->next) + { + //std::vector mid; + mid.push_back(p->table_name); + //std::cout<table_file_name<table_file_name); + //mid.push_back("./DATA/what.txt"); + condition.push_back(mid); + mid.clear(); + } + */ + + for (int i = 0; i < condition.size(); i++) + { + if (condition[i][0] == name) + { + if (i == condition.size() - 1) + { + condition.pop_back(); + break; + } + else + { + for (int ii = i; ii < condition.size() - 1; ii++) + { + condition[i] = condition[i + 1]; + } + condition.pop_back(); + break; + } + } + } + this->condition_save(); + + + + return; +} + +void Data_Table::table_list(char *input) +{ + std::string judge; + + judge = input;//这里应该只有一个参数 + judge = Upper(judge); + + if (judge != "LIST") + { + std::cout<<"语法错误"<next) + total_num++; + //这里就找到了一共有多少个表的问题 + + p = HEAD;//再将指针调回头指针 + + std::cout<<" total:"<next) + { + std::cout<<" "; + std::cout<table_name<<":"; + std::cout<<"("<head_num<<","<all_data.size()<<")"; + std::cout<<"["; + for (int i = 0; i < p->head_num - 1; i++) + { + std::cout<table_head[i]<<","; + } + std::cout<table_head[p->head_num - 1]<<"]"<next) + { + if (p->table_name == name) + break; + } + + if (p == NULL) + { + std::cout<<"表“"< add;//这里就是表示添加的信息流 + + order2++; + order1 = OrderDivide(order2, ')', 1);//将整个需要分析的语句提取出来 + if (*order1 != ' ') + { + std::cout<<"语法错误"< add_head = ListDivide(order2, ','); + //已经将需要的添加的表头进行了抓取 + + order1++; + order2 = OrderDivide(order1, ' ', 1); + judge = order1; + judge = Upper(judge); + if (judge != "VALUES" || *order2 != '(') + { + std::cout<<"语法错误"< add_data = ListDivide(order2, ','); + + //已经将需要的数据全部抓取完毕! + + for (int i = 0; i < p->head_num; i++) + { + add.push_back("NULL");//将每一个位置进行初始化 + } + + + int i = 0;//作为标记总遍历的标记 + int j = 0;//作为抓取数据的遍历 + int data_max = add_data.size(); + int head_max = add_head.size(); + + if (data_max != head_max) + { + std::cout<<"属性与数据不对应"<head_num; i++) + { + if (p->table_head[i] == add_head[j]) + { + add[i] = add_data[j]; + j++;//将指针指向下一位 + } + } + + if (j != head_max) + { + std::cout<<"添加表的属性与原属性不相符"<all_data.push_back(add);//将这一项压入表 + + + add.clear(); + + this->save(p); + + this->print_data(p->table_head, p->all_data); + + } + + else//如果是简单的全插入 + { + order1 = OrderDivide(order2, ' ', 1); + judge = order2;//抓取判定 + judge = Upper(judge); + + if (judge != "VALUES" || *order1 != '(') + { + std::cout<<"语法错误"< add = ListDivide(order1, ','); + + if (add.size() != p->head_num) + { + std::cout<<"数据参数过少"<all_data.push_back(add);//进行压入 + + this->save(p); + + add.clear(); + this->print_data(p->table_head, p->all_data); + + return; + } + + + return; +} + +void Data_Table::my_delete(char *input) +{ + char *order1, *order2;//作为命令的记录 + std::string name, judge; + + order1 = OrderDivide(input, ' ', 1); + judge = input; + judge = Upper(judge); + + if (judge == "*") + { + order2 = OrderDivide(order1, ' ', 1); + judge = order1; + name = order2; + judge = Upper(judge); + + if (judge != "FROM") + { + std::cout<<"语法错误"<next) + { + if (p->table_name == name) + break; + } + + if (!p) + { + std::cout<<"表“"<all_data.clear(); + + this->save(p); + this->print_data(p->table_head, p->all_data); + + return; + } + + else if (judge == "FROM") + { + order2 = OrderDivide(order1, ' ', 1); + name = order1; + + + Table *p = HEAD; + for (; p != NULL; p = p->next) + { + if (p->table_name == name) + break; + } + + if (!p) + { + std::cout<<"表“"<head_num; j++) + { + if (which == p->table_head[j]) + break; + } + if (j == p->head_num) + { + std::cout<<"没有这一个属性"<all_data.size(); i++)//对于数据进行遍历 + { + if (p->all_data[i][j] == what) + { + if (i == p->all_data.size() - 1) + { + p->all_data.pop_back();//这是如果是最后一个数据的话,就进行这种操作 + } + + else + { + for (int ii = i; ii < p->all_data.size() - 1; ii++) + { + p->all_data[ii] = p->all_data[ii+1]; + } + p->all_data.pop_back();//将最后一个节点进行删除 + } + } + } + + this->save(p); + + this->print_data(p->table_head, p->all_data); + + return; + } + + else + { + std::cout<<"语法错误"< head, const std::vector > data) const +{ + int ID_len = 5; + int ID = 1; + std::cout.setf(std::ios::left);//设定为左对齐 + std::vector every_max;//作为每一列的最大长度的储存 + + for (int i = 0; i < head.size(); i++) + { + every_max.push_back(head[i].length());//作为初始长度 + } + + for (int i = 0; i every_max[j]) + every_max[j] = data[i][j].length(); + } + } + + //这样子就知道了每一列的最大长度 + + std::cout<<"+"; + for (int i = 0; i < ID_len; i++) + std::cout<<"-"; + std::cout<<"+"; + for (int i = 0; i < head.size(); i++) + { + for (int j = 0; j < every_max[i]; j++) + std::cout<<"-"; + std::cout<<"+"; + } + std::cout<next) + { + if (p->table_name == name) + break; + } + if (!p) + { + std::cout<<"表“"< column_and_data = ListDividePro(order2, ',');//将每一个属性和数据的赋值数据抓取进来 + int num = column_and_data.size();//知道一共有多少个数据组 + + std::vector column; + std::vector data; + std::vector mid;//作为一个中间的储存 + + for (int i = 0; i < num; i++) + { + char *p = &column_and_data[i][0];//将第一个字符给p进行赋值 + //std::cout< which_to_update;//用这个变量和这之后的操作来进行找出哪几个属性需要替换 + for (int i = 0; i < column.size(); i++) + { + for (int j = 0; j < p->head_num; j++) + { + if (p->table_head[j] == column[i]) + { + which_to_update.push_back(j); + break; + } + } + } + + if (flag)//如果是指定位置的update + { + order2 = OrderDivide(order1, ' ', 1); + judge = order1; + judge = Upper(judge); + //std::cout< which_get = ListDivide(order2, ' '); + + if (which_get.size() != 3) + { + std::cout<<"参数不足"<head_num; which_int++) + { + if (p->table_head[which_int] == which_head) + { + which_if = which_int; + break;//一定要跳出 + } + } + if (which_int == p->head_num) + { + std::cout<<"WHERE所指条件不存在"<all_data.size(); i++) + { + if (p->all_data[i][which_if] == which_data)//如果是符合WHERE的条件的数据条 + { + for (int j = 0; j < which_to_update.size(); j++) + { + //std::cout<<"?"; + p->all_data[i][which_to_update[j]] = data[j]; + } + } + } + //以上所期许的应该是将符合的数据都进行了更新 + } + + else + { + for (int i = 0; i < p->all_data.size(); i++) + { + for (int j = 0; j < which_to_update.size(); j++) + { + p->all_data[i][which_to_update[j]] = data[j]; + } + } + } + + this->save(p); + + this->print_data(p->table_head, p->all_data); + + std::cout<<" UPDATE成功"< a, std::vector b, int which) +{ + //bool flag; + return (strcmp(a[which].c_str(), b[which].c_str()) > 0); +} + +void Data_Table::swap(std::vector &a, std::vector &b) +{ + std::vector mid = a; + a = b; + b = mid; + return; +} + + +void Data_Table::select(char *input) +{ + char *order1, *order2; + std::string name, judge; + order1 = OrderDivide(input, ' ', 1); + judge = input; + judge = Upper(judge); + Table *p = HEAD; + bool flag = false; + std::vector > data; + std::vector data_head; + char *to_file; + std::vector > mid_data; + + if (judge == "*")//这就是全部输出的模式 + { + order2 = OrderDivide(order1, ' ', 1); + judge = order1; + judge = Upper(judge); + if (judge != "FROM") + { + std::cout<<"语法错误,缺少关键词“FROM”"<next) + { + if (p->table_name == name) + break; + } + if (!p) + { + std::cout<<"表“"< column = ListDivide(order1, ' '); + //std::cout<head_num; i++) + { + if (column[0] == p->table_head[i]) + { + which = i; + break; + } + } + if (which == -1) + { + std::cout<<"属性值有偏差"<all_data.size(); i++) + { + if (p->all_data[i][which] == column[2]) + { + data.push_back(p->all_data[i]); + //std::cout<table_head; + + //已经转站完毕 + if (*order2 == 'T') + { + flag = true; + to_file = order2; + } + } + + //************************************************************* + + else//(*order1 == '\0' || *order1 == 'T')//如果名字之后是空,或者是tofile + { + data = p->all_data; + data_head = p->table_head; + if (*order1 == 'T') + { + to_file = order1; + flag = true; + } + } + + this->print_data(data_head, data); + } + + else//这时候是排序的操作!!!!!!!!!!!!!!!!!!!!!!!!!! + { + order2 = OrderDivide(order1, ' ', 2); + judge = order1; + judge = Upper(judge); + if (judge != "ORDER BY") + { + std::cout<<"语法错误,缺少关键词ORDER BY"< column_get = ListDivide(order2, ','); + order2 = OrderDivide(order1, ' ', 1); + std::vector mode_get = ListDivide(order1, '|'); + data_head = p->table_head; + std::vector > mid_data; + + //***************************************************** + if (*order2 == 'W') + { + //这里就是简单的部分展示 + order1 = OrderDivide(order2, ' ', 1); + judge = order2; + judge = Upper(judge); + if (judge != "WHERE") + { + std::cout<<"语法错误,缺少关键词“WHERE”"< column = ListDivide(order1, ' '); + if (column.size() != 3) + { + std::cout<<"参数不足"<head_num; i++) + { + if (column[0] == p->table_head[i]) + { + which = i; + break; + } + } + if (which == -1) + { + std::cout<<"属性值有偏差"<all_data.size(); i++) + { + if (p->all_data[i][which] == column[2]) + data.push_back(p->all_data[i]); + } + + //已经转站完毕 + + if (*order2 == 'T') + { + flag = true; + to_file = order2; + } + } + + //********************************************************** + else + { + if (*order2 == 'T') + { + flag = true; + to_file = order2; + } + data = p->all_data; + } + + + std::vector which; + std::vector mode; + //分别作为哪一列和哪一种模式的标志 + + for (int i = 0; i < column_get.size(); i++) + { + for (int j = 0; j < p->head_num; j++) + { + if (p->table_head[j] == column_get[i]) + { + which.push_back(j); + break; + } + } + } + if (which.size() != column_get.size()) + { + std::cout<<"存在未知属性"< 0; i--)//这时候需要的是第i和第i-1 + { + int check = 0; + for (; check < t; check++) + { + //std::cout<<"?"; + if (this->compare(data[i], data[i-1], which[check]) != 0) + { + //std::cout<<"?"; + break; + } + } + if (check == t)//前面的属性都一样,进行后一个属性的排序 + { + if (mode[t] == 1)//升序 + { + if (this->compare(data[i], data[i-1], which[t]) > 0) + this->swap(data[i], data[i-1]); + } + else + { + if (this->compare(data[i], data[i-1], which[t]) < 0) + this->swap(data[i], data[i-1]); + } + } + } + } + } + + this->print_data(data_head, data); + + } + + } + //这之中有return; + + else if (Upper(judge) == "DISTINCT") + { + order2 = OrderDivide(order1, ' ', 1); + std::vector head_get = ListDivide(order1, ',');//将所有的属性读入 + order1 = OrderDivide(order2, ' ', 1); + judge = order2; + judge = Upper(judge); + if (judge != "FROM") + { + std::cout<<"语法错误,缺少关键词“FROM”"<next) + { + if (p->table_name == name) + break; + } + if (!p) + { + std::cout<<"表“"< head; + for (int i = 0; i < head_get.size(); i++) + { + for (int j = 0; j < p->head_num; j++) + { + if (p->table_head[j] == head_get[i]) + { + head.push_back(j);//将位置进行记录 + break; + } + } + } + if (head.size() != head_get.size()) + { + std::cout<<"属性不对等"<all_data.size(); i++) + { + bool x = true; + for (int j = 0; j < data.size(); j++)//对于已有的data进行遍历 + { + int which = 0; + for (; which < head.size(); which++) + { + if (data[j][which] != p->all_data[i][head[which]]) + break; + } + if (which == head.size()) + { + x = false; + break; + } + } + + if (x) + { + std::vector mid; + for (int which = 0; which < head.size(); which++) + { + mid.push_back(p->all_data[i][head[which]]); + } + data.push_back(mid); + mid.clear(); + } + } + + this->print_data(data_head,data); + + } + + else//这时候是input就是属性的开始,order1已经越过了所有的属性 + { + std::vector head_get = ListDivide(input, ','); + //将属性领入其中 + order2 = OrderDivide(order1, ' ', 1); + judge = order1; + judge = Upper(judge); + if (judge != "FROM") + { + std::cout<<"语法错误,缺少关键词“FROM”"<next) + { + if (p->table_name == name) + break; + } + if (!p) + { + std::cout<<"表“"< head; + + for (int i = 0; i < head_get.size(); i++) + { + for (int j = 0; j < p->table_head.size(); j++) + { + if (p->table_head[j] == head_get[i]) + { + head.push_back(j); + break; + } + } + } + if (head.size() != head_get.size()) + { + std::cout<<"属性不对等"< column = ListDivide(order2, ' '); + if (column.size() != 3) + { + std::cout<<"参数不足"<head_num; i++) + { + if (column[0] == p->table_head[i]) + { + which = i; + break; + } + } + if (which == -1) + { + std::cout<<"属性值有偏差"<all_data.size(); i++) + { + if (p->all_data[i][which] == column[2]) + mid_data.push_back(p->all_data[i]); + } + + //已经转站完毕 + + if (*order1 == 'T') + { + flag = true; + to_file = order1; + } + } + + else + { + mid_data = p->all_data; + + if (*order1 == 'T') + { + flag = true; + to_file = order1; + } + } + + for (int i = 0; i < mid_data.size(); i++) + { + std::vector mid; + for (int j = 0; j < head.size(); j++) + { + mid.push_back(mid_data[i][head[j]]); + } + data.push_back(mid); + mid.clear(); + } + //将所有的东西进行压入 + + this->print_data(data_head, data); + } + + //下面有一个文件的写入这个小操作 + + + + if (flag) + { + order1 = OrderDivide(to_file, ' ', 1); + judge = to_file; + judge = Upper(judge); + if (judge != "TO") + { + std::cout<<"语法错误,缺少关键词“TO“"<save_pro(data_head, data, filename); + std::cout<<"文件写入成功"< head, const std::vector > data,const std::string filename) const +{ + FILE *file;//作为文件的指针操作 + + + file = fopen((filename_head + filename).c_str(), "w"); + if (!file) + { + std::cout<<"文件打开失败!"< +#include +#include +#include +#include +#include +#include//作为setw设定对齐的头文件 +#include + + +#define MAXLEN 1000 +#define STANDARD 18 + + +char* OrderDivide(char *p, char key, int num); + +std::vector ListDivide(char *p, char key); + +std::vector ListDividePro(char *p, char key); + +std::string Upper(std::string input); + +void MyCorrect(std::string input); + +struct Table +{ + std::string table_file_name; + std::string table_name; + std::vector table_head; + int head_num;//作为属性的数目 + std::vector > all_data; + Table *next; + +}; + +class Data_Table +{ +private: + std::vector > condition; + std::string condition_filename; + //前两个是对于数据库的维护文件的相关操作 + std::string filename_head;//用于文件名的头 + Table *HEAD; +public: + Data_Table(); + void ini(); + void condition_save(); + void save(const Table *p) const; + void save_pro(const std::vector head, const std::vector > data,const std::string filename) const; + void print_head(const Table *p) const; + void create(char *input); + void drop(char *input); + void table_list(char *input); + void insert(char *input); + void my_delete(char *input); + void print_data(const std::vector head, const std::vector > data) const; + void update(char *input); + void select(char *input); + int compare(std::vector a, std::vector b, int which); + void swap(std::vector &a, std::vector &b); + void compute(char *inupt); + void search(char *input); +}; \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..b2a6a6d --- /dev/null +++ b/main.cpp @@ -0,0 +1,82 @@ +#include +#include + +#include +#include +#include"head.h" + + +using namespace std; + + +int main() +{ + char input[MAXLEN]; + char *secondinput;//作为二级输入的指针进行储存 + string judge;//作为判断第一个关键词的词储存 + + char first[MAXLEN]; + string firstjudge;//决定是否进去 + + cout<<"~$ "; + cin>>first;//这是作为进入的判定 + + firstjudge = first; + + firstjudge = Upper(firstjudge); + + if (firstjudge != "MYSQL") + { + cout<<"无法进入"< 初始化"<"; + cin.getline(input, MAXLEN, '\n');//默许的输入语句长度为1000; + //cout< ListDivide(char *p, char key) +{ + std::vector list; + std::string mid;//作为中间的转换 + char *order = p; + + /* + for (; *(pp+1) != '\0'; pp++); + if (*pp == '\n') + { + *pp = '\0'; + std::cout<<"?"; + } + */ + + + while(*order != '\0') + { + order = OrderDivide(order, key, 1); + mid = p; + for (int i = 0; i < mid.length(); i++) + { + if (mid[i] == '\n') + mid[i] = ' '; + } + + list.push_back(mid);//将这个属性压进容器 + p = order; + } + + if (list[list.size()-1] == " ") + list.pop_back(); + + //if (list[list.size()-1][list[(list.size()-1)].length()-1] == '\n') + //list[list.size()-1][list[(list.size()-1)].length()-1] = ' '; + + + return list; +} + +std::vector ListDividePro(char *p, char key) +{ + std::vector list; + std::string mid;//作为中间的转换 + char *order = p; + + /* + for (; *(pp+1) != '\0'; pp++); + if (*pp == '\n') + { + *pp = '\0'; + std::cout<<"?"; + } + */ + + bool flag = true; + + while(*order != '\0') + { + order = OrderDivide(order, key, 1); + p = p + 1; + if (flag) + { + flag = false; + p = p - 1; + } + mid = p; + + list.push_back(mid);//将这个属性压进容器 + p = order; + } + + + return list; +} \ No newline at end of file