对select 查询内容 from
代码
#include <iostream>
#include <string>
#include <fstream>
#include <set>
#include <sstream>
#include<vector>
using namespace std;
bool isDelimiter(char c) {
if (c == ' ' || c == ',')
{
return true;
}
else {
return false;
}
}
vector<string> splitString(const string& str)
{
vector<string>tokens;
string token;
for (char c : str) { //挨个从str中取,判断是否是分隔符
if (isDelimiter(c) == 1) {
if (!token.empty()) {
tokens.push_back(token);
token.clear();
}
}
else {
token += c;
}
}
if (!token.empty()) {
tokens.push_back(token);
}
return tokens;
}
struct Student {
string studentID;
string name;
char gender;
int score;
Student(string ID, string n, char g, int s) :studentID(ID), name(n), gender(g), score(s) {}
bool operator<(const Student& other) const {
return studentID < other.studentID;
}
};
set<Student> readData(const string& filename) {
//按行读取infile文件
string line;
ifstream infile(filename);
set<Student> students;
if (!infile.is_open()) { //文件不能打开
cerr << "infile open err" << endl;
}
while (getline(infile, line)) //只要有数据
{
istringstream iss(line);
string studentID, name;
char gender;
int score;
if (iss >> studentID >> name >> gender >> score) {
students.insert(Student(studentID, name, gender, score)); //读取
}
}
infile.close();
return students;
}
void printFindColumns(set<Student>& studentSet, vector<string>& findColumns)
{
for (const auto& student : studentSet) {
for (const auto& coulumn : findColumns) {
if (coulumn == "id") {
cout << student.studentID << " ";
}
else if (coulumn=="name") {
cout <<student.name << " ";
}
else if (coulumn == "score") {
cout << student.score << " ";
}
else if (coulumn == "gender") {
cout << student.gender << " ";
}
else {
cout << "属性不存在" << endl;
}
}
cout << endl;
}
}
int main()
{
set<Student>studentSet;
string filename = "input.txt";
studentSet = readData(filename); //读取文件存储
string query;
cout << "please input SQL:"<<endl;
getline(cin, query);
string selectword = "select";
string fromword = "from";
int select_pos = query.find(selectword);
int from_pos = query.find(fromword);
//扣出select和from 中间的字符串
string columnNames;
columnNames = query.substr(select_pos + size(selectword), from_pos - (select_pos + size(selectword)));
vector<string> findColumns = splitString(columnNames);
for (const auto& token : findColumns) {
cout << token << " ";
}
cout << endl;
printFindColumns(studentSet, findColumns);
return 1;
}
现在要在这个基础上后面加一个where查询条件该怎么改一下
#include <iostream>
#include <string>
#include <fstream>
#include <set>
#include <sstream>
#include<vector>
using namespace std;
bool isDelimiter(char c) {
if (c == ' ' || c == ',')
{
return true;
}
else {
return false;
}
}
vector<string> splitString(const string& str)
{
vector<string>tokens;
string token;
for (char c : str) { //挨个从str中取,判断是否是分隔符
if (isDelimiter(c) == 1) {
if (!token.empty()) {
tokens.push_back(token);
token.clear();
}
}
else {
token += c;
}
}
if (!token.empty()) {
tokens.push_back(token);
}
return tokens;
}
struct Student {
string studentID;
string name;
char gender;
int score;
Student(string ID, string n, char g, int s) :studentID(ID), name(n), gender(g), score(s) {}
bool operator<(const Student& other) const {
return studentID < other.studentID;
}
};
set<Student> readData(const string& filename) {
//按行读取infile文件
string line;
ifstream infile(filename);
set<Student> students;
if (!infile.is_open()) { //文件不能打开
cerr << "infile open err" << endl;
}
while (getline(infile, line)) //只要有数据
{
istringstream iss(line);
string studentID, name;
char gender;
int score;
if (iss >> studentID >> name >> gender >> score) {
students.insert(Student(studentID, name, gender, score)); //读取
}
}
infile.close();
return students;
}
void printFindColumns(set<Student>& studentSet, vector<string>& findColumns)
{
for (const auto& student : studentSet) {
for (const auto& coulumn : findColumns) {
if (coulumn == "id") {
cout << student.studentID << " ";
}
else if (coulumn == "name") {
cout << student.name << " ";
}
else if (coulumn == "score") {
cout << student.score << " ";
}
else if (coulumn == "gender") {
cout << student.gender << " ";
}
else {
cout << "属性不存在" << endl;
}
}
cout << endl;
}
}
void printFindnames(set<Student>& studentSet, vector<string>& findColumns, vector<string>& findnames)
{
for (const auto& student : studentSet) {
cout << endl;
}
}
int main()
{
set<Student>studentSet;
string filename = "input.txt";
studentSet = readData(filename); //读取文件存储
string query;
cout << "please input SQL:" << endl;
getline(cin, query);
string selectword = "select";
string fromword = "from";
string whereword = "where";
int select_pos = query.find(selectword); //select 首字母在的位置
int from_pos = query.find(fromword); //from 首字母在的位置
int where_pos = query.find(whereword); //where的位置
//扣出select和from 中间的字符串
string columnNames;
columnNames = query.substr(select_pos + size(selectword), from_pos - (select_pos + size(selectword)));
//扣出where后面的字符串
string wherenames;
wherenames = query.substr(where_pos + size(whereword), size(query) - (where_pos + size(whereword)));
cout << wherenames << endl;
//去除掉多余的空格和逗号
vector<string> findColumns = splitString(columnNames);
vector<string> findnames = splitString(wherenames);
for (const auto& token : findColumns) {
cout << token << " ";
}
for (const auto& token : findnames) {
cout << token << " ";
}
cout << endl;
printFindnames(studentSet, findColumns,findnames);
return 1;
}
应该是在这个函数中改
void printFindnames(set<Student>& studentSet, vector<string>& findColumns, vector<string>& findnames)
{
for (const auto& student : studentSet) {
cout << endl;
}
}
有奖金的员工信息,并且工资较高的前10名显示出来
select * from employees
where commission_pct is not null
order by salary desc
limit 0,10;