【C实现简易内存数据库】1、命令的接收解析分发

项目要求:

实现一个基于命令行的简单数据库系统,要求能建立数据库表,能对单表进行数据导入,插入,查询,更新,删除。

支持命令:

a) -c 文件路径名 根据指定位置的文件内语句建立一个数据库表

b) -i 数据库表名 数据文件路径名 从指定文本文件中导入数据到数据库的表中

c) -s 文件路径名 从指定文本文件中读取语句执行查询操作

d) -u 文件路径名 从指定文本文件中读取语句执行更新操作

e) -d 文件路径名 从指定文本文件中读取语句执行删除操作

f) -h 帮助信息,提示各个参数的格式和含义

g) -q 退出程序

h) -index 0/1 0 表示关闭索引功能,1 表示打开索引功能,即在接下来的查询、更新、删除操作中使用索引。

具体文件格式及项目要求参见附件:http://pan.baidu.com/s/129dqm


命令接收

我们先不管背后复杂的数据结构,先来实现一个简单的命令接受解析器,来开始我们项目的第一步。
最简单的命令接收器:
#include <stdio.h>

int main(){
	char input[256];
	while(true){
		printf("DB=>");
		gets (input);
		printf("%s\n",input);
	}
	return 0;
}
我们用一个char数组input来存储用户的指令。
main函数中while(true)表示该循环将一直进行到用户手工退出为止。
每一次循环中,我们先打印“DB=>”,然后等待用户输入,当用户输入指令并敲击回车时,gets(input)函数会将用户的输入存储到input变量中。
接受到命令以后,我们简单的将命令本身打印出来,至此一次循环就结束了,进入下一次循环:打印“DB=>”,等待命令输入,处理命令,下一次循环。。。
这样一个最简单的命令接收器就完成了。

命令解析

我们希望程序根据接收到的不同命令执行不同的操作,那么我们就需要对命令进行解析,从而根据解析的结果做不同的处理。
#include <stdio.h>
#include <string.h>

int parseInput(char*);

int main(){
	char input[256];
	while(true){
		printf("DB=>");
		gets (input);
		//解析命令
		parseInput(input);
	}
	return 0;
}
int parseInput(char* input){
	char command[10];
	char param[256];
	//清空数组
	memset(&command, 0, sizeof(command));
	memset(&param, 0, sizeof(param));

	int commandIndex = 0;
	int paramIndex = 0;

	//输入的第一个字符应为‘-’,否则返回-1
	if(input[0]!='-') return -1;
	//获取command
	for(int i=1;input[i]!=' ' && input[i]!='\0';i++){
		command[commandIndex] = input[i];
		commandIndex++;
	}
	//获取param
	for(int i = commandIndex+2;input[i]!='\0';i++){
		param[paramIndex] = input[i];
		paramIndex ++;
	}
	printf(command);
	printf(param);
	return 0;
}
这里我们写了一个parseInpute函数来对命令进行解析,将用户输入分成了command和param两部分。command即-c -s等,param即文件路径等。

命令分发

得到command以后,我们就可以方便地根据不同的command来做不同的操作。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int parseInput(char*);
int distribute(char*, char*);

int main(){
	char input[256];
	while(true){
		printf("DB=>");
		gets (input);
		//解析命令
		parseInput(input);
	}
	return 0;
}
int parseInput(char* input){
	char command[10];
	char param[256];
	//清空数组
	memset(&command, 0, sizeof(command));
	memset(&param, 0, sizeof(param));

	int commandIndex = 0;
	int paramIndex = 0;

	//输入的第一个字符应为‘-’,否则返回-1
	if(input[0]!='-') return -1;
	//获取command
	for(int i=1;input[i]!=' ' && input[i]!='\0';i++){
		command[commandIndex] = input[i];
		commandIndex++;
	}
	//获取param
	for(int i = commandIndex+2;input[i]!='\0';i++){
		param[paramIndex] = input[i];
		paramIndex ++;
	}
	//分发命令
	distribute(command,param);
	return 0;
}
int distribute(char* command,char* param){
	if(!strcmp(command,"c"))
		printf("建表%s\n",param);
	else if(!strcmp(command,"i"))
		printf("导入数据%s\n",param);
	else if(!strcmp(command,"u"))
		printf("更新%s\n",param);
	else if(!strcmp(command,"d"))
		printf("删除%s\n",param);
	else if(!strcmp(command,"h"))
		 printf("帮助\n");
	else if(!strcmp(command,"q")){
		printf("退出");
		exit(0);
	}
	else if(!strcmp(command,"index")) 
		printf("索引%s\n",param);
	else
		printf("Bad Command! Try again please.\n");
	return 0;
}
这里我们用distribute(command,param)替换了parseInput函数中简单的打印command和param。
在distribute函数中,我们根据不同的command做不同的操作,这里只是简单打印了命令的含义以及相应的param,效果如下:

需要注意的是strcmp函数的返回值:
  • strcmp(s1,s2)
    • 当s1<s2时,返回为负数 注意不是-1
    • 当s1==s2时,返回值= 0
    • 当s1>s2时,返回正数 注意不是1
所以strcmp(command,“c”)将command与“c”作比较,若相等则返回0。
但0对应false,所以if(strcmp(command,"c"))实际上是当command不等于“c”时执行,取反后if(!strcmp(command,"c"))才是我们想要的当command等于“c”时执行。
PS: 不让用string.h就自己写一个strcmp吧= =
另外exit(0)需要引入stdlib.h头文件。

后续可以参考老师的博客: http://orangegao-gmail-com.iteye.com/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值