📄 das.txt
字号:
#include "iostream"
#include "string"
#include "iomanip"
#include "stdlib.h"
#include "fstream"
using namespace std;
struct item
{
string name;
string type;
};
struct seq
{
item *t[10];
int num;
string name;
seq *next;
seq *follow;
};
int N;
seq *head;
void showFunction()
{
cout <<"输入功能"<<endl;
cout <<"create"<<endl<<"insert"<<endl<<"delete"<<endl<<"modify"<<endl<<"charu"<<endl<<"show"<<endl<<"exit"<<endl;
}
void showTable()
{
string name;
seq *r,*s;
r = head->follow;
while(r!=NULL)
{
cout <<"表名为:";
cout <<r->name<<endl;
N = r->num;
s = r;
while(s->next!=NULL)
{
int n = 0;
while(n!=N)
{
if(s->next->t[n])
cout <<s->next->t[n]->name<<setw(10);
else
cout <<"空"<<setw(10);
n++;
}
cout <<endl;
s = s->next;
}
r = r->follow;
}
cout <<"没有表了"<<endl;
}
void create() //////见表
{
string tName;int n=0;
seq *newhead = new seq;
seq *t = new seq;
cout<<"输入表名"<<endl;
cin >>tName;
newhead->name = tName;
cout <<"输入属性个数(第一个参数是主键)"<<endl;
cin >>N;
newhead->num = N;
newhead->next = t;
string type,vName;
while(n!=N)
{
t->t[n] = new item;
cout <<"输入类型"<<endl;
cin >>type;
t->t[n]->type = type;
cout <<"输入变量名"<<endl;
cin >>vName;
t->t[n]->name = vName;
n++;
}
t->next = NULL;
seq *r;
r = head;
while(r->follow)
r = r->follow;
r->follow = newhead;
r = newhead;
r->follow = NULL;
}
void insert()
{
string tName;
cout <<"输入要添加数据的表名";
cin >>tName;
int n=0;
seq *temp;
temp = head->follow;
while(tName!=temp->name&&temp->follow)
temp = temp->follow;
if(tName==temp->name)
{
N = temp->num;
seq *r,*s;
r = temp;
while(r->next)
r = r->next;
s = new seq;
cout <<"输入数据"<<endl;
string name;
while(n!=N)
{
s->t[n] = new item;
cin >>name;
s->t[n]->name = name;
n++;
}
r->next = s;
r = s;
r->next = NULL;
}
else
cout <<"无此表"<<endl;
}
void del()
{
string tName;
cout <<"要删除的表名"<<endl;
cin >>tName;
seq *r,*s;
r = head->follow;
while(r->name!=tName&&r->follow)
r = r->follow;
if(r->name==tName)
{
int n;s = r->next;int flag;
cout <<"1.删除属性"<<endl<<"2.删除数据"<<endl;
cin >>flag;
if(flag==2)
{
cout<<"要删除第几行数据"<<endl;
cin >>n;
while(n-1)
{
s = s->next;
n--;
}
s->next = s->next->next;
}
else if(flag==1)
{
cout <<"要删除第几个属性 "<<endl;
cin >>n;
s = r->next;
while(s)
{
if(n<r->num)
{ s->t[n-1]->name = s->t[n]->name;
}
s = s->next;
}
r->num--;
}
}
else
cout <<"无此表"<<endl;
}
void modify()
{
string tName;
cout <<"要插入的表名"<<endl;
cin >>tName;
seq *r,*s;
r = head->follow;
while(r->name!=tName&&r->follow)
r = r->follow;
if(r->name==tName)
{
int flag=0;
cout <<"1、插入属性"<<endl<<"2、插入数据"<<endl;
cin >>flag;
if(flag==2)
{
int n;s = r->next;
cout<<"要插入到第几行之后"<<endl;
cin >>n;
while(n-1)
{
s = s->next;
n--;
}
cout <<"输入数据"<<endl;
N = r->num;int t = 0;
seq *newins = new seq;
while(t!=N)
{
newins->t[t] = new item;
cin >>newins->t[t]->name;
t++;
}
newins->next = s->next;
s->next = newins;
}
else if(flag==1)
{
string newName,newType;
s = r->next;
s->t[r->num] = new item;
cout <<"输入属性类型"<<endl;
cin >>newType;
cout <<"输入属性名"<<endl;
cin >>newName;
s->t[r->num]->type = newType;
s->t[r->num]->name = newName;
r->num++;
cout <<"一次输入数据"<<endl;
while(s->next)
{
s = s->next;
s->t[r->num-1] = new item;
cin >>s->t[r->num-1]->name;
}
}
else
cout <<"书入错误"<<endl;
/* string ins;
cout <<"输入要插入的位置(在那个主键之后)"<<endl;
cin >>ins;
while(ins!=r->t[n]->name&&r->next)
temp = temp->next;
seq *newins = new seq;
cout <<"输入数据"<<endl;
string name;
while(n!=N)
{
newins->t[n] = new item;
cin >>name;
newins->t[n]->name = name;
n++;
}
newins->next = temp->next->next;
temp->next = newins;
*/ }
else
cout <<"无此表"<<endl;
}
void charu()
{
string tName;
cout <<"要修改的表名"<<endl;
cin >>tName;
seq *r,*s;
r = head->follow;
while(r->name!=tName&&r->follow)
r = r->follow;
if(r->name==tName)
{
int n,n2;s = r->next;string xiu;
cout<<"要修改到第几行,包括属性行"<<endl;
cin >>n;
cout <<"要修改的第几个数据"<<endl;
cin >>n2;
cout <<"输入要修改后的数据"<<endl;
cin >>xiu;
while(n-1)
{
s = s->next;
n--;
}
s->t[n2-1]->name=xiu;
}
else
cout <<"无此表"<<endl;
}
void fileWrite(ofstream& fw)
{
seq *s,*r;
r = head;
while(r->follow)
{
int n=0;
r = r->follow;
fw <<r->name<<setw(10)<<r->num<<endl;
s = r->next;
while(s)
{
n =0;
while(n!=r->num)
{
fw <<s->t[n]->type<<setw(5)<<s->t[n]->name<<setw(10);
n++;
}
fw <<endl;
s = s->next;
}
}
}
void choose(ofstream& fw,ifstream& fr)
{
string ch;
while(1)
{
showFunction();
cin >>ch;
if(ch == "create")
create();
else if(ch == "insert")
insert();
else if(ch == "delete")
del();
else if(ch == "modify")
modify();
else if(ch == "charu")
charu();
else if(ch == "show")
showTable();
else if(ch == "exit")
{fileWrite(fw); exit(0);}
else
cout <<"正确输入"<<endl;
}
}
void fileRead(ifstream& fr)
{
string parameter;int n=0;
fr >>parameter;
seq *s,*t;
t = new seq;
head->next = t;
t->name = parameter;
fr >>N;
t->num = N;
while(!fr.fail())
{
s = new seq;
s->t[n] = new item;
while(n!=N)
{
fr >>parameter;
s->t[n]->name = parameter;
fr >>parameter;
s->t[n]->type = parameter;
t->next = s;
t = s;
n++;
}
}
t->next = NULL;
}
int main()
{
// system("mkdir E:\\stu");
ifstream fr("output.txt");
head = new seq;
head->follow = NULL;
head->next = NULL;
if(fr.fail())
cout<<"文件不存在"<<endl;
// else
// fileRead(fr);
ofstream fw("output.txt");
choose(fw,fr);
fw.close();
fr.close();
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -