⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbdb.cpp

📁 数据库模拟系统
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	printf("投影操作");
	
	printf("输入关系R的表名:");
	gets(mn);
	//添加错误处理
	for(i=0;i<t_num;i++)
		if(strcmp(mn,t[i].tname)==0)
		{
			//i的值就是表的位置.
			goto eodrp;
		}
	printf("未找到表,请确认输入.\a");
	getchar();
	return;
eodrp:;//找到表所在位置,表t[i]是查到的表.
	int y=0;
	for(p=0;p<MAX_PRONUM;p++)
		for(q=0;q<MAX_PROLEN;q++)
			pro[p][q]='\0';
	k=0;
	char ch;
	while(y!=1)
	{
		printf("\n输入投影的属性:");
		gets(sx);
		//这里添加错误处理,重名或者其他
		strcpy(pro[k++],sx);
		printf("    是否继续输入?[y/n]");
		cin>>ch;
		if(ch=='y')
			continue;
		else
			break;
	}//这里保存了pro[MAX_PRONUM][MAX_PROLEN]
	tnew=yunshadow(&t[i],pro,k);		//传参的k是pro下标
	printf("\n投影结果是:");
	tableprint(&tnew);		//输出表咯~~
	
	printf("\n投影输出结束,回车返回上一级菜单。");
	getchar();
}
void relationsel0()
{
	clrscr();
	char mn[MAX_TNAME];
	char sx[MAX_PROLEN];
	char str[MAX_PROLEN];
	int i,ysf,shu;		//用整型变量保存运算符类别。
	char ch;
	table tnew;		//结果集
	printf("选择单一操作");
	printf("输入关系R的表名:");
	gets(mn);
	//添加错误处理
	for(i=0;i<t_num;i++)
		if(strcmp(mn,t[i].tname)==0)
		{
			//i的值就是表的位置.
			goto eodrp;
		}
	printf("未找到表,请确认输入.\a");
	getchar();
	return;
eodrp:;//找到表所在位置,表t[i]是查到的表.    =、≠、≤、≥、<、>
	printf("请输入选择的属性:");
	gets(sx);
	//添加错误处理

as:	printf("\n请输入关系运算符(1.=  2.≠  3.≤  4.≥  5.<  6.>)  [1-6]:");
	cin>>ch;
	if(ch>48 && ch<55)
		ysf=ch-48;
	else
		goto as;
	printf("\n请输入运算符后的数值:");
	if(ysf>2)
		cin>>shu;	//3.≤  4.≥  5.<  6.>这四种是数值比较
	else
		gets(str);	//1.=  2.≠    这两种是字符串比较
	tnew=yunsel0(&t[i],sx,ysf,shu,str);

	printf("\n选择结果是:");
	tableprint(&tnew);		//输出表咯~~
	printf("\n选择输出结束,回车返回上一级菜单。");
	getchar();
}
void relationseland()
{
	clrscr();
	char mn[MAX_TNAME];
	char sx[MAX_PROLEN];
	char str[MAX_PROLEN];
	char ch;
	int i,shu,ysf;	//用整型变量保存运算符类别。
	table tnew1,tnew2;		//结果集
	printf("选择操作AND");
	printf("输入关系R的表名:");
	gets(mn);
	//添加错误处理
	for(i=0;i<t_num;i++)
		if(strcmp(mn,t[i].tname)==0)
		{
			//i的值就是表的位置.
			goto eodrp;
		}
	printf("未找到表,请确认输入.\a");
	getchar();
	return;
eodrp:;//找到表所在位置,表t[i]是查到的表.    =、≠、≤、≥、<、>
	printf("请输入第一个选择的属性:");
	gets(sx);
	//添加错误处理

as:	printf("\n请输入关系运算符(1.=  2.≠  3.≤  4.≥  5.<  6.>)  [1-6]:");
	cin>>ch;
	if(ch>48 && ch<55)
		ysf=ch-48;
	else
		goto as;
	printf("\n请输入运算符后的数值:");
	if(ysf>2)
		cin>>shu;	//3.≤  4.≥  5.<  6.>这四种是数值比较
	else
		gets(str);	//1.=  2.≠    这两种是字符串比较
	tnew1=yunsel0(&t[i],sx,ysf,shu,str);
//****************************************************************
	printf("\n请输入第二个选择的属性:");
	gets(sx);
	//添加错误处理

as1:	printf("\n请输入关系运算符(1.=  2.≠  3.≤  4.≥  5.<  6.>)  [1-6]:");
	cin>>ch;
	if(ch>48 && ch<55)
		ysf=ch-48;
	else
		goto as1;
	printf("\n请输入运算符后的数值:");
	if(ysf>2)
		cin>>shu;	//3.≤  4.≥  5.<  6.>这四种是数值比较
	else
		gets(str);	//1.=  2.≠    这两种是字符串比较
	tnew2=yunsel0(&t[i],sx,ysf,shu,str);
//***************************************************************
	tnew1=yunjiao(&tnew1,&tnew2);
	printf("\n选择AND结果是:");
	tableprint(&tnew1);		//输出表咯~~
	printf("\n选择输出结束,回车返回上一级菜单。");
	getchar();	
}
void relationselor()
{
	clrscr();
	char mn[MAX_TNAME];
	char sx[MAX_PROLEN];
	char str[MAX_PROLEN];
	char ch;
	int i,shu,ysf;	//用整型变量保存运算符类别。
	table tnew1,tnew2;		//结果集
	
	printf("选择操作OR");
	printf("输入关系R的表名:");
	gets(mn);
	//添加错误处理
	for(i=0;i<t_num;i++)
		if(strcmp(mn,t[i].tname)==0)
		{
			//i的值就是表的位置.
			goto eodrp;
		}
	printf("未找到表,请确认输入.\a");
	getchar();
	return;
eodrp:;//找到表所在位置,表t[i]是查到的表.    =、≠、≤、≥、<、>
	printf("请输入第一个选择的属性:");
	gets(sx);
	//添加错误处理

as:	printf("\n请输入关系运算符(1.=  2.≠  3.≤  4.≥  5.<  6.>)  [1-6]:");
	cin>>ch;
	if(ch>48 && ch<55)
		ysf=ch-48;
	else
		goto as;
	printf("\n请输入运算符后的数值:");
	if(ysf>2)
		cin>>shu;	//3.≤  4.≥  5.<  6.>这四种是数值比较
	else
		gets(str);	//1.=  2.≠    这两种是字符串比较
	tnew1=yunsel0(&t[i],sx,ysf,shu,str);
//****************************************************************
	printf("\n请输入第二个选择的属性:");
	gets(sx);
	//添加错误处理

as1:	printf("\n请输入关系运算符(1.=  2.≠  3.≤  4.≥  5.<  6.>)  [1-6]:");
	cin>>ch;
	if(ch>48 && ch<55)
		ysf=ch-48;
	else
		goto as1;
	printf("\n请输入运算符后的数值:");
	if(ysf>2)
		cin>>shu;	//3.≤  4.≥  5.<  6.>这四种是数值比较
	else
		gets(str);	//1.=  2.≠    这两种是字符串比较
	tnew2=yunsel0(&t[i],sx,ysf,shu,str);
//***************************************************************
	tnew1=yununion(&tnew1,&tnew2);
	printf("\n选择AND结果是:");
	tableprint(&tnew1);		//输出表咯~~
	printf("\n选择输出结束,回车返回上一级菜单。");
	getchar();	
}
void relationselwhere()
{
		clrscr();
	char mn[MAX_TNAME];
	char sx[MAX_PROLEN],sx1[MAX_PROLEN];
	char str[MAX_PROLEN];
	char pro[MAX_PRONUM][MAX_PROLEN];
	int i,ysf,shu;		//用整型变量保存运算符类别。
	int q,p,k;
	char ch;
	table tnew;		//结果集
	printf("投影选择综合操作");
	 printf("输入关系R的表名:");
	gets(mn);
	//添加错误处理
	for(i=0;i<t_num;i++)
		if(strcmp(mn,t[i].tname)==0)
		{
			//i的值就是表的位置.
			goto eodrp;
		}
	printf("未找到表,请确认输入.\a");
	getchar();
	return;
eodrp:;//找到表所在位置,表t[i]是查到的表.    =、≠、≤、≥、<、>
	printf("请输入选择的属性:");
	gets(sx);
	//添加错误处理
	strcpy(sx1,sx);
as:	printf("\n请输入关系运算符(1.=  2.≠  3.≤  4.≥  5.<  6.>)  [1-6]:");
	cin>>ch;
	if(ch>48 && ch<55)
		ysf=ch-48;
	else
		goto as;
	printf("\n请输入运算符后的数值:");
	if(ysf>2)
		cin>>shu;	//3.≤  4.≥  5.<  6.>这四种是数值比较
	else
		gets(str);	//1.=  2.≠    这两种是字符串比较
	tnew=yunsel0(&t[i],sx,ysf,shu,str);		//选择结束,即将投影。
//*********************************************************************
	int y=0;
	for(p=0;p<MAX_PRONUM;p++)
		for(q=0;q<MAX_PROLEN;q++)
			pro[p][q]='\0';
	k=0;
	while(y!=1)
	{
		printf("\n输入投影的属性:");
		gets(sx);
		//这里添加错误处理,重名或者其他
		strcpy(pro[k++],sx);
		printf("    是否继续输入?[y/n]");
		cin>>ch;
		if(ch=='y')
			continue;
		else
			break;
	}//这里保存了pro[MAX_PRONUM][MAX_PROLEN]
	tnew=yunshadow(&tnew,pro,k);		//传参的k是pro下标
	printf("\n查询的结果是:select %s from %s where %sθ",sx1,t[i].tname,sx1);
	tableprint(&tnew);		//输出表咯~~
	printf("\n选择输出结束,回车返回上一级菜单。");
	getchar();
}
void relationconnect()
{
	table relationequalconnect(table *t1,table *t2,int x1,int x2);
	table relationnaturalconnect(table *t1,table *t2);
	//这里包含两种连接,将这两种连接的结果都要输出出来。
ab:	clrscr();
	char mn[MAX_TNAME];
	table tnew;	//结果集
	int i,j,x1,x2;
	printf("连接操作");
	printf("选择连接类型: 1.等值 2.自然[ ]\b\b");
	char ch;
	cin>>ch;
	if(ch=='1')
		goto ab1;
	else if(ch=='2')
		goto ab2;
	else 
		goto ab;
//**********以下处理等值连接和自然连接*************************************************
//**********首先是等值连接*************************************************
ab1:printf("输入关系R的表名:");
	gets(mn);
	//添加错误处理
	for(i=0;i<t_num;i++)
		if(strcmp(mn,t[i].tname)==0)
		{
			//i的值就是表的位置.
			goto eodrp;
		}
	printf("未找到表,请确认输入.\a");
	getchar();
	return;
eodrp:;//找到表所在位置,表t[i]是查到的表.
	printf("\n输入关系R中的一个属性用做等值连接:");
	gets(mn);
	for(x1=0;x1<t[i].pro_num;x1++)
		if(strcmp(t[i].proname[x1],mn)==0)
			break;
	//这里不添加错误处理的理由是减少goto语句的使用,但是在结构化程序设计中,
	//GOTO语句是一个很好用的东东!!!......
	printf("\n输入关系S的表名:");
	gets(mn);
	//添加错误处理
	for(j=0;j<t_num;j++)
		if(strcmp(mn,t[j].tname)==0)		//j的值就是表的位置.
			goto eodrp1;
	printf("未找到表,请确认输入.\a");
	getchar();
	return;
eodrp1:;//找到表所在位置,表t[j]是查到的表.
	   	printf("\n输入关系R中的一个属性用做等值连接:");
	gets(mn);
	for(x2=0;x2<t[j].pro_num;x2++)
		if(strcmp(t[j].proname[x2],mn)==0)
			break;
	//表S与表R。。。。。表R i  表S j
	tnew=relationequalconnect(&t[i],&t[j],x1,x2);
	goto enda;
//*********再次是自然连接*******************************************
//***********************************容易乱*************************
ab2: printf("输入关系R的表名:");
	gets(mn);
	//添加错误处理
	for(i=0;i<t_num;i++)
		if(strcmp(mn,t[i].tname)==0)
		{
			//i的值就是表的位置.
			goto eodrp2;
		}
	printf("未找到表,请确认输入.\a");
	getchar();
	return;
eodrp2:;//找到表所在位置,表t[i]是查到的表.
	printf("\n输入关系S的表名:");
	gets(mn);
	//添加错误处理
	for(j=0;j<t_num;j++)
		if(strcmp(mn,t[j].tname)==0)		//j的值就是表的位置.
			goto eodrp3;
	printf("未找到表,请确认输入.\a");
	getchar();
	return;
eodrp3:;//找到表所在位置,表t[j]是查到的表.
	//表S与表R。。。。。表R i  表S j 
	tnew=relationnaturalconnect(&t[i],&t[j]);
	goto enda;


enda:tableprint(&tnew);
	printf("\n连接操作成功,回车返回上一级菜单.");
	getchar(); 
}
table relationequalconnect(table *t1,table *t2,int x1,int x2)
{
	int m,n,i,j;
	table tnew;
	tnew.pro_num=t1->pro_num+t2->pro_num;
	strcpy(tnew.tname,"T");
	tnew.re_num=0;
	tnew.key_no=0;
	for(m=0;m<t1->pro_num;m++)
		strcpy(tnew.proname[m],t1->proname[m]);
	n=m;
	for(m=0;m<t2->pro_num;m++)
		strcpy(tnew.proname[m+n],t2->proname[m]);
//以上初始化结果集tnew
	for(i=0;i<t1->re_num;i++)
		for(j=0;j<t2->re_num;j++)
		{
			if(strcmp(t1->record[i][x1],t2->record[j][x2])==0)	//等值
			{
				for(m=0;m<t1->pro_num;m++)
					strcpy(tnew.record[tnew.re_num][m],t1->record[i][m]);
				n=m;
				for(m=0;m<t2->pro_num;m++)
					strcpy(tnew.record[tnew.re_num][m+n],t2->record[j][m]);
				tnew.re_num++;
			}
		}
	return tnew;
}
int prosubscript(table *t1,char *str)		//返回一个属性在一个表中的位置(下标)
{
	int i;
	for(i=0;i<t1->pro_num;i++)
		if(strcmp(str,t1->proname[i])==0)
			return i;
	return -1;
}
table relationnaturalconnect(table *t1,table *t2)
{
	table tnew;
	char prosame[MAX_PRONUM][MAX_PROLEN];
	int i,j,m,n,k,pronum=0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -