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

📄 display.cpp

📁 数据库模拟程序 实现控制台命令词法分析
💻 CPP
字号:
// display.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "display.h"
#include "DBControl.h"
#include <iomanip.h>
#include <conio.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object


void OnReadCidian();
void OnReadTable(char filaname[]);
void out(char filename[]);
void read(char filename[]);


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		CString strHello;
		strHello.LoadString(IDS_HELLO);
		cout << (LPCTSTR)strHello << endl;
	}

//	OnReadCidian();
	char filename[20];

	cout<<"1=退出 2=win32模式——查看数据表 3=查看数据字典文件 4=console模式——查看数据表"<<endl;
	cout<<"默认为查看数据表"<<endl;
	int flag = 4;
	while(1)
	{
		memset(filename, 0, 20);
		if(argc < 2)
		{
			cout<<"SQL>";
			cin>>filename;
			if(filename[0] == '1'){
				break;
			}
			if(filename[0] == '4'){
				flag = 4;continue;
			}
			else if(filename[0] == '2'){
				flag = 2;continue;
			}
			else if(filename[0] == '3'){
				flag = 3;//continue;
			}
		}
		else
			strcpy(filename, argv[1]);
		
		switch(flag)
		{
		case 2:
			{
				strcat(filename, ".tab");
				OnReadTable(filename);
				if(argc < 2)
					break;
				else
				{
					getch();
					return 0;
				}
			}
		case 3:
			{
				strcpy(filename, "CiDian.txt");
				out(filename);
				if(argc < 2)
					break;
				else
				{
					getch();
					return 0;
				}
			}
		case 4:
			{
				strcat(filename, ".tab");
				read(filename);
				if(argc < 2)
					break;
				else
				{
					getch();
					return 0;
				}
			}
		}		
	}
	return nRetCode;
}


void OnReadTable(char filename[]) 
{
	// TODO: Add your command handler code here
	ifstream file;
	
	file.open(filename, ios::nocreate);
	Data data;
	memset(&data, 0, sizeof(Data));
	char countBuff[10];
	char buff[100];
	memset(buff, 0, 100);
	memset(countBuff, 0, 10);
	file.read(countBuff, 10);
	file.read(buff, 1);
//	file.read(data.name, NAME_LEN);
	char ch[2];
	memset(ch, 0, 2);
	int count = atoi(countBuff);

	char a[10][ITEM_LEN + 1];//此语句限定程序只能处理最大10个属性
	memset(a, 0, 10 * ITEM_LEN + 1);
	//char (*a)[count] = new char[NAME_LEN][count];
	
	int i;
	CString str;
	int flag = 0;
	cout<<endl<<endl;
	while(file.read(buff, 1))
	{
		str.Empty();
		file.seekg(-1, ios::cur);
		int offset = file.tellg();
		file.read(data.name, NAME_LEN);
		if(flag == 0 && data.name[0] != 0)
		{
			flag = 1;
			cout<<"读取表:"<<data.name<<endl<<endl;
		}
		offset = file.tellg();

		str.Format("表名:%s, 属性数据: ", data.name);
		i = 0;
		for(i = 0; i < count; i++)
		{
			file.getline(a[i], ITEM_LEN + 2, ';');//数据项最大ITEM_LEN但为了能读到‘;’所以加长一个单位,加长一个结束符。
			offset = file.tellg();
			str += a[i];
			str += "; ";
		}
		
		if(data.name[0] != 0)//如果是空数据则不显示。
		{
//			cout<<str.GetBuffer(str.GetLength())<<endl;
			AfxMessageBox(str);
		}

		file.read(buff, 1);//读取回车
	}
	file.close();
}
void out(char filename[])
{
	CDBControl db;
	int i = 0, j = 0;
	Data data;
	memset(&data, 0, sizeof(Data));
	
	CString str, temp;
	int flag = 0;
	fstream fff;
	str = filename;
	int off = str.Find(".", 0);
	str = str.Left(off);

	cout<<endl;
	cout<<"-----------------------------------------------------------------------"<<endl;
	
	i = 0;
	while(db.ReadFromCiDian(i, data))
	{	
//		str.Format("name = %s,", data.name);
		cout<<setw(10)<<data.name;
		for(j = 0; j < data.pDataArray_len; j++)
		{
			cout<<setw(10)<<data.pDataArray[j].name;
			switch(data.pDataArray[j].type)
			{
			case 0:temp = "INTEGER";break;//int
			case 2:temp.Format("CHAR(%d)", data.pDataArray[j].len);break;//char
			}
//			str += " 类型 ";
//			str += temp;
//			str += ");";
			cout<<setw(10)<<temp.GetBuffer(temp.GetLength());
			temp.Empty();
		}
		//AfxMessageBox(str);
		//cout<<str.GetBuffer(str.GetLength())<<endl;
		cout<<endl;
		str.Empty();
		
		delete [] data.pDataArray;
		memset(&data, 0, sizeof(Data));
		i++;
	}
	cout<<"-----------------------------------------------------------------------"<<endl;
}
void OnReadCidian() 
{
	// TODO: Add your command handler code here
	CDBControl db;
	int i = 0, j = 0;
	Data data;
	memset(&data, 0, sizeof(Data));
	
	CString str, temp;
	int flag = 0;
	cout<<"-----------------------------------------------------------------"<<endl;
	while(db.ReadFromCiDian(i, data))
	{	
		str.Format("name = %s,", data.name);
		for(j = 0; j < data.pDataArray_len; j++)
		{
			str += "( 属性 ";
			str += data.pDataArray[j].name;
			switch(data.pDataArray[j].type)
			{
			case 0:temp = "INTEGER";break;//int
			case 2:temp.Format("CHAR(%d)", data.pDataArray[j].len);break;//char
			}
			str += " 类型 ";
			str += temp;
			str += ");";
			temp.Empty();
		}
		//AfxMessageBox(str);
		cout<<str.GetBuffer(str.GetLength())<<endl;
		str.Empty();
		
		delete [] data.pDataArray;
		memset(&data, 0, sizeof(Data));
		i++;
	}
	cout<<"-----------------------------------------------------------------"<<endl;
}

void read(char filename[])
{
	ifstream file;
	file.open(filename, ios::nocreate);
	Data data;
	memset(&data, 0, sizeof(Data));
	char countBuff[10];
	char buff[100];
	memset(buff, 0, 100);
	memset(countBuff, 0, 10);
	file.read(countBuff, 10);
	file.read(buff, 1);

//	file.read(data.name, NAME_LEN);
	char ch[2];
	memset(ch, 0, 2);
	int count = atoi(countBuff);

	char a[10][ITEM_LEN + 1];//此语句限定程序只能处理最大10个属性
	memset(a, 0, 10 * ITEM_LEN + 1);
	//char (*a)[count] = new char[NAME_LEN][count];
	
	int i;
	CString str = filename;
	int off = str.Find(".", 0);
	str = str.Left(off);

	IndexForCiDian index;
	memset(&index, 0, sizeof(IndexForCiDian));

	index.indexType = TABLE;
	strcpy(index.name, str.GetBuffer(str.GetLength()));

	CDBControl db;
	if(db.findInIndexFile(index) == -1){
		AfxMessageBox("Error! 没找到索引!");
		return;
	}

	cout<<setw(10)<<"表";
	for(i = 0; i < (index.item_len - NAME_LEN)/ sizeof(DataItem); i++){
		cout<<setw(10)<<"属性";
	}
	cout<<endl;
	cout<<"-----------------------------------------------------------------------"<<endl;

	str.Empty();
	while(file.read(buff, 1))
	{
		str.Empty();
		file.seekg(-1, ios::cur);
		int offset = file.tellg();
		file.read(data.name, NAME_LEN);
		offset = file.tellg();

		cout<<setw(10)<<data.name;
//		str.Format("表名:%s, 属性数据: ", data.name);
		i = 0;
		for(i = 0; i < count; i++)
		{
			file.getline(a[i], ITEM_LEN + 2, ';');//数据项最大ITEM_LEN但为了能读到‘;’所以加长一个单位,加长一个结束符。
			offset = file.tellg();			
			//str += a[i];
			//str += "; ";
		}
		
		if(data.name[0] != 0)//如果是空数据则不显示。
		{
			//AfxMessageBox(str);
			for(i = 0; i < count; i++)
				cout<<setw(10)<<a[i];
			cout<<endl;
		}

		file.read(buff, 1);//读取回车
	}
	file.close();
}

⌨️ 快捷键说明

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