求助(代码短)
查看原帖
求助(代码短)
60864
tiger2005楼主2020/7/18 18:09

只有读入,而且只读入命令。

根据判断,是WHERE语句出了问题。(getWhereCmd())

代码中addadd_num表示下一组数据的第一个数有没有被读到一半,以及读完的数

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<string>
#include<iostream>
#include<cctype>
#include<cstdlib>
using namespace std;
int T;
int N;
vector<string> Command;
bool add;
int add_num;
bool isNum(string str){
	int u;
	while(str[u]=='+' || str[u]=='-')	++u;
	if(u==str.size())	return false;
	while(isdigit(str[u]))	++u;
	return u==str.size();
}
int getNum(string str,int st){
	int num=0;int flag=1;
	while(str[st]=='+' || str[st]=='-'){
		flag*=(str[st]=='+'?1:-1);
		++st;
	}
	while(isdigit(str[st])){
		num=num*10+(str[st]-'0');
		++st;
	}
	return num*flag;
}
char ch;
bool isWs(char c){
	return (c==' ' || c=='\r' || c=='\n'
			 || c=='\t' || c=='\0' || c==EOF);
}
char getNxtChr(bool wsAllow){
	while(cin >> noskipws >> ch){
		if(wsAllow)	return ch;
		if(!isWs(ch))	return ch; 
	}
	return ch=EOF;
}
void getFromCmd(){
	getNxtChr(false);
	if(ch=='('){
		Command.push_back("(");
		getFromCmd();
		Command.push_back(")");
		getNxtChr(false);
	}
	else{
		string nam="";
		while(isalnum(ch)){
			nam+=ch;
			getNxtChr(true);
		}
		Command.push_back(nam);
		getNxtChr(false);
	}
	if(ch=='I'){//INNER JOIN
		for(int i=0;i<9;i++)	getNxtChr(false);
		Command.push_back("INNER");
		Command.push_back("JOIN");
		string nam="";
		while(isalnum(ch)){
			nam+=ch;
			getNxtChr(true);
		}
		Command.push_back(nam);
		getNxtChr(false);
		for(int i=0;i<2;i++)	getNxtChr(false);
		Command.push_back("ON");
		nam="";
		while(isalnum(ch)){
			nam+=ch;
			getNxtChr(true);
		}
		Command.push_back(nam);
		if(isWs(ch))	getNxtChr(false);
		Command.push_back("=");
		getNxtChr(false);
		nam="";
		while(isalnum(ch)){
			nam+=ch;
			getNxtChr(true);
		}
		Command.push_back(nam);
		getNxtChr(false);
	}
}
void getOrderCmd(){
	getNxtChr(false);
	while(!(ch=='\0' || ch==EOF || isdigit(ch))){
		if(ch==','){
			getNxtChr(false);
		}
		else if(isalnum(ch)){
			string nam="";
			while(isalnum(ch)){
				nam+=ch;
				getNxtChr(true);
			}
			Command.push_back(nam);
			if(isWs(ch))	getNxtChr(false);
		}
	}
	if(isdigit(ch)){
		add=true;
		add_num=ch-'0';
	}
	return;
}
void getWhereCmd(){
	string nam="";
	getNxtChr(false);
	if(ch=='('){
		Command.push_back("(");
		getWhereCmd();
		Command.push_back(")");
		getNxtChr(false);
	}
	else{
		if(ch=='\"'){
			Command.push_back("\"");
			getNxtChr(true);
			while(ch!='\"'){
				nam+=ch;
				if(ch=='\\'){
					getNxtChr(true);
					nam+=ch;
				}
				getNxtChr(true);
			}
			Command.push_back(nam);
			Command.push_back("\"");
			getNxtChr(false);
		}
		else if(isalnum(ch)){
			while(isalnum(ch) || ch=='+' || ch=='-'){
				nam+=ch;
				getNxtChr(true);
			}
			Command.push_back(nam);
			if(nam=="NOT"){
				getWhereCmd();
				return;
			}
			if(isWs(ch))	getNxtChr(false);
		}
		nam="";
		while(ch=='=' || ch=='>' || ch=='<'){
			nam+=ch;getNxtChr(false);
		}
		Command.push_back(nam);
		nam="";
		if(ch=='\"'){
			Command.push_back("\"");
			getNxtChr(true);
			while(ch!='\"'){
				nam+=ch;
				if(ch=='\\'){
					getNxtChr(true);
					nam+=ch;
				}
				getNxtChr(true);
			}
			Command.push_back(nam);
			Command.push_back("\"");
			getNxtChr(false);
		}
		else{
			while(isalnum(ch) || ch=='+' || ch=='-'){
				nam+=ch;
				getNxtChr(true);
			}
			Command.push_back(nam);
			if(isWs(ch))	getNxtChr(false);
		}
	}
	if(ch==')')	return;
	nam="";
	while(isalnum(ch)){
		nam+=ch;
		getNxtChr(true);
	}
	if(nam=="ORDER"){
		for(int i=0;i<2;i++)	getNxtChr(false);
		Command.push_back("ORDER");
		Command.push_back("BY");
		getOrderCmd();
		return;
	}
	if(nam=="AND" || nam=="OR"){
		Command.push_back(nam);
		getWhereCmd();
		return;
	}
	if(isNum(nam)){
		add=true;
		add_num=getNum(nam,0);
		return;
	}
	if(nam=="")	return;
	return;
}
int main(){
	cin >> T;
	while(T--){
		Command.clear();
		if(!add)	cin >> skipws >> N;
		else{
			N=add_num;
			char c;
			cin >> noskipws >> c;
			while(c>='0' && c<='9'){
				N=N*10+c-'0';
				cin >> noskipws >> c;
			}
			add=false;
		}
		//SELECT
		for(int i=0;i<7;i++)	getNxtChr(false);
		Command.push_back("SELECT");
		if(ch=='*'){
			Command.push_back("*");
			getNxtChr(false);
		}
		else{
			string nam;
			while(1){
				nam="";
				while(isalnum(ch)){
					nam+=ch;
					getNxtChr(true);
				}
				while(isWs(ch))	getNxtChr(true);
				Command.push_back(nam);
				if(ch!=',')	break;
				getNxtChr(false);
			}
		}
		//ROM
		for(int i=0;i<3;i++)	getNxtChr(false);
		Command.push_back("FROM");
		getFromCmd();
		if(isdigit(ch)){
			add=true;
			add_num=ch-'0';
		}
		else if(ch=='W'){
			//HERE
			for(int i=0;i<4;i++)	getNxtChr(false);
			Command.push_back("WHERE");
			getWhereCmd();
		}
		else if(ch=='O'){
			for(int i=0;i<6;i++)	getNxtChr(false);
			Command.push_back("ORDER");
			Command.push_back("BY");
			getOrderCmd();
		}
	}
	return 0;
}
2020/7/18 18:09
加载中...