数据库课程设计代码

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct Table
{
    string table_name;///表名
    vector column;///列名
    vector table_property;///属性
    vector length;///长度
    vector > constraint;///约束
    vector > data;///数据

};

struct View
{
    string view_name;///视图名
    vector View_proproperty;///视图属性;
};

struct Index
{
    string index_name;///索引名
    vector > index_data;///索引数据
};
class User
{
private:
    string user;///用户名
    string password;///密码
    string Userstatus; ///用户的身份
public:
    vector User_select;///对于用户表的select权限
    vector User_insert;///对于用户表的insert权限
    vector User_delete;///对于用户表的delete权限
    vector User_update;///对于用户表的update权限
    vector User_drop;  ///对于用户表的drop权限

    User()
    {
        user = "root";
        password = "root";
        Userstatus = "user";
    }
    User(string _user,string _password,string _Userstatus,vector_select,vector _insert,vector_delete,vector _update,
         vector _drop)
    {
        user = _user;
        password = _password;
        Userstatus = _Userstatus;
        User_select = _select;///对于用户表的select权限
        User_insert = _insert;///对于用户表的insert权限
        User_delete = _delete;///对于用户表的delete权限
        User_update = _update;///对于用户表的update权限
        User_drop = _drop;
    }
    void setUser(string _user)
    {
        user = _user;
    }
    string getUser()
    {
        return user;
    }
    void setPassword(string _password)
    {
        password = _password;
    }
    string getPassword()
    {
        return password;
    }
    void setUserstatus(string _Userstatus)
    {
        Userstatus = _Userstatus;
    }
    string getUserstatus()
    {
        return Userstatus;
    }

};
class Database
{
private:
    string DatabaseName;///数据库的名字
    string path;///路径
    string Allpath;///全局路径
    string file_root_name;///存储表的txt文件
    string file_view;///存储视图文件名;
    string file_index;///存储索引文件名
    string file_user_name;///存储用户信息的TXT文件
public:
    void CreateDatabase();///数据库
    void CreateTable(Table&,Database,User);///表
    void DeleteTable(string, User,Database);///删除表中的信息
    void DropTable(string,User,Database);///删除表
    void DeleteRow();///删除一个j记录内的信息
    void Addrow();///添加一行
    ///void AddTable();///添加一个表
    void Select(string,User,Database);///选择语句
    void AppointSelect(string ,string ,User ,Database);///
    void UIMSelect(string,string,User,Database,vector,string);///集合查询
    void NESTSelect(string,string,User,Database,vector&);///嵌套查询
    void Insert(string,User,Database);///添加数据
    void Update(string,User,Database);///更新数据
    void Grant(string,User&,Database);///授予用户权限
    void Revoke(string,User&,Database);///收回权限
    Database()
    {
        Allpath = "0";
    }
    string getAllpath()
    {
        return Allpath;
    }
    string getDatabaseName()
    {
        return DatabaseName;
    }
    string getFile_root_name()
    {
        return file_root_name;
    }
    string getFile_view()
    {
        return file_view;
    }
    string getFile_index()
    {
        return file_index;
    }
};


void Database::CreateDatabase()///建数据库
{

    cout<<"Please enter the database name:   ";

    cin>>DatabaseName;
    path="F:\\"+DatabaseName; ///路径=在F盘内,加上数据库的名字
    Allpath = path+"\\"; ///全局记录路径
    path="md " + path; ///MD创建路径
    system(path.c_str()); ///把字符串转入命令
    file_root_name = Allpath + DatabaseName + ".txt";
    file_view = Allpath + "VIEW" + ".txt";
    file_index = Allpath + "INDEX" + ".txt";
    cout<    FILE *fp;
    ofstream file;///定义输出文件
    if((fp = fopen(file_root_name.c_str(),"r+")) != NULL) ///判断按读方式打 file_name 文件是否失败,如果打开
    {
        return;
    }

    file.open(file_root_name.c_str());     ///作为输出文件打开
    file.close();
    file.open(file_view.c_str());
    file.close();
    file.open(file_index.c_str());
    file.close();
}




int AboutTable_property(string table_property)
{
    if(table_property == "int")
        return 0;
    else if(table_property == "char")
        return 0;
    else if(table_property == "double")
        return 0;
    else if(table_property == "varchar")
        return 0;
    else if(table_property == "string")
        return 0;
    else
        return 1;

}

void Database::CreateTable(Table &table,Database database,User user)
/// create table table_name
///column1 属性 长度 <约束>,
///column1 属性 长度 <约束>;
{

    FILE *fp;///定义一个名叫fp文件指针
    string table_name;///表名
    string column;///列名
    string property;///属性
    string terminator;///一行结束符
    //string null;///空值约束
    //string notnull;
    //string primary;///主键约束
    //string foreign;///外键约束
    int  length;///属性长度
    string file_name;///文件名

    cin>>table_name;
    table.table_name = table_name;

    ofstream ofile;///定义输出文件

    file_name = table_name;
    file_name = database.getAllpath() + file_name;
    string fileType = ".txt";
    file_name = file_name + fileType;

    while((fp = fopen(file_name.c_str(),"r+")) != NULL) ///判断按读方式打 file_name 文件是否失败,如果打开
    {
        ///printf("The file can not be opened.\n");
        cout<<"The data table has been established. Please re-establish it :"<        ///exit(1);///结束程序的执行
        cin>>table_name;
        table.table_name = table_name;
        file_name = table_name;
        file_name = database.getAllpath() + file_name;
        string fileType = ".txt";
        file_name = file_name + fileType;
    }
    ofile.open(database.getFile_root_name().c_str(),ios::app);///存储表名
    ofile<    ofile.close();
    ofile.open(file_name.c_str(),ios::app);///作为输出文件打开


    cin>>column;

    while(column!= ";")
    {
        //cin>>property>>length>>null>>notnull>>primary>>foreign;
        cin>>property>>length>>terminator;
        while(AboutTable_property(property))
        {
            cout<<"property error!"<            //cin>>property>>length>>null>>notnull>>primary>>foreign;
            cin>>property>>length>>terminator;
        }
        table.column.push_back(column);
        table.table_property.push_back(property);
        table.length.push_back(length);
        int number = 0;///设计约束的数量即约束的列数
        vector constraint1;///临时记录一行的约束
        while(terminator != "," && terminator != ";")
        {
            transform(terminator.begin(),terminator.end(),terminator.begin(),towupper);
            if(terminator == "NULL" || terminator == "NOTNULL"||terminator == "PRIMARY" || terminator =="FOREIGN")
            {

                constraint1.push_back(terminator);
            }
            //table.constraint.push_back(constraintofhang);
            cin>>terminator;

            number++;
        }
        //cout<<"NUMBER:  "<        if(number == 0)///若没有输入约束项
        {
            if(terminator == ",")
            {
                constraint1.push_back("NOTNULL");
            }
            else if(terminator == ";")
            {
                constraint1.push_back("NOTNULL");
            }
        }
        table.constraint.push_back(constraint1);
        constraint1.clear();
        if(terminator == ";")
        {
            break;
        }
        cin>>column;
        cout<<"BBB"<
    }
    ofile<    ofile<<"- - - - - - - - - - - - - - - - - - - - - - - - -"<    ofile<    //left << setw(10)<<"Null"<    //   left << setw(10)<<"Primary"<    ofile<<"- - - - - - - - - - - - - - - - - - - - - - - - -"<    for(int i =  0; i <  table.column.size(); ++i)
    {
        ofile<        for(int j = 0 ; j < table.constraint[i].size()  ; ++j)
        {
            ofile<        }


        ofile<        //left << setw(10) <    }
    ofile<<"- - - - - - - - - - - - - - - - - - - - - - - - ---"<    for(int i =  0; i <  table.column.size(); ++i)
    {
        ofile<    }
    ofile<    ofile.close();


    ///-----------------------对用户设置对该表的权限-------------------------------------------------///
    user.User_delete.push_back(table_name);
    user.User_drop.push_back(table_name);
    user.User_insert.push_back(table_name);
    user.User_select.push_back(table_name);
    user.User_update.push_back(table_name);
    string path = "F:\\";
    path = path + user.getUser() + ".txt";
    ///----------清除表数据-----///////
    ofile.open(path.c_str(),ios::trunc);///删除表中的数据
    ofile.close();
    ///---------------------------------------------///

    ///------------将数据重新输入-----------------///
    ofile.open(path.c_str(),ios::app);     ///作为输出文件打开
    for(int i = 0 ; i < user.User_select.size(); ++i)
        ofile<    ofile<    for(int i = 0 ; i < user.User_insert.size(); ++i)
        ofile<    ofile<    for(int i = 0 ; i < user.User_delete.size(); ++i)
        ofile<    ofile<    for(int i = 0 ; i < user.User_update.size(); ++i)
        ofile<    ofile<    for(int i = 0 ; i < user.User_drop.size(); ++i)
        ofile<    ofile<    ofile.close();
    ///-----------------------------------------------------------------------------------------------///

    return;

}

void show_Table(string name,Database database)
{

    int i;
    char line[100];
    string path;
    path = database.getAllpath() + name+".txt";
    ifstream fin(path.c_str());
    i=0;
    while(fin.getline(line,100))
    {
        cout<        i++;
    }
    fin.close();
}

void show_View(string name,Database database)///CREATE VIEW V_Customer
///AS
{

    int i;
    char line[100];
    string path;
    path = database.getAllpath() + name+".txt";
    ifstream fin(path.c_str());
    i=0;
    cout<<"CREATE VIEW " + name + " AS ";
    while(fin.getline(line,100))
    {
        cout<        i++;
    }
    fin.close();
}

void show_Index(string name,Database database)///CREATE INDEX V_Customer
///AS
{

    int i;
    char line[100];
    string path;
    path = database.getAllpath() + name+".txt";
    ifstream fin(path.c_str());
    i=0;
    while(fin.getline(line,100))
    {
        cout<        i++;
    }
    fin.close();
}

void show_database(Database database)
{
    int i;
    char line[100];
    string path_table;
    string path_view;
    string path_index;
    path_table = database.getAllpath() + database.getDatabaseName()+".txt";
    path_view = database.getFile_view();
    path_index = database.getFile_index();
    ifstream fin(path_table.c_str());
    i=0;
    cout<<"Table"<    cout<<"-------------"<    while(fin.getline(line,100))
    {
        cout<        i++;
    }
    cout<<"-------------"<    fin.close();
    cout<<"VIEW"<    cout<<"-------------"<    fin.open(path_view.c_str());
    while(fin.getline(line,100))
    {
        cout<        i++;
    }
    cout<<"-------------"<    fin.close();
    cout<<"INDEX"<    cout<<"-------------"<    fin.open(path_index.c_str());
    while(fin.getline(line,100))
    {
        cout<        i++;
    }
    cout<<"-------------"<    fin.close();
}

void Database::DropTable(string table_name,User user,Database database)///DROP 类型 名称
{
    int key = 0;///是否拥有权限
    vector::iterator it;
    for(it=user.User_drop.begin(); it!=user.User_drop.end();)
    {
        if(*it  == table_name)
        {
            key = 1;
            break;
        }

        else
            ++it;    //指向下一个位置
    }

    if(key == 1)
    {
        FILE *fp;
        ofstream file;
        string table_file;
        string database_file;///存放表名的文件
///    string data;///数据
        table_file = database.Allpath + table_name +".txt";
        while((fp = fopen(table_file.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
        {
            ///printf("The file can not be opened.\n");
            cout<<"Table name input error:  ";
            ///exit(1);///结束程序的执行
            cin>>table_name;
            table_file = database.Allpath + table_name +".txt";
        }
        fclose(fp);
        DeleteFile(table_file.c_str());
        cout<<"DROP SUCCESS"<        ///-------------------------------------////
        ///将数据存入data中---------------///////
        database_file = database.Allpath + database.getDatabaseName() +".txt";
        char line[100];
        ifstream fin(database_file.c_str());
        vector data;///数据
        int number = 0;
        while(fin.getline(line,100))
        {

            if(strcmp(table_name.c_str(),line) != 0)
            {
                data.push_back(line);
                ++number;
            }
        }
        fin.close();
        ///----------------------///
        file.open(database_file.c_str(),ios::trunc);///删除表中的数据
        file.clear();
        file.close();
        ///-------------------------------/////
        ///----------将数据重新写入文件中----------------------///
        file.open(database_file.c_str(),ios::app);     ///作为输出文件打开
        for(int i = 0 ; i < number; ++i)
        {
            file<        }
        file.close();
        ///-------------------------------------///
        ///----------------------读出INDEX存入data2中---------------------------------///

        vector >  data2;///存索引数据
        vector rowdata;
        string t_file = "F:\\database\\INDEX.txt";
        ifstream fin1(t_file.c_str());


        while(fin1.getline(line,100))
        {
            char *p;
            const char *d = "_";
            p = strtok(line,d);
            while(p)
            {

                rowdata.push_back(p);
                p=strtok(NULL,d);
            }
            data2.push_back(rowdata);
            rowdata.clear();
        }
        fin1.close();
        ///-------------------------------------------------------///
        ///---------------------判断表是否拥有索引----------------------------------///
        vector >::iterator it;///迭代器
        it = data2.begin();
        int exist = 0;
        for(int i = 0 ; i < data2.size(); ++i)
        {
            if(table_name == data2[i][0])///存在
            {
                ///----------------------删除table_name_index文件---------------------------------///
                vector >  data3;///存索引数据
                string file_index = database.getAllpath() + table_name + "_index"+".txt";

                DeleteFile(file_index.c_str());
                ///----------------------------------------------------------------------------------///
                ///进入存储索引名称的文件中、删除对应的索引------------------------------------------//
//                fin.open(t_file.c_str());
//                fin.clear();
//                fin.close();
                ///在data2中删除对应索引名
                data2.erase(it);
                exist  = 1;
            }
            it++;
        }

        if(exist == 1)
        {
            ///进入存储索引名称的文件中、删除对应的索引------------------------------------------//
            ofstream fin_index(t_file.c_str());
            fin_index.close();
            ///------------将数据重新输入--------///
            file.open(t_file.c_str(),ios::app);     ///作为输出文件打开
            for(int j = 0 ; j < data2.size(); ++j)
            {
                file<
            }
            file<            file.close();
        }

    }
    else
    {
        cout<<"The user does not have the authority"<        return;
    }

}

void Database::DeleteTable(string table_name,User user,Database database)  ///DELETE FROM 表名称 WHERE 列名称 = 值
///DELETE FROM 表名称 ;
{
    int key = 0;///是否拥有权限
    vector::iterator it;
    for(it=user.User_delete.begin(); it!=user.User_delete.end();)
    {
        if(*it  == table_name)
        {
            key = 1;
            break;
        }
        else
            ++it;    //指向下一个位置
    }

    if(key == 1)
    {
        FILE *fp;
        ofstream file;
        string table_file;
        string command;///命令
        string column;
        table_file = database.Allpath + table_name +".txt";
        while((fp = fopen(table_file.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
        {
            ///printf("The file can not be opened.\n");
            cout<<"Table name input error:  ";
            ///exit(1);///结束程序的执行
            cin>>table_name;
            table_file = database.Allpath + table_name +".txt";
        }
        fclose(fp);

        ///--------------------------------将表数据存入data1中---------------///////
        char line[100];

        vector  data;///表格式无数据
        vector  rowdata;///只存数据(行数据)
        vector >  data1;///存数据
        string ss = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
        int number = 0;
        int temporary1 = 0;
        int temporary2 = 0;
        ifstream fin(table_file.c_str());
        while(fin.getline(line,100))///*****************************************************************************问题
        {
            if(temporary1 == 0 || temporary2 == -1)
            {
                data.push_back(line);
                ++number;
            }

            if(temporary1 == 1)
            {
                char *p;
                const char *d = " ";
                p = strtok(line,d);
                while(p)
                {
                    ///printf("%s\n",p);
                    rowdata.push_back(p);
                    p=strtok(NULL,d);
                }
                data1.push_back(rowdata);
                rowdata.clear();
                temporary2++;
            }
            if(strcmp(ss.c_str(),line) == 0)
            {
                temporary1 = 1;
                temporary2 = -1;
            }
        }
        fin.close();
        ///------------------------------------------------------////

        cin>>command;
        transform(command.begin(),command.end(),command.begin(),towupper);
        if(command == ";")
        {
            ///----------------------------------//
            ///----------清除表数据-----///////
            file.open(table_file.c_str(),ios::trunc);///删除表中的数据
            file.close();
            ///---------------------------///
            ///------------将数据重新输入--------///
            file.open(table_file.c_str(),ios::app);     ///作为输出文件打开
            for(int i = 0 ; i < number; ++i)
            {
                file<            }
            file.close();

        }
        else if(command == "WHERE")
        {

            cin>>command;
            int positionOfColumn2;
            int LineOfCommand = 0;///
            int temp = 0;
            ///----------------判断是否存在command---------------//
            for(int j = 0 ; j < data1[0].size(); ++j)
            {
                if(data1[0][j] == command)
                {
                    positionOfColumn2 = j;
                    temp = 1;
                    break;
                }

            }
            if(temp == 0)
                return;
            cin>>command;///输入命令
            string value1;
            string value2;
            transform(command.begin(),command.end(),command.begin(),towupper);
            if(command == "=")
            {
                cin>>value1;
                cin>>command;
                transform(command.begin(),command.end(),command.begin(),towupper);
                if(command == ";")///select * form table where column = values ;
                {
                    for(int i = 0 ; i < data1[0].size(); ++i)
                        cout<                    cout<                    vector >::iterator dataiterator;
                    dataiterator = data1.begin();
                    dataiterator++;
                    for(int i = 1; i < data1.size(); ++i)
                    {
                        for(int j = 0 ; j < data1[i].size(); ++j)
                        {
                            if(j == positionOfColumn2)
                            {
                                if(data1[i][j] == value1)
                                {
                                    data1.erase(dataiterator);
                                }
                            }

                        }
                        dataiterator++;
                    }
                    for(int kk = 1 ; kk < data1.size(); ++kk)
                    {
                        for(int mm = 0; mm < data1[kk].size(); ++mm)
                        {
                            cout<                        }
                        cout<                    }

                }
                else if(command == "AND")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
                {
                    cin>>command;
                    int positionOfColumn3;///command的位置
                    int LineOfCommand = 0;///
                    int temp = 0;
                    ///----------------判断是否存在command---------------//
                    for(int j = 0 ; j < data1[0].size(); ++j)
                    {
                        if(data1[0][j] == command)
                        {
                            positionOfColumn3 = j;
                            temp = 1;
                            break;
                        }

                    }
                    if(temp == 0)
                        return;
                    cin>>command;
                    if(command == "=")
                    {
                        cin>>value2;
                        for(int i = 0 ; i < data1[0].size(); ++i)
                            cout<                        cout<                        vector >::iterator dataiterator;
                        dataiterator = data1.begin();
                        dataiterator++;
                        for(int i = 1; i < data1.size(); ++i)
                        {
                            for(int j = 0 ; j < data1[i].size(); ++j)
                            {
                                if(j == positionOfColumn2)
                                {
                                    if(data1[i][j] == value1)
                                    {
                                        ///cout<                                        for(int j1 = 0 ; j1 < data1[i].size(); ++j1)
                                        {
                                            if(j1 == positionOfColumn3)
                                            {
                                                if(data1[i][j1] == value2)
                                                {
                                                    data1.erase(dataiterator);
                                                }
                                            }
                                        }

                                    }
                                }

                            }

                            dataiterator++;
                        }
                    }

                }


                else if(command == "OR")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
                {
                    cin>>command;
                    int positionOfColumn3;///command的位置
                    int LineOfCommand = 0;///
                    int temp = 0;
                    ///----------------判断是否存在command---------------//
                    for(int j = 0 ; j < data1[0].size(); ++j)
                    {
                        if(data1[0][j] == command)
                        {
                            positionOfColumn3 = j;
                            temp = 1;
                            break;
                        }

                    }
                    if(temp == 0)
                        return;
                    cin>>command;
                    if(command == "=")
                    {
                        cin>>value2;
                        for(int i = 0 ; i < data1[0].size(); ++i)
                            cout<                        cout<                        vector >::iterator dataiterator;
                        dataiterator = data1.begin();
                        dataiterator++;
                        for(int i = 1; i < data1.size(); ++i)
                        {
                            for(int j = 0 ; j < data1[i].size(); ++j)
                            {
                                if(positionOfColumn2 == positionOfColumn3)
                                {
                                    if(j == positionOfColumn2)
                                    {
                                        if(data1[i][j] == value1 || data1[i][j] == value2)
                                        {
                                            data1.erase(dataiterator);

                                        }
                                    }
                                }
                                else
                                {
                                    if(j == positionOfColumn2 || j == positionOfColumn3)
                                    {
                                        if(j == positionOfColumn2)
                                        {
                                            if(data1[i][j] == value1)
                                            {
                                                data1.erase(dataiterator);

                                            }
                                        }
                                        else if(j == positionOfColumn3)
                                        {
                                            if(data1[i][j] == value2)
                                            {
                                                data1.erase(dataiterator);
                                            }
                                        }

                                    }
                                }


                            }
                            dataiterator++;
                        }
                    }

                }

            }
            else if(command == ">")
            {
                cin>>command;
                for(int i = 0 ; i < data1[0].size(); ++i)
                    cout<                cout<                vector >::iterator dataiterator;
                dataiterator = data1.begin();
                dataiterator++;
                for(int i = 1; i < data1.size(); ++i)
                {
                    for(int j = 0 ; j < data1[i].size(); ++j)
                    {
                        if(j == positionOfColumn2)
                        {
                            int integer=atoi(data1[i][j].c_str());///string转为int
                            int integer1 = atoi(command.c_str());
                            if(integer > integer1)
                            {
                                data1.erase(dataiterator);
                            }
                        }
                    }
                    dataiterator++;
                }
            }
            else if(command == "<")
            {
                cin>>command;
                for(int i = 0 ; i < data1[0].size(); ++i)
                    cout<                cout<                vector >::iterator dataiterator;
                dataiterator = data1.begin();
                dataiterator++;
                for(int i = 1; i < data1.size(); ++i)
                {
                    for(int j = 0 ; j < data1[i].size(); ++j)
                    {
                        if(j == positionOfColumn2)
                        {
                            int integer=atoi(data1[i][j].c_str());///string转为int
                            int integer1 = atoi(command.c_str());
                            if(integer < integer1)
                            {
                                data1.erase(dataiterator);
                            }
                        }
                    }
                    dataiterator++;
                }
            }
            else if(command == "<>")
            {
                cin>>command;
                for(int i = 0 ; i < data1[0].size(); ++i)
                    cout<                cout<                vector >::iterator dataiterator;
                dataiterator = data1.begin();
                dataiterator++;
                for(int i = 1; i < data1.size(); ++i)
                {
                    for(int j = 0 ; j < data1[i].size(); ++j)
                    {
                        if(j == positionOfColumn2)
                        {
                            int integer=atoi(data1[i][j].c_str());///string转为int
                            int integer1 = atoi(command.c_str());
                            if(integer != integer1)
                            {
                                data1.erase(dataiterator);
                            }
                        }
                    }
                    dataiterator++;
                }
            }
            else if(command == ">=")
            {
                cin>>command;
                for(int i = 0 ; i < data1[0].size(); ++i)
                    cout<                cout<                vector >::iterator dataiterator;
                dataiterator = data1.begin();
                dataiterator++;
                for(int i = 1; i < data1.size(); ++i)
                {
                    for(int j = 0 ; j < data1[i].size(); ++j)
                    {
                        if(j == positionOfColumn2)
                        {
                            int integer=atoi(data1[i][j].c_str());///string转为int
                            int integer1 = atoi(command.c_str());
                            if(integer >= integer1)
                            {
                                data1.erase(dataiterator);
                            }
                        }
                    }
                    dataiterator++;
                }
            }
            else if(command == "<=")
            {
                cin>>command;
                for(int i = 0 ; i < data1[0].size(); ++i)
                    cout<                cout<                vector >::iterator dataiterator;
                dataiterator = data1.begin();
                dataiterator++;
                for(int i = 1; i < data1.size(); ++i)
                {
                    for(int j = 0 ; j < data1[i].size(); ++j)
                    {
                        if(j == positionOfColumn2)
                        {
                            int integer=atoi(data1[i][j].c_str());///string转为int
                            int integer1 = atoi(command.c_str());
                            if(integer <= integer1)
                            {
                                data1.erase(dataiterator);

                            }
                        }
                    }
                    dataiterator++;
                }
            }
            else if(command == "BETWEEN")
            {
                int number1;
                int number2;
                cin>>number1;
                cin>>command;
                transform(command.begin(),command.end(),command.begin(),towupper);
                if(command == "AND")
                {
                    cin>>number2;
                    int maxn = number1;
                    if(maxn > number2)
                    {
                        maxn = number2;
                        number2 = number1;
                        number1 = maxn;
                    }
                    for(int i = 0 ; i < data1[0].size(); ++i)
                        cout<                    cout<                    vector >::iterator dataiterator;
                    dataiterator = data1.begin();
                    dataiterator++;
                    for(int i = 1; i < data1.size(); ++i)
                    {
                        for(int j = 0 ; j < data1[i].size(); ++j)
                        {
                            if(j == positionOfColumn2)
                            {
                                int integer=atoi(data1[i][j].c_str());///string转为int
                                if(integer > number1 && integer < number2)
                                {
                                    data1.erase(dataiterator);

                                }
                            }

                        }
                        dataiterator++;
                    }
                }

            }
            else if(command == "IN")
            {
                cin>>command;
                if(command == "(")
                {
                    vector values;
                    cin>>command;
                    while(command != ")")
                    {
                        int commandint = atoi(command.c_str());
                        values.push_back(commandint);
                        cin>>command;
                    }
                    for(int i = 0 ; i < data1[0].size(); ++i)
                        cout<                    cout<                    vector >::iterator dataiterator;
                    dataiterator = data1.begin();
                    dataiterator++;
                    for(int i = 1; i < data1.size(); ++i)
                    {
                        for(int j = 0 ; j < data1[i].size(); ++j)
                        {
                            if(j == positionOfColumn2)
                            {
                                int integer=atoi(data1[i][j].c_str());///string转为int
                                vector::iterator val;
                                for(val = values.begin(); val <= values.end(); ++val)
                                {
                                    if(integer == *val)
                                    {
                                        data1.erase(dataiterator);

                                    }
                                }

                            }

                        }
                        dataiterator++;
                    }

                }

            }
            file.open(table_file.c_str(),ios::trunc);///删除表中的数据
            file.close();
            ///---------------------------///
            ///------------将数据重新输入--------///
            file.open(table_file.c_str(),ios::app);     ///作为输出文件打开
            for(int i = 0 ; i < number; ++i)
            {
                file<            }
            for(int i = 1 ; i < data1.size(); ++i)
            {
                for(int j =0; j < data1[i].size(); ++j)
                {
                    file<                }
                file<            }
            file.close();

        }
        vector >  data11;///存数据
        temporary1 = 0;
        fin.open(table_file.c_str());
        while(fin.getline(line,100))///*****************************************************************************问题
        {

            if(temporary1 == 1)
            {
                char *p;
                const char *d = " ";
                p = strtok(line,d);
                while(p)
                {
                    ///printf("%s\n",p);
                    rowdata.push_back(p);
                    p=strtok(NULL,d);
                }
                data11.push_back(rowdata);
                rowdata.clear();

            }
            if(strcmp(ss.c_str(),line) == 0)
            {
                temporary1 = 1;

            }
        }
        fin.close();


        ///----------------------读出INDEX存入data2中---------------------------------///

        vector >  data2;///存索引数据
        //vector rowdata;
        string t_file = "F:\\database\\INDEX.txt";
        ifstream fin1(t_file.c_str());


        while(fin1.getline(line,100))
        {
            char *p;
            const char *d = "_";
            p = strtok(line,d);
            while(p)
            {
                ///printf("%s\n",p);
                rowdata.push_back(p);
                p=strtok(NULL,d);
            }
            data2.push_back(rowdata);
            rowdata.clear();
        }
        fin1.close();
        ///-------------------------------------------------------///
        ///---------------------判断表是否拥有索引----------------------------------///
        int exist = 0;
        for(int i = 0 ; i < data2.size(); ++i)
        {
            if(table_name == data2[i][0])///存在
            {
                exist  = 1;
            }
        }

        if(exist == 1)
        {
            ///**************读取索引文件内容*********************----//
            string file_index = database.getAllpath() + table_name + "_index.txt";
            fin.open(file_index.c_str());
            string column;
            while(fin.getline(line,100))///*****************************************************************************问题
            {
                char *p;
                const char *d = " ";
                p = strtok(line,d);
                while(p)
                {
                    column = p;
                    break;
                }
                break;
            }
            fin.close();
            ///***********************读取结束----------------------------------------------------------------------///
            ///进入索引的文件中、更新索引------------------------------------------//
            int positionOfColumn;///column的位置
            int temp = 0;
            ///----------------判断是否存在command---------------//
            for(int j = 0 ; j < data11[0].size(); ++j)
            {
                if(data11[0][j] == column)///**************问题
                {
                    positionOfColumn = j;
                    temp = 1;
                }

            }
            if(temp == 0)
                return;
            ofstream fin_index(file_index.c_str());///删除文件中的内容
            fin_index<            for(int i = 1 ; i < data11.size(); ++i)
            {
                fin_index<                fin_index<            }
            fin_index.close();
        }
    }
    else
    {
        cout<<"The user does not have the authority"<        return;
    }

}

void Database::Insert(string table_name,User user,Database database)///INSERT INTO 表名称 VALUES (值1, 值2,....)
{
    int key = 0;///是否拥有权限
    vector::iterator it;
    for(it=user.User_insert.begin(); it!=user.User_insert.end();)
    {
        if(*it  == table_name)
        {
            key = 1;
            break;
        }

        else
            ++it;    //指向下一个位置
    }
    if(key == 1)
    {
        FILE *fp;
        string table_file;
        string data;///数据
        table_file = database.Allpath + table_name +".txt";
        ofstream ofile;///向文件中写入数据
        while((fp = fopen(table_file.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
        {
            ///printf("The file can not be opened.\n");
            cout<<"Table name input error:  ";
            ///exit(1);///结束程序的执行
            cin>>table_name;
            if(table_name == "ENDINPUT")
                return;
            table_file = database.Allpath + table_name +".txt";
        }

        fclose(fp);
///--------------------求属性个数------------------------------//

        char line[100];
        int number = 0;///属性个数
        int numberofline = 0;
        ifstream fin(table_file.c_str());
        string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
        while(fin.getline(line,100))
        {
            numberofline++;
            if(numberofline > 4)
            {
                number++;
            }

            if(strcmp(s.c_str(),line) == 0)
            {
                break;
            }

        }
        number = number - 1;
        //cout<        fin.close();
///---------------------------------------------------///
        string datas[number];///输入的内容
        cin>>data;
        int i = 0;
        transform(data.begin(),data.end(),data.begin(),towupper);
        if(data == "VALUES")
        {
            ofile.open(table_file.c_str(),ios::app);
            //cout<            while(i <= number - 1)
            {
                cin>>datas[i];
                ofile<                ++i;
            }
            ofile<            cout<<"Add Success"<
        }
        fclose(fp);




        ///--------------------------------将数据存入data1中---------------///////

        vector  rowdata;///只存数据(行数据)
        vector >  data1;///存数据
        string ss = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
        int temporary1 = 0;
        fin.open(table_file.c_str());
        while(fin.getline(line,100))///*****************************************************************************问题
        {
            if(temporary1 == 1)
            {
                char *p;
                const char *d = " ";
                p = strtok(line,d);
                while(p)
                {
                    ///printf("%s\n",p);
                    rowdata.push_back(p);
                    p=strtok(NULL,d);
                }
                data1.push_back(rowdata);
                rowdata.clear();
            }
            if(strcmp(ss.c_str(),line) == 0)
            {
                temporary1 = 1;
            }
        }
        fin.close();
        ///------------------------------------------------------////




        ///----------------------读出INDEX存入data2中---------------------------------///

        vector >  data2;///存索引数据
        //vector rowdata;
        string t_file = "F:\\database\\INDEX.txt";
        ifstream fin1(t_file.c_str());


        while(fin1.getline(line,100))
        {
            char *p;
            const char *d = "_";
            p = strtok(line,d);
            while(p)
            {
                ///printf("%s\n",p);
                rowdata.push_back(p);
                p=strtok(NULL,d);
            }
            data2.push_back(rowdata);
            rowdata.clear();
        }
        fin1.close();
        ///-------------------------------------------------------///
        ///---------------------判断表是否拥有索引----------------------------------///
        int exist = 0;
        for(int i = 0 ; i < data2.size(); ++i)
        {
            if(table_name == data2[i][0])///存在
            {
                exist  = 1;
            }
        }

        if(exist == 1)
        {
            ///**************读取索引文件内容*********************----//
            string file_index = database.getAllpath() + table_name + "_index.txt";
            fin.open(file_index.c_str());
            string column;
            while(fin.getline(line,100))///*****************************************************************************问题
            {
                char *p;
                const char *d = " ";
                p = strtok(line,d);
                while(p)
                {
                    column = p;
                    break;
                }
                break;
            }
            fin.close();
            ///***********************读取结束----------------------------------------------------------------------///


            ///进入索引的文件中、更新索引------------------------------------------//
            int positionOfColumn;///column的位置
            int temp = 0;
            ///----------------判断是否存在command---------------//
            for(int j = 0 ; j < data1[0].size(); ++j)
            {
                if(data1[0][j] == column)
                {
                    positionOfColumn = j;
                    temp = 1;
                }

            }
            if(temp == 0)
                return;
            ofstream fin_index(file_index.c_str());///删除文件中的内容
            fin_index.close();
            ///------------将数据重新输入--------///
            fin_index.open(file_index.c_str(),ios::app);     ///作为输出文件打开
            fin_index<            for(int i = 1 ; i < data1.size(); ++i)
            {
                fin_index<                fin_index<            }
            fin_index.close();
        }
    }
    else
    {
        cout<<"The user does not have the authority"<        return;
    }

}

void Database::Update(string name,User user,Database database)///UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
///UPDATE 表名称 SET 列名称 = 新值 ;
{
    int key = 0;///是否拥有权限
    vector::iterator it;
    for(it=user.User_update.begin(); it!=user.User_update.end();)
    {
        if(*it  == name)
        {
            key = 1;
            break;
        }

        else
            ++it;    //指向下一个位置
    }

    if(key == 1)
    {
        int number;
        string command;
        string column1;
        string value1;///操作数1
        string column2;
        string value2;///操作数2
        char line[100];
        FILE *fp;
        string path;
        path = database.getAllpath() + name+".txt";
        ifstream fin(path.c_str());
        number=0;
        ofstream file;///向文件中写入数据
        while((fp = fopen(path.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
        {
            ///printf("The file can not be opened.\n");
            cout<<"Table name input error:  ";
            ///exit(1);///结束程序的执行
            cin>>name;
            if(name == "ENDINPUT")
                return;
            path = database.Allpath + name +".txt";
        }
        fclose(fp);
        cin>>command;
        transform(command.begin(),command.end(),command.begin(),towupper);
        if(command == "SET")
        {
            cin>>column1;
            int number2 = -1;///记录行数;
            int number1 = -1;///记录column1的所在位置;
            int number = 0;///记录是否存在column1;
            string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
            ///----------------判断是否存在column1---------------//

            while(fin.getline(line,100))
            {
                number2++;
                if(number == 1)
                {
                    char *p;
                    const char *d = " ";
                    p = strtok(line,d);
                    vector str;
                    while(p)
                    {
                        ///printf("%s\n",p);
                        str.push_back(p);
                        p=strtok(NULL,d);
                    }
                    for(int i = 0 ; i < str.size(); ++i)
                    {
                        if(str[i] == column1)
                        {
                            number1 = i;
                        }

                    }
                    if(number1 == -1)
                    {
                        return;
                    }
                    break;
                }
                if(strcmp(s.c_str(),line) == 0)
                {
                    number = 1;
                }


            }
            fin.close();
            ///------------------------------------------------------------------------//////
            ///--------------------------------将数据保存在data里--------------------///////////
            vector > data;///数据
            vector rowdata;
            int temp = 0;
            int hang = 0;///data行
            ifstream fin(path.c_str());
            ///cout<<"DDDDDDD"<            while(fin.getline(line,100))///从第零行第零列开始
            {
                if(temp == 0)
                {
                    rowdata.push_back(line);
                    data.push_back(rowdata);
                    rowdata.clear();
                }
                else
                {
                    char *p;
                    const char *d = " ";
                    p = strtok(line,d);
                    while(p)
                    {
                        ///printf("%s\n",p);
                        rowdata.push_back(p);
                        p=strtok(NULL,d);
                    }
                    data.push_back(rowdata);
                    rowdata.clear();

                }
                if(strcmp(s.c_str(),line) == 0)
                {
                    temp = 1;
                }

            }
            fin.close();

//            for(int i = 0 ; i < data.size(); ++i)
//            {
//                for(int j = 0 ; j < data[i].size(); ++j)
//                    cout<//                cout<//            }
            ///-----------------------------------------------------------------------------//
            cin>>command;
            if(command == "=")
            {
                cin>>value1;///输入操作数
                cin>>command;
                transform(command.begin(),command.end(),command.begin(),towupper);
                if(command == ";")
                {
                    for(int i = 0 ; i < data.size(); ++i)
                    {
                        if(i > number2)
                        {
                            for(int j = 0 ; j < data[i].size(); ++j)
                            {
                                if(j == number1)
                                {
                                    data[i][j] = value1;
                                }

                            }
                        }

                    }
                }
                else if(command == "WHERE")
                {
                    cin>>column2;
                    int positionOfColumn2;
                    temp = 0;
                    ///----------------判断是否存在column2---------------//
                    for(int i = 0 ; i < data.size(); ++i)
                    {
                        if(i == number2)
                        {
                            for(int j = 0 ; j < data[i].size(); ++j)
                            {
                                if(data[i][j] == column2)
                                {
                                    positionOfColumn2 = j;
                                    temp = 1;
                                }

                            }
                        }

                    }
                    if(temp == 0)
                        return;
                    //cout<                    cin>>command;///输入命令
                    transform(command.begin(),command.end(),command.begin(),towupper);
                    if(command == "=")
                    {
                        cin>>value2;
                        cin>>command;
                        transform(command.begin(),command.end(),command.begin(),towupper);
                        if(command == ";")
                        {
                            for(int i = 0 ; i < data.size(); ++i)
                            {
                                if(i > number2)
                                {
                                    for(int j = 0 ; j < data[i].size(); ++j)
                                    {
                                        if(j == positionOfColumn2)
                                        {
                                            if(data[i][j] == value2)
                                            {
                                                data[i][number1] = value1;
                                            }
                                        }

                                    }
                                }

                            }
                        }

                        else if(command == "AND")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
                        {
                            cin>>command;
                            int positionOfColumn3;///command的位置
                            int LineOfCommand = 0;///
                            int temp = 0;
                            ///----------------判断是否存在command---------------//
                            for(int j = 0 ; j < data[0].size(); ++j)
                            {
                                if(data[0][j] == command)
                                {
                                    positionOfColumn3 = j;
                                    temp = 1;
                                    break;
                                }

                            }
                            if(temp == 0)
                                return;
                            cin>>command;
                            if(command == "=")
                            {
                                cin>>value2;
                                for(int i = 1; i < data.size(); ++i)
                                {
                                    if(i > number2)
                                    {
                                        for(int j = 0 ; j < data[i].size(); ++j)
                                        {
                                            if(j == positionOfColumn2)
                                            {
                                                if(data[i][j] == value1)
                                                {
                                                    ///cout<                                                    for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                                    {
                                                        if(j1 == positionOfColumn3)
                                                        {
                                                            if(data[i][j1] == value2)
                                                            {
                                                                data[i][number1] = value1;
                                                            }
                                                        }
                                                    }

                                                }
                                            }

                                        }
                                    }

                                }
                            }

                        }


                        else if(command == "OR")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
                        {
                            cin>>command;
                            int positionOfColumn3;///command的位置

                            int temp = 0;
                            ///----------------判断是否存在command---------------//
                            for(int j = 0 ; j < data[0].size(); ++j)
                            {
                                if(data[0][j] == command)
                                {
                                    positionOfColumn3 = j;
                                    temp = 1;
                                    break;
                                }

                            }
                            if(temp == 0)
                                return;
                            cin>>command;
                            if(command == "=")
                            {
                                cin>>value2;
//                                for(int i = 0 ; i < data[0].size(); ++i)
//                                    cout<//                                cout<                                for(int i = 1; i < data.size(); ++i)
                                {
                                    if(i > number2)
                                    {
                                        for(int j = 0 ; j < data[i].size(); ++j)
                                        {
                                            if(positionOfColumn2 == positionOfColumn3)
                                            {
                                                if(j == positionOfColumn2)
                                                {
                                                    if(data[i][j] == value1 || data[i][j] == value2)
                                                    {
                                                        data[i][number1] = value1;

                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if(j == positionOfColumn2 || j == positionOfColumn3)
                                                {
                                                    if(j == positionOfColumn2)
                                                    {
                                                        if(data[i][j] == value1)
                                                        {
                                                            data[i][number1] = value1;

                                                        }
                                                    }
                                                    else if(j == positionOfColumn3)
                                                    {
                                                        if(data[i][j] == value2)
                                                        {
                                                            data[i][number1] = value1;
                                                        }
                                                    }

                                                }
                                            }


                                        }
                                    }

                                }
                            }

                        }
                    }
                    else if(command == ">")
                    {
                        cin>>value2;
                        for(int i = 0 ; i < data.size(); ++i)
                        {
                            if(i > number2)
                            {
                                for(int j = 0 ; j < data[i].size(); ++j)
                                {
                                    if(j == positionOfColumn2)
                                    {
                                        if(data[i][j] > value2)
                                        {
                                            data[i][number1] = value1;
                                        }
                                    }

                                }
                            }

                        }
                    }
                    else if(command == "<")
                    {
                        cin>>value2;
                        for(int i = 0 ; i < data.size(); ++i)
                        {
                            if(i > number2)
                            {
                                for(int j = 0 ; j < data[i].size(); ++j)
                                {
                                    if(j == positionOfColumn2)
                                    {
                                        if(data[i][j] < value2)
                                        {
                                            data[i][number1] = value1;
                                        }
                                    }

                                }
                            }

                        }
                    }
                    else if(command == "<>")
                    {
                        cin>>value2;
                        for(int i = 0 ; i < data.size(); ++i)
                        {
                            if(i > number2)
                            {
                                for(int j = 0 ; j < data[i].size(); ++j)
                                {
                                    if(j == positionOfColumn2)
                                    {
                                        if(data[i][j] != value2)
                                        {
                                            data[i][number1] = value1;
                                        }
                                    }

                                }
                            }

                        }
                    }
                    else if(command == ">=")
                    {
                        cin>>value2;
                        for(int i = 0 ; i < data.size(); ++i)
                        {
                            if(i > number2)
                            {
                                for(int j = 0 ; j < data[i].size(); ++j)
                                {
                                    if(j == positionOfColumn2)
                                    {
                                        if(data[i][j] >= value2)
                                        {
                                            data[i][number1] = value1;
                                        }
                                    }

                                }
                            }

                        }
                    }
                    else if(command == "<=")
                    {
                        cin>>value2;
                        for(int i = 0 ; i < data.size(); ++i)
                        {
                            if(i > number2)
                            {
                                for(int j = 0 ; j < data[i].size(); ++j)
                                {
                                    if(j == positionOfColumn2)
                                    {
                                        if(data[i][j] <= value2)
                                        {
                                            data[i][number1] = value1;
                                        }
                                    }

                                }
                            }

                        }
                    }
                    else if(command == "BETWEEN")
                    {
                        int number3;
                        int number4;
                        cin>>number3;
                        cin>>command;
                        transform(command.begin(),command.end(),command.begin(),towupper);
                        if(command == "AND")
                        {
                            cin>>number4;
                            int maxn = number3;
                            if(maxn > number4)
                            {
                                maxn = number4;
                                number4 = number3;
                                number3 = maxn;
                            }
                            for(int i = 0; i < data.size(); ++i)
                            {
                                if(i > number2)
                                {
                                    for(int j = 0 ; j < data[i].size(); ++j)
                                    {
                                        if(j == positionOfColumn2)
                                        {
                                            int integer=atoi(data[i][j].c_str());///string转为int
                                            if(integer > number3 && integer < number4)
                                            {
                                                data[i][number1] = value1;

                                            }
                                        }

                                    }
                                }

                            }

                        }

                    }
                    else if(command == "IN")
                    {
                        cin>>command;
                        if(command == "(")
                        {
                            vector values;
                            cin>>command;
                            while(command != ")")
                            {
                                int commandint = atoi(command.c_str());
                                values.push_back(commandint);
                                cin>>command;
                            }
                            for(int i = 1; i < data.size(); ++i)
                            {
                                if(i >number2)
                                {
                                    for(int j = 0 ; j < data[i].size(); ++j)
                                    {
                                        if(j == positionOfColumn2)
                                        {
                                            int integer=atoi(data[i][j].c_str());///string转为int
                                            vector::iterator val;
                                            for(val = values.begin(); val <= values.end(); ++val)
                                            {
                                                if(integer == *val)
                                                {
                                                    data[i][number1] = value1;
                                                }
                                            }
                                        }
                                    }
                                }

                            }

                        }

                    }

                }

            }

            ///----------清除表数据-----///////
            file.open(path.c_str(),ios::trunc);///删除表中的数据
            file.close();
            ///------------将数据重新输入--------///
            file.open(path.c_str(),ios::app);     ///作为输出文件打开
            for(int i = 0 ; i < data.size(); ++i)
            {
                for(int j = 0 ; j < data[i].size(); ++j)
                    file<                file<            }
            file.close();

        }

        ///--------------------------------将数据存入data1中---------------///////

        vector  rowdata;///只存数据(行数据)
        vector >  data1;///存数据
        string ss = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
        string table_file = database.Allpath + name +".txt";
        int temporary1 = 0;
        fin.open(table_file.c_str());
        while(fin.getline(line,100))///*****************************************************************************问题
        {
            if(temporary1 == 1)
            {
                char *p;
                const char *d = " ";
                p = strtok(line,d);
                while(p)
                {
                    ///printf("%s\n",p);
                    rowdata.push_back(p);
                    p=strtok(NULL,d);
                }
                data1.push_back(rowdata);
                rowdata.clear();
            }
            if(strcmp(ss.c_str(),line) == 0)
            {
                temporary1 = 1;
            }
        }
        fin.close();
        ///------------------------------------------------------////




        ///----------------------读出INDEX存入data2中---------------------------------///

        vector >  data2;///存索引数据
        //vector rowdata;
        string t_file = "F:\\database\\INDEX.txt";
        ifstream fin1(t_file.c_str());
//        cout<<"GETFILE_VIEW: "<
        while(fin1.getline(line,100))
        {
            char *p;
            const char *d = "_";
            p = strtok(line,d);
            while(p)
            {
                ///printf("%s\n",p);
                rowdata.push_back(p);
                p=strtok(NULL,d);
            }
            data2.push_back(rowdata);
            rowdata.clear();
        }
        fin1.close();
        ///-------------------------------------------------------///
        ///---------------------判断表是否拥有索引----------------------------------///
        int exist = 0;
        for(int i = 0 ; i < data2.size(); ++i)
        {
            if(name == data2[i][0])///存在
            {
                exist  = 1;
            }
        }
//        cout<<"EXIT: "<        if(exist == 1)
        {
            ///**************读取索引文件内容*********************----//
            string file_index = database.getAllpath() + name + "_index.txt";
            fin.open(file_index.c_str());
            string column;
            while(fin.getline(line,100))///*****************************************************************************问题
            {
                char *p;
                const char *d = " ";
                p = strtok(line,d);
                while(p)
                {
                    column = p;
                    break;
                }
                break;
            }
            fin.close();
            ///***********************读取结束----------------------------------------------------------------------///


            ///进入索引的文件中、更新索引------------------------------------------//
            int positionOfColumn;///column的位置
            int temp = 0;
            ///----------------判断是否存在command---------------//
            for(int j = 0 ; j < data1[0].size(); ++j)
            {
                if(data1[0][j] == column)
                {
                    positionOfColumn = j;
                    temp = 1;
                }

            }
//            cout<<"TEMP: "<            if(temp == 0)
                return;
            ofstream fin_index(file_index.c_str());///删除文件中的内容
            fin_index.close();
            ///------------将数据重新输入--------///
            fin_index.open(file_index.c_str(),ios::app);     ///作为输出文件打开
            fin_index<            for(int i = 1 ; i < data1.size(); ++i)
            {
                fin_index<                fin_index<            }
            fin_index.close();
        }

        cout<<"UPDATE SUCCESS!"<
    }
    else
    {
        cout<<"The user does not have the authority"<        return;
    }

}

void OrderBy(string command, vector >&data)
{
    int commandposition = -1;
    for(int j = 0 ; j < data[0].size(); ++j)
    {
        if(command == data[0][j])
        {
            commandposition = j;
        }
    }
    string temp;
    if(commandposition == -1)
    {
        cout<<"Input error"<    }
    else
    {
        for(int i = 1 ; i < data.size(); ++i)
        {
            for(int j = i ; j < data.size(); ++j)
            {
                if(data[i][commandposition] > data[j][commandposition])
                {
                    temp = data[i][commandposition];
                    data[i][commandposition] = data[j][commandposition];
                    data[j][commandposition] = temp;
                }
            }
        }
    }
    cin>>command;
    transform(command.begin(),command.end(),command.begin(),towupper);
    if(command == ";")
    {
        for(int i = 0 ; i < data.size(); ++i)
        {
            for(int j = 0 ; j < data[i].size(); ++j)
            {
                cout<            }
            cout<        }

    }
    else if(command == "DECS")
    {
        for(int j = 0 ; j < data[0].size(); ++j)
        {
            cout<        }
        cout<        for(int i = data.size() - 1 ; i > 0; --i)
        {
            for(int j = 0 ; j < data[i].size(); ++j)
            {
                cout<            }
            cout<        }
    }
}

void Database::Select(string name,User user,Database database)///选择语句 select * from TABLE WHERE 属性 = values
///select * from table ;///*********************************************************************************************************///
{
    int key = 0;///是否拥有权限
    vector::iterator it;
    for(it=user.User_select.begin(); it!=user.User_select.end();)
    {
        if(*it  == name)
        {
            key = 1;
            break;
        }

        else
            ++it;    //指向下一个位置
    }

    if(key == 1)
    {
        int number;
        char line[100];
        FILE *fp;
        string path;
        string command;///命令
        path = database.getAllpath() + name+".txt";
        number=0;
        ofstream ofile;///向文件中写入数据
        while((fp = fopen(path.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
        {
            ///printf("The file can not be opened.\n");
            cout<<"Table name input error:  ";
            ///exit(1);///结束程序的执行
            cin>>name;
            if(name == "ENDINPUT")
                return;
            path = database.getAllpath() + name +".txt";
        }
        fclose(fp);
        ///--------------------------------将数据保存在data里--------------------///////////
        vector > data;///数据
        vector rowdata;
        int hang = 0;///data行
        //cout<<"DDDDDDD"<        ifstream fin(path.c_str());
        string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
        while(fin.getline(line,100))///从s开始读出存入data中。
        {
            if(number == 1)
            {
                char *p;
                const char *d = " ";
                p = strtok(line,d);
                while(p)
                {
                    //cout<                    rowdata.push_back(p);
                    p=strtok(NULL,d);
                }
                //cout<                data.push_back(rowdata);
                rowdata.clear();
            }
            if(strcmp(s.c_str(),line) == 0)
            {
                number = 1;
            }

        }
        fin.close();
///--------------------------------------------------------------------------------///
        cin>>command;
        transform(command.begin(),command.end(),command.begin(),towupper);
        if(command == ";")
        {
            for(int i = 0 ; i < data.size(); ++i)
            {
                for(int j = 0 ; j < data[i].size(); ++j)
                {
                    cout<                }
                cout<            }
        }
        else if(command == "ORDER")
        {
            cin>>command;
            int commandposition = -1;
            transform(command.begin(),command.end(),command.begin(),towupper);
            if(command == "BY")
            {
                cin>>command;///属性
                OrderBy(command, data);
            }
        }
        else if(command == "WHERE")
        {
            cin>>command;
            int positionOfColumn2;
            int LineOfCommand = 0;///
            int temp = 0;
            ///----------------判断是否存在command---------------//
            for(int j = 0 ; j < data[0].size(); ++j)
            {
                if(data[0][j] == command)
                {
                    positionOfColumn2 = j;
                    temp = 1;
                    break;
                }

            }
            if(temp == 0)
                return;
            cin>>command;///输入命令
            string value1;
            string value2;
            transform(command.begin(),command.end(),command.begin(),towupper);
            if(command == "=")
            {
                cin>>value1;
                cin>>command;
                transform(command.begin(),command.end(),command.begin(),towupper);
                if(command == ";")///select * form table where column = values ;
                {
                    for(int i = 0 ; i < data[0].size(); ++i)
                        cout<                    cout<                    for(int i = 1; i < data.size(); ++i)
                    {
                        for(int j = 0 ; j < data[i].size(); ++j)
                        {
                            if(j == positionOfColumn2)
                            {
                                if(data[i][j] == value1)
                                {
                                    for(int k = 0; k < data[i].size(); ++k)
                                    {
                                        cout<                                    }
                                    cout<
                                }
                            }

                        }
                    }
                }
                else if(command == "AND")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
                {
                    cin>>command;
                    int positionOfColumn3;///command的位置
                    int LineOfCommand = 0;///
                    int temp = 0;
                    ///----------------判断是否存在command---------------//
                    for(int j = 0 ; j < data[0].size(); ++j)
                    {
                        if(data[0][j] == command)
                        {
                            positionOfColumn3 = j;
                            temp = 1;
                            break;
                        }

                    }
                    if(temp == 0)
                        return;
                    cin>>command;
                    if(command == "=")
                    {
                        cin>>value2;
                        for(int i = 0 ; i < data[0].size(); ++i)
                            cout<                        cout<                        for(int i = 1; i < data.size(); ++i)
                        {
                            for(int j = 0 ; j < data[i].size(); ++j)
                            {
                                if(j == positionOfColumn2)
                                {
                                    if(data[i][j] == value1)
                                    {
                                        ///cout<                                        for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                        {
                                            if(j1 == positionOfColumn3)
                                            {
                                                if(data[i][j1] == value2)
                                                {
                                                    for(int k = 0; k < data[i].size(); ++k)
                                                    {
                                                        cout<                                                    }
                                                    cout<                                                }
                                            }
                                        }

                                    }
                                }

                            }
                        }
                    }

                }


                else if(command == "OR")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
                {
                    cin>>command;
                    int positionOfColumn3;///command的位置
                    int LineOfCommand = 0;///
                    int temp = 0;
                    ///----------------判断是否存在command---------------//
                    for(int j = 0 ; j < data[0].size(); ++j)
                    {
                        if(data[0][j] == command)
                        {
                            positionOfColumn3 = j;
                            temp = 1;
                            break;
                        }

                    }
                    if(temp == 0)
                        return;
                    cin>>command;
                    if(command == "=")
                    {
                        cin>>value2;
                        for(int i = 0 ; i < data[0].size(); ++i)
                            cout<                        cout<                        for(int i = 1; i < data.size(); ++i)
                        {
                            for(int j = 0 ; j < data[i].size(); ++j)
                            {
                                if(positionOfColumn2 == positionOfColumn3)
                                {
                                    if(j == positionOfColumn2)
                                    {
                                        if(data[i][j] == value1 || data[i][j] == value2)
                                        {
                                            for(int k = 0; k < data[i].size(); ++k)
                                            {
                                                cout<                                            }
                                            cout<
                                        }
                                    }
                                }
                                else
                                {
                                    if(j == positionOfColumn2 || j == positionOfColumn3)
                                    {
                                        if(j == positionOfColumn2)
                                        {
                                            if(data[i][j] == value1)
                                            {
                                                for(int k = 0; k < data[i].size(); ++k)
                                                {
                                                    cout<                                                }
                                                cout<
                                            }
                                        }
                                        else if(j == positionOfColumn3)
                                        {
                                            if(data[i][j] == value2)
                                            {
                                                for(int k = 0; k < data[i].size(); ++k)
                                                {
                                                    cout<                                                }
                                                cout<                                            }
                                        }

                                    }
                                }


                            }
                        }
                    }

                }

            }
            else if(command == ">")
            {
                cin>>command;
                for(int i = 0 ; i < data[0].size(); ++i)
                    cout<                cout<                for(int i = 1; i < data.size(); ++i)
                {
                    for(int j = 0 ; j < data[i].size(); ++j)
                    {
                        if(j == positionOfColumn2)
                        {
                            int integer=atoi(data[i][j].c_str());///string转为int
                            int integer1 = atoi(command.c_str());
                            if(integer > integer1)
                            {
                                for(int k = 0; k < data[i].size(); ++k)
                                {
                                    cout<                                }
                                cout<
                            }
                        }

                    }
                }
            }
            else if(command == "<")
            {
                cin>>command;
                for(int i = 0 ; i < data[0].size(); ++i)
                    cout<                cout<                for(int i = 1; i < data.size(); ++i)
                {
                    for(int j = 0 ; j < data[i].size(); ++j)
                    {
                        if(j == positionOfColumn2)
                        {
                            int integer=atoi(data[i][j].c_str());///string转为int
                            int integer1 = atoi(command.c_str());
                            if(integer < integer1)
                            {
                                for(int k = 0; k < data[i].size(); ++k)
                                {
                                    cout<                                }
                                cout<
                            }
                        }

                    }
                }
            }
            else if(command == "<>")
            {
                cin>>command;
                for(int i = 0 ; i < data[0].size(); ++i)
                    cout<                cout<                for(int i = 1; i < data.size(); ++i)
                {
                    for(int j = 0 ; j < data[i].size(); ++j)
                    {
                        if(j == positionOfColumn2)
                        {
                            int integer=atoi(data[i][j].c_str());///string转为int
                            int integer1 = atoi(command.c_str());
                            if(integer != integer1)
                            {
                                for(int k = 0; k < data[i].size(); ++k)
                                {
                                    cout<                                }
                                cout<
                            }
                        }

                    }
                }
            }
            else if(command == ">=")
            {
                cin>>command;
                for(int i = 0 ; i < data[0].size(); ++i)
                    cout<                cout<                for(int i = 1; i < data.size(); ++i)
                {
                    for(int j = 0 ; j < data[i].size(); ++j)
                    {
                        if(j == positionOfColumn2)
                        {
                            int integer=atoi(data[i][j].c_str());///string转为int
                            int integer1 = atoi(command.c_str());
                            if(integer >= integer1)
                            {
                                for(int k = 0; k < data[i].size(); ++k)
                                {
                                    cout<                                }
                                cout<
                            }
                        }

                    }
                }
            }
            else if(command == "<=")
            {
                cin>>command;
                for(int i = 0 ; i < data[0].size(); ++i)
                    cout<                cout<                for(int i = 1; i < data.size(); ++i)
                {
                    for(int j = 0 ; j < data[i].size(); ++j)
                    {
                        if(j == positionOfColumn2)
                        {
                            int integer=atoi(data[i][j].c_str());///string转为int
                            int integer1 = atoi(command.c_str());
                            if(integer <= integer1)
                            {
                                for(int k = 0; k < data[i].size(); ++k)
                                {
                                    cout<                                }
                                cout<
                            }
                        }

                    }
                }
            }
            else if(command == "BETWEEN")
            {
                int number1;
                int number2;
                cin>>number1;
                cin>>command;
                transform(command.begin(),command.end(),command.begin(),towupper);
                if(command == "AND")
                {
                    cin>>number2;
                    int maxn = number1;
                    if(maxn > number2)
                    {
                        maxn = number2;
                        number2 = number1;
                        number1 = maxn;
                    }
                    for(int i = 0 ; i < data[0].size(); ++i)
                        cout<                    cout<                    for(int i = 1; i < data.size(); ++i)
                    {
                        for(int j = 0 ; j < data[i].size(); ++j)
                        {
                            if(j == positionOfColumn2)
                            {
                                int integer=atoi(data[i][j].c_str());///string转为int
                                if(integer > number1 && integer < number2)
                                {
                                    for(int k = 0; k < data[i].size(); ++k)
                                    {
                                        cout<                                    }
                                    cout<
                                }
                            }

                        }
                    }
                }

            }
            else if(command == "IN")
            {
                cin>>command;
                if(command == "(")
                {
                    vector values;
                    cin>>command;
                    while(command != ")")
                    {
                        int commandint = atoi(command.c_str());
                        values.push_back(commandint);
                        cin>>command;
                    }
                    for(int i = 0 ; i < data[0].size(); ++i)
                        cout<                    cout<                    for(int i = 1; i < data.size(); ++i)
                    {
                        for(int j = 0 ; j < data[i].size(); ++j)
                        {
                            if(j == positionOfColumn2)
                            {
                                int integer=atoi(data[i][j].c_str());///string转为int
                                vector::iterator val;
                                for(val = values.begin(); val <= values.end(); ++val)
                                {
                                    if(integer == *val)
                                    {
                                        for(int k = 0; k < data[i].size(); ++k)
                                        {
                                            cout<                                        }
                                        cout<
                                    }
                                }

                            }

                        }
                    }

                }

            }
        }

    }
    else
    {
        cout<<"The user does not have the authority"<        return;
    }

}



void Database::AppointSelect(string column,string name,User user,Database database)///选择语句 select column from TABLE WHERE 属性 = values
///select column from TABLE ;--*****************************************问题
{
    int key = 0;///是否拥有权限
    vector::iterator it;
    for(it=user.User_select.begin(); it!=user.User_select.end();)
    {
        if(*it  == name)
        {
            key = 1;
            break;
        }

        else
            ++it;    //指向下一个位置
    }

    if(key == 1)
    {
        int number;
        char line[100];
        FILE *fp;
        string path;
        string command;///命令
        path = database.getAllpath() + name+".txt";
        number=0;
        ofstream ofile;///向文件中写入数据
        while((fp = fopen(path.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
        {
            cout<<"Table name input error:  ";
            cin>>name;
            if(name == "ENDINPUT")
                return;
            path = database.getAllpath() + name +".txt";
        }
        fclose(fp);

        ///----------------------读出INDEX存入data2中---------------------------------///

        vector >  data2;///存索引数据
        vector rowdata;
        string t_file = "F:\\database\\INDEX.txt";
        ifstream fin1(t_file.c_str());

        while(fin1.getline(line,100))
        {
            char *p;
            const char *d = "_";
            p = strtok(line,d);
            while(p)
            {
                ///printf("%s\n",p);
                rowdata.push_back(p);
                p=strtok(NULL,d);
            }
            data2.push_back(rowdata);
            rowdata.clear();
        }
        fin1.close();
        ///-------------------------------------------------------///
        ///---------------------判断表是否拥有索引----------------------------------///
        int exist = 0;
        for(int i = 0 ; i < data2.size(); ++i)
        {
            if(name == data2[i][0])///存在
            {
                exist  = 1;
            }
        }

        ///--------------------------------将数据保存在data里--------------------///////////
        vector > data;///数据
//        vector rowdata;
        int hang = 0;///data行
        //cout<<"DDDDDDD"<        ifstream fin(path.c_str());
        string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
        while(fin.getline(line,100))///从s开始读出存入data中。
        {
            if(number == 1)
            {
                char *p;
                const char *d = " ";
                p = strtok(line,d);
                while(p)
                {
                    //cout<                    rowdata.push_back(p);
                    p=strtok(NULL,d);
                }
                //cout<                data.push_back(rowdata);
                rowdata.clear();
            }
            if(strcmp(s.c_str(),line) == 0)
            {
                number = 1;
            }

        }
        fin.close();

        if(exist == 1)
        {
            vector indexdata;
            string file_index = database.getAllpath() + name + "_index.txt";
            fin1.open(file_index.c_str());
            while(fin1.getline(line,100))
            {
                char *p;
                const char *d = " ";
                p = strtok(line,d);
                while(p)
                {
                    indexdata.push_back(p);
                    p=strtok(NULL,d);
                }
            }
            fin1.close();
            if(column == indexdata[0])
            {
                cin>>command;
                transform(command.begin(),command.end(),command.begin(),towupper);
                if(command == ";")
                {
                    for(int i = 0 ; i < indexdata.size(); ++i)
                    {
                        cout<                    }
                }
                else if(command == "UNION" || command == "INTERSECT" || command == "MINUS")
                {
                    string command1;
                    cin>>command1;
                    transform(command1.begin(),command1.end(),command1.begin(),towupper);
                    if(command1 == "SELECT")///SELECT * FROM 表名称
                    {
                        string Newcommand;
                        cin>>command1;
                        cin>>Newcommand;
                        transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
                        if(Newcommand == "FROM")
                        {
                            cin>>Newcommand;
                            database.UIMSelect(command1,Newcommand,user,database,indexdata,command);
                        }

                    }
                }
                else if(command == "ORDER")
                {
                    cin>>command;
                    transform(command.begin(),command.end(),command.begin(),towupper);
                    if(command == "BY")
                    {
                        cin>>command;
                        if(command == column)
                        {
                            string temp;
                            int positionOfColumn = -1;///column的位置
                            int LineOfCommand = 0;///
                            ///----------------判断是否存在command---------------//
                            for(int j = 0 ; j < data[0].size(); ++j)
                            {
                                if(data[0][j] == column)
                                {
                                    positionOfColumn = j;

                                }

                            }
                            if(positionOfColumn == -1)
                                return;
                            for(int i = 1 ; i < data.size(); ++i)
                            {
                                for(int j = i ; j < data.size(); ++j)
                                {
                                    if(data[i][positionOfColumn] > data[j][positionOfColumn])
                                    {
                                        temp = data[i][positionOfColumn];
                                        data[i][positionOfColumn] = data[j][positionOfColumn];
                                        data[j][positionOfColumn] = temp;
                                    }
                                }
                            }
                            cin>>command;
                            transform(command.begin(),command.end(),command.begin(),towupper);
                            if(command == ";")
                            {
                                for(int i = 0 ; i < data.size(); ++i)
                                {
                                    cout<                                    cout<                                }

                            }
                            else if(command == "DECS")
                            {

                                cout<                                cout<                                for(int i = data.size() - 1 ; i > 0; --i)
                                {
                                    cout<                                    cout<                                }
                            }
                        }

                    }
                }
                else if(command == "WHERE")
                {
                    cin>>command;
                    int positionOfColumn2;///command的位置
                    int positionOfColumn;///column的位置
                    int LineOfCommand = 0;///
                    int temp1 = 0;
                    int temp2 = 0;
                    ///----------------判断是否存在command---------------//
                    for(int j = 0 ; j < data[0].size(); ++j)
                    {
                        if(data[0][j] == command)
                        {
                            positionOfColumn2 = j;
                            temp1 = 1;
                        }
                        if(data[0][j] == column)
                        {
                            positionOfColumn = j;
                            temp2 = 1;
                        }

                    }
                    if(temp1 == 0 || temp2 == 0)
                        return;
                    //cout<                    cin>>command;///输入命令
                    transform(command.begin(),command.end(),command.begin(),towupper);
                    if(command == "=")
                    {
                        cin>>command;
                        cout<                        for(int i = 1; i < data.size(); ++i)
                        {
                            for(int j = 0 ; j < data[i].size(); ++j)
                            {
                                if(j == positionOfColumn2)
                                {
                                    if(data[i][j] == command)
                                    {
                                        for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                        {
                                            if(j1 == positionOfColumn)
                                            {
                                                cout<
                                            }

                                        }
                                    }
                                }

                            }
                        }
                    }
                    else if(command == ">")
                    {
                        cin>>command;
                        cout<                        for(int i = 1; i < data.size(); ++i)
                        {
                            for(int j = 0 ; j < data[i].size(); ++j)
                            {
                                if(j == positionOfColumn2)
                                {
                                    //int integer=atoi(data[i][j].c_str());///string转为int
                                    if(atoi(data[i][j].c_str()) > atoi(command.c_str()))
                                    {
                                        for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                        {
                                            if(j1 == positionOfColumn)
                                            {
                                                cout<
                                            }

                                        }
                                    }
                                }

                            }
                        }
                    }
                    else if(command == "<")
                    {
                        cin>>command;
                        cout<                        for(int i = 1; i < data.size(); ++i)
                        {
                            for(int j = 0 ; j < data[i].size(); ++j)
                            {
                                if(j == positionOfColumn2)
                                {
                                    //int integer=atoi(data[i][j].c_str());///string转为int
                                    if(atoi(data[i][j].c_str()) < atoi(command.c_str()))
                                    {
                                        for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                        {
                                            if(j1 == positionOfColumn)
                                            {
                                                cout<
                                            }

                                        }
                                    }
                                }

                            }
                        }
                    }
                    else if(command == ">=")
                    {
                        cin>>command;
                        cout<                        for(int i = 1; i < data.size(); ++i)
                        {
                            for(int j = 0 ; j < data[i].size(); ++j)
                            {
                                if(j == positionOfColumn2)
                                {
                                    //int integer=atoi(data[i][j].c_str());///string转为int
                                    if(atoi(data[i][j].c_str()) >= atoi(command.c_str()))
                                    {
                                        for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                        {
                                            if(j1 == positionOfColumn)
                                            {
                                                cout<
                                            }

                                        }
                                    }
                                }

                            }
                        }
                    }
                    else if(command == "<=")
                    {
                        cin>>command;
                        cout<                        for(int i = 1; i < data.size(); ++i)
                        {
                            for(int j = 0 ; j < data[i].size(); ++j)
                            {
                                if(j == positionOfColumn2)
                                {
                                    //int integer=atoi(data[i][j].c_str());///string转为int
                                    if(atoi(data[i][j].c_str()) <= atoi(command.c_str()))
                                    {
                                        for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                        {
                                            if(j1 == positionOfColumn)
                                            {
                                                cout<
                                            }

                                        }
                                    }
                                }

                            }
                        }
                    }
                    else if(command == "<>")
                    {
                        cin>>command;
                        cout<                        for(int i = 1; i < data.size(); ++i)
                        {
                            for(int j = 0 ; j < data[i].size(); ++j)
                            {
                                if(j == positionOfColumn2)
                                {
                                    //int integer=atoi(data[i][j].c_str());///string转为int
                                    if(atoi(data[i][j].c_str()) != atoi(command.c_str()))
                                    {
                                        for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                        {
                                            if(j1 == positionOfColumn)
                                            {
                                                cout<
                                            }

                                        }
                                    }
                                }

                            }
                        }
                    }
                    else if(command == "BETWEEN")
                    {
                        int number1;
                        int number2;
                        cin>>number1;
                        cin>>command;
                        transform(command.begin(),command.end(),command.begin(),towupper);
                        if(command == "AND")
                        {
                            cin>>number2;
                            int maxn = number1;
                            if(maxn > number2)
                            {
                                maxn = number2;
                                number2 = number1;
                                number1 = maxn;
                            }
                            for(int i = 0 ; i < data[0].size(); ++i)
                                cout<                            cout<                            for(int i = 1; i < data.size(); ++i)
                            {
                                for(int j = 0 ; j < data[i].size(); ++j)
                                {
                                    if(j == positionOfColumn2)
                                    {
                                        int integer=atoi(data[i][j].c_str());///string转为int
                                        if(integer > number1 && integer < number2)
                                        {
                                            for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                            {
                                                if(j1 == positionOfColumn)
                                                {
                                                    cout<
                                                }

                                            }
                                        }
                                    }

                                }
                            }
                        }

                    }
                    else if(command == "IN")
                    {
                        cin>>command;
                        if(command == "(")
                        {
                            vector values;
                            vector values1;
                            cin>>command;
                            string command1 = command;
                            transform(command1.begin(),command1.end(),command1.begin(),towupper);
                            if(command1 == "SELECT")///SELECT sno FROM 表名称
                            {
                                string Newcommand;
                                cin>>command1;
                                cin>>Newcommand;
                                transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
                                if(Newcommand == "FROM")
                                {
                                    cin>>Newcommand;
                                    database.NESTSelect(command1,Newcommand,user,database,values1);
                                    if(values1.size() == 0)
                                    {
                                        cout<<"SELECT ERROR"<                                        return;
                                    }
                                    else
                                    {
                                        cout<                                        for(int i = 1; i < data.size(); ++i)
                                        {
                                            for(int j = 0 ; j < data[i].size(); ++j)
                                            {
                                                if(j == positionOfColumn2)
                                                {
                                                    string integer = data[i][j];
                                                    vector::iterator val;
                                                    for(val = values1.begin(); val < values1.end(); ++val)
                                                    {
                                                        if(integer == *val)
                                                        {
                                                            cout<                                                            break;
                                                        }
                                                    }

                                                }

                                            }
                                        }
                                    }

                                }


                            }
                            else
                            {
                                while(command != ")")
                                {
                                    int commandint = atoi(command.c_str());
                                    values.push_back(commandint);
                                    cin>>command;
                                }
                                for(int i = 0 ; i < data[0].size(); ++i)
                                    cout<                                cout<                                for(int i = 1; i < data.size(); ++i)
                                {
                                    for(int j = 0 ; j < data[i].size(); ++j)
                                    {
                                        if(j == positionOfColumn2)
                                        {
                                            int integer=atoi(data[i][j].c_str());///string转为int
                                            vector::iterator val;
                                            for(val = values.begin(); val < values.end(); ++val)
                                            {
                                                if(integer == *val)
                                                {
                                                    cout<                                                }
                                            }

                                        }

                                    }
                                }

                            }


                        }

                    }
                }

            }
        }
        else
        {
///--------------------------------------------------------------------------------///
            cin>>command;
            transform(command.begin(),command.end(),command.begin(),towupper);
            if(command == ";")
            {
                int positionOfColumn;///column的位置
                int LineOfCommand = 0;///
                int temp = 0;
                ///----------------判断是否存在command---------------//
                for(int j = 0 ; j < data[0].size(); ++j)
                {
                    if(data[0][j] == column)
                    {
                        positionOfColumn = j;
                        temp = 1;
                    }

                }
                if(temp == 0)
                    return;
                for(int i = 0 ; i < data.size(); ++i)
                {
                    cout<                    cout<                }
            }
            else if(command == "ORDER")
            {
                cin>>command;
                transform(command.begin(),command.end(),command.begin(),towupper);
                if(command == "BY")
                {
                    cin>>command;
                    if(command == column)
                    {
                        string temp;
                        int positionOfColumn = -1;///column的位置
                        int LineOfCommand = 0;///
                        ///----------------判断是否存在command---------------//
                        for(int j = 0 ; j < data[0].size(); ++j)
                        {
                            if(data[0][j] == column)
                            {
                                positionOfColumn = j;

                            }

                        }
                        if(positionOfColumn == -1)
                            return;
                        for(int i = 1 ; i < data.size(); ++i)
                        {
                            for(int j = i ; j < data.size(); ++j)
                            {
                                if(data[i][positionOfColumn] > data[j][positionOfColumn])
                                {
                                    temp = data[i][positionOfColumn];
                                    data[i][positionOfColumn] = data[j][positionOfColumn];
                                    data[j][positionOfColumn] = temp;
                                }
                            }
                        }
                        cin>>command;
                        transform(command.begin(),command.end(),command.begin(),towupper);
                        if(command == ";")
                        {
                            for(int i = 0 ; i < data.size(); ++i)
                            {
                                cout<                                cout<                            }

                        }
                        else if(command == "DECS")
                        {
                            cout<                            cout<                            for(int i = data.size() - 1 ; i > 0; --i)
                            {
                                cout<                                cout<                            }
                        }
                    }

                }
            }
            else if(command == "UNION" || command == "INTERSECT" || command == "MINUS")
            {
                vector onedata;
                int positionOfColumn;///column的位置
                int LineOfCommand = 0;///
                int temp = 0;
                ///----------------判断是否存在command---------------//
                for(int j = 0 ; j < data[0].size(); ++j)
                {
                    if(data[0][j] == column)
                    {
                        positionOfColumn = j;
                        temp = 1;
                    }

                }
                if(temp == 0)
                    return;
                for(int i = 0 ; i < data.size(); ++i)
                {
                    onedata.push_back(data[i][positionOfColumn]);
                }
                string command1;
                cin>>command1;
                transform(command1.begin(),command1.end(),command1.begin(),towupper);
                if(command1 == "SELECT")///SELECT * FROM 表名称
                {
                    string Newcommand;
                    cin>>command1;
                    cin>>Newcommand;
                    transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
                    if(Newcommand == "FROM")
                    {
                        cin>>Newcommand;
                        database.UIMSelect(command1,Newcommand,user,database,onedata,command);
                    }

                }
            }
            else if(command == "WHERE")
            {
                cin>>command;
                int positionOfColumn2;///command的位置
                int positionOfColumn;///column的位置
                int LineOfCommand = 0;///
                int temp1 = 0;
                int temp2 = 0;
                ///----------------判断是否存在command---------------//
                for(int j = 0 ; j < data[0].size(); ++j)
                {
                    if(data[0][j] == command)
                    {
                        positionOfColumn2 = j;
                        temp1 = 1;
                    }
                    if(data[0][j] == column)
                    {
                        positionOfColumn = j;
                        temp2 = 1;
                    }

                }
                if(temp1 == 0 || temp2 == 0)
                    return;
                //cout<                cin>>command;///输入命令
                transform(command.begin(),command.end(),command.begin(),towupper);
                if(command == "=")
                {
                    cin>>command;
                    cout<                    for(int i = 1; i < data.size(); ++i)
                    {
                        for(int j = 0 ; j < data[i].size(); ++j)
                        {
                            if(j == positionOfColumn2)
                            {
                                if(data[i][j] == command)
                                {
                                    for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                    {
                                        if(j1 == positionOfColumn)
                                        {
                                            cout<
                                        }

                                    }
                                }
                            }

                        }
                    }
                }
                if(command == ">")
                {
                    cin>>command;
                    cout<                    for(int i = 1; i < data.size(); ++i)
                    {
                        for(int j = 0 ; j < data[i].size(); ++j)
                        {
                            if(j == positionOfColumn2)
                            {
                                //int integer=atoi(data[i][j].c_str());///string转为int
                                if(atoi(data[i][j].c_str()) > atoi(command.c_str()))
                                {
                                    for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                    {
                                        if(j1 == positionOfColumn)
                                        {
                                            cout<
                                        }

                                    }
                                }
                            }

                        }
                    }
                }
                if(command == "<")
                {
                    cin>>command;
                    cout<                    for(int i = 1; i < data.size(); ++i)
                    {
                        for(int j = 0 ; j < data[i].size(); ++j)
                        {
                            if(j == positionOfColumn2)
                            {
                                //int integer=atoi(data[i][j].c_str());///string转为int
                                if(atoi(data[i][j].c_str()) < atoi(command.c_str()))
                                {
                                    for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                    {
                                        if(j1 == positionOfColumn)
                                        {
                                            cout<
                                        }

                                    }
                                }
                            }

                        }
                    }
                }
                if(command == ">=")
                {
                    cin>>command;
                    cout<                    for(int i = 1; i < data.size(); ++i)
                    {
                        for(int j = 0 ; j < data[i].size(); ++j)
                        {
                            if(j == positionOfColumn2)
                            {
                                //int integer=atoi(data[i][j].c_str());///string转为int
                                if(atoi(data[i][j].c_str()) >= atoi(command.c_str()))
                                {
                                    for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                    {
                                        if(j1 == positionOfColumn)
                                        {
                                            cout<
                                        }

                                    }
                                }
                            }

                        }
                    }
                }
                if(command == "<=")
                {
                    cin>>command;
                    cout<                    for(int i = 1; i < data.size(); ++i)
                    {
                        for(int j = 0 ; j < data[i].size(); ++j)
                        {
                            if(j == positionOfColumn2)
                            {
                                //int integer=atoi(data[i][j].c_str());///string转为int
                                if(atoi(data[i][j].c_str()) <= atoi(command.c_str()))
                                {
                                    for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                    {
                                        if(j1 == positionOfColumn)
                                        {
                                            cout<
                                        }

                                    }
                                }
                            }

                        }
                    }
                }
                if(command == "<>")
                {
                    cin>>command;
                    cout<                    for(int i = 1; i < data.size(); ++i)
                    {
                        for(int j = 0 ; j < data[i].size(); ++j)
                        {
                            if(j == positionOfColumn2)
                            {
                                //int integer=atoi(data[i][j].c_str());///string转为int
                                if(atoi(data[i][j].c_str()) != atoi(command.c_str()))
                                {
                                    for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                    {
                                        if(j1 == positionOfColumn)
                                        {
                                            cout<
                                        }

                                    }
                                }
                            }

                        }
                    }
                }
                else if(command == "BETWEEN")
                {
                    int number1;
                    int number2;
                    cin>>number1;
                    cin>>command;
                    transform(command.begin(),command.end(),command.begin(),towupper);
                    if(command == "AND")
                    {
                        cin>>number2;
                        int maxn = number1;
                        if(maxn > number2)
                        {
                            maxn = number2;
                            number2 = number1;
                            number1 = maxn;
                        }
                        for(int i = 0 ; i < data[0].size(); ++i)
                            cout<                        cout<                        for(int i = 1; i < data.size(); ++i)
                        {
                            for(int j = 0 ; j < data[i].size(); ++j)
                            {
                                if(j == positionOfColumn2)
                                {
                                    int integer=atoi(data[i][j].c_str());///string转为int
                                    if(integer > number1 && integer < number2)
                                    {
                                        for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                                        {
                                            if(j1 == positionOfColumn)
                                            {
                                                cout<
                                            }

                                        }
                                    }
                                }

                            }
                        }
                    }

                }
                else if(command == "IN")
                {
                    cin>>command;
                    if(command == "(")
                    {
                        vector values;
                        vector values1;
                        cin>>command;
                        string command1 = command;
                        transform(command1.begin(),command1.end(),command1.begin(),towupper);
                        if(command1 == "SELECT")///SELECT * FROM 表名称
                        {
                            string Newcommand;
                            cin>>command1;
                            cin>>Newcommand;
                            transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
                            if(Newcommand == "FROM")
                            {
                                cin>>Newcommand;
                                database.NESTSelect(command1,Newcommand,user,database,values1);
//                                for(int i = 1; i < values1.size(); ++i)
//                                    cout<                                cout<                                for(int i = 1; i < data.size(); ++i)
                                {
                                    for(int j = 0 ; j < data[i].size(); ++j)
                                    {
                                        if(j == positionOfColumn2)
                                        {
                                            string integer = data[i][j];
                                            vector::iterator val;
                                            for(val = values1.begin(); val < values1.end(); ++val)
                                            {
                                                if(integer == *val)
                                                {

                                                    cout<                                                }
                                            }

                                        }

                                    }
                                }
                            }


                        }
                        else
                        {
                            while(command != ")")
                            {
                                int commandint = atoi(command.c_str());
                                values.push_back(commandint);
                                cin>>command;
                            }
                            for(int i = 0 ; i < data[0].size(); ++i)///********************************************
                                cout<                            cout<                            for(int i = 1; i < data.size(); ++i)
                            {
                                for(int j = 0 ; j < data[i].size(); ++j)
                                {
                                    if(j == positionOfColumn2)
                                    {
                                        int integer=atoi(data[i][j].c_str());///string转为int
                                        vector::iterator val;
                                        for(val = values.begin(); val <= values.end(); ++val)
                                        {
                                            if(integer == *val)
                                            {

                                                cout<

                                            }
                                        }

                                    }

                                }
                            }

                        }


                    }

                }
            }
        }
    }
    else
    {
        cout<<"The user does not have the authority"<        return;
    }
}

void Database::NESTSelect(string column,string name,User user,Database database,vector&values)///嵌套查询
{
    ///--------------------------------将数据保存在data里--------------------///////////
    vector > data;///数据
    vector rowdata;
    string path = database.getAllpath() + name +".txt";
    ifstream fin(path.c_str());
    string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
    char line[100];
    int number = 0;
    while(fin.getline(line,100))///从s开始读出存入data中。
    {
        if(number == 1)
        {
            char *p;
            const char *d = " ";
            p = strtok(line,d);
            while(p)
            {
                //cout<                rowdata.push_back(p);
                p=strtok(NULL,d);
            }
            //cout<            data.push_back(rowdata);
            rowdata.clear();
        }
        if(strcmp(s.c_str(),line) == 0)
        {
            number = 1;
        }

    }
    fin.close();

    string command;
    cin>>command;
    transform(command.begin(),command.end(),command.begin(),towupper);
    if(command == ")")///SELECT SNO FROM TABLE
    {
        int positionOfColumn;
        int temp2 = 0;
        for(int j = 0 ; j < data[0].size(); ++j)
        {
            if(data[0][j] == column)
            {
                positionOfColumn = j;
                temp2 = 1;
            }

        }
        if(temp2 == 1)
        {
            for(int i = 1; i < data.size(); ++i)
            {
                for(int j = 0 ; j < data[i].size(); ++j)
                {
                    if(j == positionOfColumn)
                    {
                        values.push_back(data[i][j]);
                    }

                }
            }
        }
        else
        {
            cout<<"ERROR"<            return;
        }

    }
    else if(command == "WHERE")///SELECT SNO FROM TABLE WHERE CNO = VALUES
    {
        cin>>command;
        int positionOfColumn2;///command的位置
        int positionOfColumn;///column的位置
        int temp1 = 0;
        int temp2 = 0;
        ///----------------判断是否存在command---------------//
        for(int j = 0 ; j < data[0].size(); ++j)
        {
            if(data[0][j] == command)
            {
                positionOfColumn2 = j;
                temp1 = 1;
            }
            if(data[0][j] == column)
            {
                positionOfColumn = j;
                temp2 = 1;
            }

        }
        if(temp1 == 0 || temp2 == 0)
            return;
//        for(int t1 = 0 ; t1 < data.size(); ++t1)
//        {
//            for(int t2 = 0; t2 < data[t1].size() ; ++t2)
//                cout<//        }
        cin>>command;///输入命令
        if(command == "=")
        {
            cin>>command;
            for(int i = 1; i < data.size(); ++i)
            {
                for(int j = 0 ; j < data[i].size(); ++j)
                {
                    if(j == positionOfColumn2)
                    {
                        if(data[i][j] == command)
                        {
                            for(int j1 = 0 ; j1 < data[i].size(); ++j1)
                            {
                                if(j1 == positionOfColumn)
                                {
                                    values.push_back(data[i][j1]);
                                }

                            }
                        }
                    }

                }
            }
        }
    }
}

void Database::UIMSelect(string column,string name,User user,Database database,vectoronedata,string command1)///集合查询
{
    int key = 0;///是否拥有权限
    vector::iterator it;
    vector twodata;
    for(it=user.User_select.begin(); it!=user.User_select.end();)
    {
        if(*it  == name)
        {
            key = 1;
            break;
        }

        else
            ++it;    //指向下一个位置
    }

    if(key == 1)
    {
        int number;
        char line[100];
        FILE *fp;
        string path;
        string command;///命令
        path = database.getAllpath() + name+".txt";
        number=0;
        ofstream ofile;///向文件中写入数据
        while((fp = fopen(path.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
        {
            cout<<"Table name input error:  ";
            cin>>name;
            if(name == "ENDINPUT")
                return;
            path = database.Allpath + name +".txt";
        }
        fclose(fp);
        ///--------------------------------将数据保存在data里--------------------///////////
        vector > data;///数据
        vector rowdata;
        int hang = 0;///data行
        //cout<<"DDDDDDD"<        ifstream fin(path.c_str());
        string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
        while(fin.getline(line,100))///从s开始读出存入data中。
        {
            if(number == 1)
            {
                char *p;
                const char *d = " ";
                p = strtok(line,d);
                while(p)
                {
                    rowdata.push_back(p);
                    p=strtok(NULL,d);
                }
                data.push_back(rowdata);
                rowdata.clear();
            }
            if(strcmp(s.c_str(),line) == 0)
            {
                number = 1;
            }

        }
        fin.close();
///--------------------------------------------------------------------------------///
        cin>>command;
        transform(command.begin(),command.end(),command.begin(),towupper);
        if(command == ";")
        {
            int positionOfColumn;///column的位置

            int temp = 0;
            ///----------------判断是否存在command---------------//
            for(int j = 0 ; j < data[0].size(); ++j)
            {
                if(data[0][j] == column)
                {
                    positionOfColumn = j;
                    temp = 1;
                }

            }
            if(temp == 0)
                return;
            for(int i = 0 ; i < data.size(); ++i)
            {
                twodata.push_back(data[i][positionOfColumn]);
            }
            if(onedata[0] == twodata[0])
            {
                vector::iterator oneiterator;
                vector::iterator twoiterator;
                if(command1 == "UNION")
                {
                    vector uniondata;
                    sort(onedata.begin(),onedata.end());
                    sort(twodata.begin(),twodata.end());
                    set_union(onedata.begin(),onedata.end(),twodata.begin(),twodata.end(),back_inserter(uniondata));//求交集
                    for(int i = uniondata.size() - 1; i > -1; --i)
                        cout<
                }
                else if(command1 == "INTERSECT")
                {
                    ///两个vector求交集
                    vector intersectdata;
                    sort(onedata.begin(),onedata.end());
                    sort(twodata.begin(),twodata.end());
                    set_intersection(onedata.begin(),onedata.end(),twodata.begin(),twodata.end(),back_inserter(intersectdata));//求交集
                    for(int i = intersectdata.size() - 1; i > -1; --i)
                        cout<                }
                else if(command1 == "MINUS")
                {
                    ///两个vector求交集
                    vector minusdata;
                    sort(onedata.begin(),onedata.end());
                    sort(twodata.begin(),twodata.end());
                    set_intersection(onedata.begin(),onedata.end(),twodata.begin(),twodata.end(),back_inserter(minusdata));//求交集
                    cout<                    for(int i = 0; i < onedata.size() -1; ++i)
                    {
                        int datatemp = 0;
                        for(int j = 0 ; j < twodata.size() - 1 ; ++j)
                        {
                            if(onedata[i] == twodata[j])
                            {
                                datatemp = 1;
                            }
                        }
                        if(datatemp == 0)
                        {
                            cout<                        }
                    }
                }
            }
            else
            {
                cout<<"ERROR: Different attributes"<            }

        }
    }
}


void Database::Grant(string jurisdiction,User &user,Database database)///授权语句
{
    string command;
    string type;///对象类型
    string object;///对象
    string role;///用户
    ofstream ofile;
    cin>>command;
    transform(command.begin(),command.end(),command.begin(),towupper);
    if(command == "ON")
    {
        cin>>type;
        transform(type.begin(),type.end(),type.begin(),towupper);
        if(type == "TABLE")
        {
            cin>>object;
            cin>>command;
            transform(command.begin(),command.end(),command.begin(),towupper);
            if(command == "TO")
            {
                cin>>role;
                fstream _file;
                string path = "F:\\" + role + ".txt";
                _file.open(path.c_str(),ios::in);
                if(!_file)
                {
                    cout<<"User does not exist"<                    _file.close();
                    return;
                }
                else
                {
                    _file.close();
                    ///--------------------------------将数据保存在data里--------------------///////////
                    vector > data;///数据
                    vector rowdata;
                    int hang = 0;///data行
                    ifstream fin(path.c_str());
                    ofstream ofile;
                    char line[100];
                    ///cout<<"DDDDDDD"<                    while(fin.getline(line,100))///从第零行第零列开始
                    {

                        char *p;
                        const char *d = " ";
                        p = strtok(line,d);
                        while(p)
                        {
                            ///printf("%s\n",p);
                            rowdata.push_back(p);
                            p=strtok(NULL,d);
                        }
                        data.push_back(rowdata);
                        rowdata.clear();

                    }
                    fin.close();
                    ///-----------------------对用户添加对该表的权限-------------------------------------------------//
                    if(jurisdiction == "DELETE")
                        data[2].push_back(object);
                    else if(jurisdiction == "DROP")
                        data[4].push_back(object);    //删除元素,返回值指向已删除元素的下一个位置
                    else if(jurisdiction == "INSERT")
                        data[1].push_back(object);    //删除元素,返回值指向已删除元素的下一个位置
                    else if(jurisdiction == "SELECT")
                        data[0].push_back(object);    //删除元素,返回值指向已删除元素的下一个位置
                    else if(jurisdiction == "UPDATE")
                        data[3].push_back(object);    //删除元素,返回值指向已删除元素的下一个位置

                    ///----------清除表数据-----///////
                    ofile.open(path.c_str(),ios::trunc);///删除表中的数据
                    ofile.close();
                    ///---------------------------///

                    ///------------将数据重新输入--------///
                    ofile.open(path.c_str(),ios::app);     ///作为输出文件打开
                    for(int i = 0 ; i < data[0].size(); ++i)
                        ofile<                    ofile<                    for(int i = 0 ; i < data[1].size(); ++i)
                        ofile<                    ofile<                    for(int i = 0 ; i < data[2].size(); ++i)
                        ofile<                    ofile<                    for(int i = 0 ; i < data[3].size(); ++i)
                        ofile<                    ofile<                    for(int i = 0 ; i < data[4].size(); ++i)
                        ofile<                    ofile<                    ofile.close();
                    ///-----------------------------------------------------------------------------------------------///
                    cout<<"GRANT success"<                }
            }


        }
    }
    return;


}

void Database::Revoke(string jurisdiction,User &user,Database database)///收回权限
{
    string command;
    string type;///对象类型
    string object;///对象
    string role;///用户
    ofstream ofile;
    cin>>command;
    transform(command.begin(),command.end(),command.begin(),towupper);
    if(command == "ON")
    {
        cin>>type;
        transform(type.begin(),type.end(),type.begin(),towupper);
        if(type == "TABLE")
        {
            cin>>object;
            cin>>command;
            transform(command.begin(),command.end(),command.begin(),towupper);
            if(command == "FROM")
            {
                cin>>role;
                fstream _file;
                string path = "F:\\" + role + ".txt";
                _file.open(path.c_str(),ios::in);
                if(!_file)
                {
                    cout<<"User does not exist"<                    _file.close();
                    return;
                }
                else
                {
                    _file.close();
                    ///--------------------------------将数据保存在data里--------------------///////////
                    vector > data;///数据
                    vector rowdata;
                    int hang = 0;///data行
                    ifstream fin(path.c_str());
                    ofstream ofile;
                    char line[100];
                    ///cout<<"DDDDDDD"<                    while(fin.getline(line,100))///从第零行第零列开始
                    {

                        char *p;
                        const char *d = " ";
                        p = strtok(line,d);
                        while(p)
                        {
                            ///printf("%s\n",p);
                            rowdata.push_back(p);
                            p=strtok(NULL,d);
                        }
                        data.push_back(rowdata);
                        rowdata.clear();

                    }
                    fin.close();
                    ///-----------------------对用户删除对该表的权限-------------------------------------------------///
                    vector::iterator it;
                    if(jurisdiction == "DELETE")
                        for(it=data[2].begin(); it!=data[2].end();)
                        {
                            if(*it  == object)
                            {
                                data[2].erase(it);    //删除元素,返回值指向已删除元素的下一个位置
                                break;
                            }

                            else
                                ++it;    //指向下一个位置
                        }
                    else if(jurisdiction == "DROP")
                        for(it=data[4].begin(); it!= data[4].end();)
                        {
                            if(*it  == object)
                            {
                                data[4].erase(it);    //删除元素,返回值指向已删除元素的下一个位置
                                break;
                            }

                            else
                                ++it;    //指向下一个位置
                        }
                    else if(jurisdiction == "INSERT")
                        for(it=data[1].begin(); it!=data[1].end();)
                        {
                            if(object == *it)
                            {
                                data[1].erase(it);    //删除元素,返回值指向已删除元素的下一个位置
                                cout<<*it<                                break;
                            }

                            else
                                ++it;    //指向下一个位置
                        }
                    else if(jurisdiction == "SELECT")
                        for(it=data[0].begin(); it!=data[0].end();)
                        {
                            if(*it  == object)
                            {
                                data[0].erase(it);    //删除元素,返回值指向已删除元素的下一个位置
                                break;
                            }

                            else
                                ++it;    //指向下一个位置
                        }
                    else if(jurisdiction == "UPDATE")
                        for(it=data[3].begin(); it!=data[3].end();)
                        {
                            if(*it  == object)
                            {
                                data[3].erase(it);    //删除元素,返回值指向已删除元素的下一个位置
                                break;
                            }

                            else
                                ++it;    //指向下一个位置
                        }
                    ///----------清除表数据-----///////
                    ofile.open(path.c_str(),ios::trunc);///删除表中的数据
                    ofile.close();
                    ///---------------------------///

                    ///------------将数据重新输入--------///
                    ofile.open(path.c_str(),ios::app);     ///作为输出文件打开
                    for(int i = 0 ; i < data[0].size(); ++i)
                        ofile<                    ofile<                    for(int i = 0 ; i < data[1].size(); ++i)
                        ofile<                    ofile<                    for(int i = 0 ; i < data[2].size(); ++i)
                        ofile<                    ofile<                    for(int i = 0 ; i < data[3].size(); ++i)
                        ofile<                    ofile<                    for(int i = 0 ; i < data[4].size(); ++i)
                        ofile<                    ofile<                    ofile.close();
                    ///-----------------------------------------------------------------------------------------------///
                    cout<<"Revoke success"<                }

            }

        }
    }
    return;
}

void CreateView(View &view,Database database)///CREATE VIEW V_Customer
///AS SELECT First_Name, Last_Name, Country
///FROM Customer;
{
    FILE *fp;
    ofstream ofile;
    string table_file;
    string command;
    table_file = database.getAllpath() + view.view_name +".txt";
    while((fp = fopen(table_file.c_str(),"r+")) != NULL) ///判断按读方式打 file_name 文件是否失败,如果存在
    {
        ///printf("The file can not be opened.\n");
        cout<<"Naming error";
        return;

    }
    fclose(fp);
    ofile.open(database.getFile_view().c_str(),ios::app);///存储表名
    ofile<    ofile.close();
    ofile.open(table_file.c_str(),ios::app);
    cin>>command;
    transform(command.begin(),command.end(),command.begin(),towupper);
    if(command == "SELECT")
    {
        while(command != ";")
        {
            ofile<            cin>>command;
            if(command == "stop")
                return;

        }
    }

    ofile.close();
}


void CreateIndex(Index &index,Database database)///CREATE INDEX index_name  ON table_name (column_name)
///输入表名,判断是否存在表,若不存在则return。若存在,将该表的数据存入vector >中,输入列名。
///判断列名是否存在,若不存在,则return。若存在则将该列按照升序存入index.file中。
{
    FILE *fp;
    ofstream ofile;///写入文件中
    ifstream fin;///从文件中输出
    int temp = 0;///当temp为0时,不存在该数据表。当temp为1时,存在该数据表。
    string index_file;
    string command;
    string table_file;///表的路径
    int column_position;///记录列的位置
    index_file = database.getAllpath() + index.index_name +".txt";

    while((fp = fopen(index_file.c_str(),"r+")) != NULL) ///判断按读方式打 file_name 文件是否失败,如果存在
    {
        ///printf("The file can not be opened.\n");
        cout<<"Naming error";
        return;

    }
    fclose(fp);
    fin.open(index_file.c_str());
    fin.close();
    ofile.open(database.getFile_index().c_str(),ios::app);///存储索引名
    ofile<    ofile.close();
    cin>>command;///table_name
    ///---------------------------查询是否存在table_name------------------/
    char line[100];
    fin.open(database.getFile_root_name().c_str());
    while(fin.getline(line,100))
    {

        if(strcmp(command.c_str(),line) == 0)
        {
            temp = 1;
            break;
        }
    }

    fin.close();
    ///------------------------------------------------------------------------/
    table_file = database.getAllpath() + command + ".txt";
    string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
    ///--------------------------------将s之后的数据保存在data里--------------------/////
    vector > data;///数据
    vector rowdata;
    fin.open(table_file.c_str());
    int s_temp = 0;///临时变量
    while(fin.getline(line,100))///从第零行第零列开始
    {
        if(strcmp(s.c_str(),line) == 0)
        {
            s_temp = 1;
        }
        if(s_temp == 1)
        {
            char *p;
            const char *d = " ";
            p = strtok(line,d);
            while(p)
            {
                rowdata.push_back(p);
                p=strtok(NULL,d);
            }
            data.push_back(rowdata);
            rowdata.clear();

        }

    }
    fin.close();
///--------------------------------------------------------------------------------///

    if(temp == 1)
    {
        cin>>command;
        if(command == "(")
        {
            cin>>command;///输入列
            for(int i = 0 ; i < data.size(); ++i)
            {
                for(int j = 0; j < data[i].size(); ++j)
                {
                    if(command.c_str() == data[i][j])
                    {
                        ofile.open(index_file.c_str(),ios::app);///存储索引名
                        for(int comi = 1; comi < data.size(); ++comi)
                        {
                            ofile<                        }

                    }
                }
            }
            cin>>command;
            if(command == ")")
            {
                cout<<"INDEX Success"<                return ;
            }

        }
    }
    return;
}

int main()
{
    ///--------------------------------将数据保存在data里--------------------///////////
    vector > data;///数据
    vector rowdata;
    int hang = 0;///data行
    string path = "F:\\user.txt";
    ifstream fin(path.c_str());
    char line[100];
    ///cout<<"DDDDDDD"<    while(fin.getline(line,100))///从第零行第零列开始
    {

        char *p;
        const char *d = " ";
        p = strtok(line,d);
        while(p)
        {
            ///printf("%s\n",p);
            rowdata.push_back(p);
            p=strtok(NULL,d);
        }
        data.push_back(rowdata);
        rowdata.clear();

    }
    fin.close();
///--------------------------------------------------------------------------------///
    string name;
    string password;
    int Userstatus = 0;
    cout<<"Input user: ";
    cin>>name;
    while(Userstatus == 0)
    {
        for(int i =1; i < data.size(); ++i)
        {
            if(data[i][0] == name)
            {
                Userstatus = i;
            }

        }
        if(Userstatus != 0)
        {
            break;
        }
        else
        {
            cout<<"ERROR: No user exists"<            cout<<"Input user: ";
            cin>>name;
        }
    }
    //cout<<"STATUS:  "<    string fileType = ".txt";
    vector > data1;///存储name的权限;
    path = "F:\\";
    path = path + name + fileType;
    fin.open(path.c_str());

    while(fin.getline(line,100))///从第零行第零列开始
    {

        char *p;
        const char *d = " ";
        p = strtok(line,d);
        while(p)
        {
            rowdata.push_back(p);
            p=strtok(NULL,d);
        }
        data1.push_back(rowdata);
        rowdata.clear();

    }
    fin.close();
    User user(data[Userstatus][0],data[Userstatus][1],data[Userstatus][2],data1[0],data1[1],data1[2],data1[3],data1[4]);
    cout<<"Enter password: ";
    cin>>password;
    while( password!= user.getPassword())
    {
        cout<<"ERROR: Password error"<        cout<<"Enter password: ";
        cin>>password;
    }
    cout<<"Welcom to the DCF"<    cout<<"DatabaseName:   "<<"database"<    cout<<"create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)"<    cout<<"create table NULL   NOTNULL   PRIMARY   FOREIGN"<    cout<<"drop table tabname "<    cout<<"Alter table tabname add column col type"<    cout<<"drop index idxname"<    int countoftable = 0;
    Table table[100];
    Database database;
    database.CreateDatabase();
    FILE file_root;
    while(1)
    {
        cout<<"sql> ";
        string command;
        cin>>command;
        transform(command.begin(),command.end(),command.begin(),towupper);
        if(command == "CREATE")
        {
            cin>>command;
            transform(command.begin(),command.end(),command.begin(),towupper);
            if(command == "TABLE")
            {
                database.CreateTable(table[countoftable],database,user);
                show_Table(table[countoftable].table_name,database);
                countoftable++;
            }
            else if(command == "VIEW")///CREATE VIEW V_Customer AS SELECT First_Name, Last_Name, Country FROM Customer;
            {
                View view;
                cin>>command;
                view.view_name = command;
                cin>>command;
                transform(command.begin(),command.end(),command.begin(),towupper);
                if(command == "AS")
                {
                    CreateView(view,database);
                }
            }
            else if(command == "INDEX")///CREATE INDEX index_name  ON table_name (column_name)
            {
                Index index;
                cin>>command;
                index.index_name= command;
                cin>>command;
                transform(command.begin(),command.end(),command.begin(),towupper);
                if(command == "ON")
                {
                    CreateIndex(index,database);
                }
            }
        }
        else if(command == "HELP")
        {
            cin>>command;
            if(command == "table")
            {
                cin>>command;
                show_Table(command,database);

            }
            else if(command == "database")
            {
                show_database(database);
            }
            else if(command == "view")
            {
                cin>>command;
                show_View(command,database);
            }
            else if(command == "index")
            {
                cin>>command;
                show_Index(command,database);
            }
        }
        else if(command == "INSERT")
        {
            cin>>command;
            transform(command.begin(),command.end(),command.begin(),towupper);
            if(command == "INTO")
            {
                cin>>command;
                database.Insert(command,user,database);
            }
        }
        else if(command == "DROP")
        {
            cin>>command;
            transform(command.begin(),command.end(),command.begin(),towupper);
            if(command == "TABLE")
            {
                cin>>command;
                database.DropTable(command,user,database);
            }
        }
        else if(command == "DELETE")///DELETE FROM Person WHERE LastName = 'Wilson'
        {
            cin>>command;
            transform(command.begin(),command.end(),command.begin(),towupper);
            if(command == "FROM")
            {
                cin>>command;
                database.DeleteTable(command,user,database);
            }
        }
        else if(command == "SELECT")///SELECT * FROM 表名称
        {
            string Newcommand;
            cin>>command;
            if(command == "*")
            {
                cin>>command;
                transform(command.begin(),command.end(),command.begin(),towupper);
                if(command == "FROM")
                {
                    cin>>command;
                    database.Select(command,user,database);
                }
            }
            else
            {
                cin>>Newcommand;
                transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
                if(Newcommand == "FROM")
                {
                    cin>>Newcommand;
                    database.AppointSelect(command,Newcommand,user,database);
                }
            }
        }
        else if(command == "UPDATE")
        {
            cin>>command;
            database.Update(command,user,database);
        }
        else if(command == "GRANT")
        {
            if(user.getUserstatus() == "Administrators")
            {
                cin>>command;
                transform(command.begin(),command.end(),command.begin(),towupper);
                database.Grant(command,user,database);
            }
            else
            {
                cout<<"The user has no permissions"<            }

        }
        else if(command == "REVOKE")
        {
            if(user.getUserstatus() == "Administrators")
            {
                cin>>command;
                transform(command.begin(),command.end(),command.begin(),towupper);
                database.Revoke(command,user,database);
            }
            else
            {
                cout<<"The user has no permissions"<            }

        }
        else if(command == "MODIFY")///修改用户
        {
            if(user.getUserstatus() == "user")
            {
                cout<<"YOU DO NOT HAVE THIS AUTHORITY"<            }
            else
            {
                string username;
                cin>>username;
                int tempname = 0;
                for(int i = 1 ; i < data.size(); ++i)
                {
                    if(username == data[i][0])
                    {
                        tempname = i;
                        break;
                    }
                }
                if(tempname != 0)
                {
                    string password;
                    cin>>username>>password;
                    data[tempname][0] = username;
                    data[tempname][1] = password;
                    ofstream ofile;
                    ofile.open("F:\\user.txt");
                    //ofile.clear();
                    for(int i = 0; i < data.size(); ++i)
                    {
                        for(int j = 0 ; j < data[i].size(); ++j)
                        {
                            ofile<                        }
                        ofile<                    }
                    ofile.close();
                    cout<<"Successful operation"<                }
            }
        }
        else if(command == "ADD")
        {
            cin>>command;
            transform(command.begin(),command.end(),command.begin(),towupper);
            if(command == "USER")
            {
                string username,password,status;
                vector da;
                cin>>username>>password>>status;
                int t = 0;
                for(int i = 1 ; i < data.size(); ++i)
                {
                    if(username == data[i][0])
                    {

                        t = 1;
                        break;
                    }
                }
                if(t == 1)
                {
                    cout<<"The user has already existed"<                }
                else
                {
                    if(status == "user" || status == "authority")
                    {
                        da.push_back(username);
                        da.push_back(password);
                        da.push_back(status);
                        data.push_back(da);
                        ofstream ofile;
                        ofile.open("F:\\user.txt");
                        //ofile.clear();
                        for(int i = 0; i < data.size(); ++i)
                        {
                            for(int j = 0 ; j < data[i].size(); ++j)
                            {
                                ofile<                            }
                            ofile<                        }
                        ofile.close();
                        cout<<"Successful operation"<                    }
                }
            }
        }
    }
}

你可能感兴趣的:(数据库课程设计代码)