3699

来源:互联网 发布:网络信贷业务员好做吗 编辑:程序博客网 时间:2024/05/22 04:24
#include <iostream>#include <vector>#include <string>#include <cstdlib>#include <algorithm>using namespace std;struct column_name_type{bool type;//int: truestring colname;};struct pstr_int{int num;string str;};column_name_type col[10], *cname[10];pstr_int chunk[10][10000], *content_sort[10][10000];vector<int> row, co;int m, n, k;bool column_name_judge(const column_name_type *const left, const column_name_type *const right){return left ->colname < right ->colname;}bool content_judge_true(const pstr_int *const left, const pstr_int *const right){return left ->num < right ->num;}bool content_judge_false(const pstr_int *const left, const pstr_int *const right){return left ->str < right ->str;}void init(){string tmp;cin >> m >> n >> k;for(int i = 0; i < m; ++i){cin >> col[i].colname >> tmp;col[i].type = tmp.compare("INT")? false: true;}for(int i = 0; i < n; ++i){for(int j = 0; j < m; ++j){cin >> chunk[j][i].str;if(col[j].type){chunk[j][i].num = atoi(chunk[j][i].str.c_str());}}}for(int i = 0; i < m; cname[i] = &col[i], ++i);sort(cname, cname + m, column_name_judge);for(int i = 0; i < m; ++i){for(int j = 0; j < n; content_sort[i][j] = &chunk[i][j], ++j);sort(content_sort[i], content_sort[i] + n, col[i].type? content_judge_true: content_judge_false);}}void output(){int maxlength[10] = {0}, total = 0;for(int i = 0; i < co.size(); ++i){maxlength[co[i]] = col[co[i]].colname.length();for(int j = 0; j < row.size(); ++j){maxlength[co[i]] = chunk[co[i]][row[j]].str.length() > maxlength[co[i]]? chunk[co[i]][row[j]].str.length(): maxlength[co[i]];}maxlength[co[i]] += 2;total += maxlength[co[i]] + 1;}++total;string ot;ot.resize((total + 1) * (row.size() + 4) + 1);ot.clear();ot.append(1, '+');ot.append(total - 2, '-');ot.append(1, '+');ot.append(1, '\n');ot.append(1, '|');for(int i = 0; i < co.size(); ++i){ot.append((maxlength[co[i]] - col[co[i]].colname.length()) / 2, ' ');ot.append(col[co[i]].colname);ot.append(maxlength[co[i]] - col[co[i]].colname.length() - (maxlength[co[i]] - col[co[i]].colname.length()) / 2, ' ');ot.append(1, '|');}ot.append(1, '\n');ot.append(1, '|');for(int i = 0; i < co.size(); ++i){ot.append(maxlength[co[i]], '-');ot.append(1, '|');}ot.append(1, '\n');if(row.size()){for(int i = 0; i < row.size(); ++i){ot.append(1, '|');for(int j = 0; j < co.size(); ++j){ot.append((maxlength[co[j]] - chunk[co[j]][row[i]].str.length()) / 2, ' ');ot.append(chunk[co[j]][row[i]].str);ot.append(maxlength[co[j]] - chunk[co[j]][row[i]].str.length() - (maxlength[co[j]] - chunk[co[j]][row[i]].str.length()) / 2, ' ');ot.append(1, '|');}ot.append(1, '\n');}}ot.append(1, '+');ot.append(total - 2, '-');ot.append(1, '+');ot.append(1, '\n');ot.append(1, '\n');cout << ot;}void transact(){pair<pstr_int **, pstr_int **> range;column_name_type ctmp;pstr_int ptmp;string select, c1, wh, c2;for(int i = 0; i < k; ++i){cin >> select >> c1 >> wh >> c2;const int tok = c2.find_first_of("=<>");ctmp.colname.assign(c2.begin(), c2.begin() + tok);const int index = *lower_bound(cname, cname + m, &ctmp, column_name_judge) - col;if(col[index].type){ptmp.num = atoi(c2.c_str() + tok + 1);switch(c2[tok]){case '<':range.first = content_sort[index];range.second = lower_bound(content_sort[index], content_sort[index] + n, &ptmp, content_judge_true);break;case '=':range = equal_range(content_sort[index], content_sort[index] + n, &ptmp, content_judge_true);break;case '>':range.first = upper_bound(content_sort[index], content_sort[index] + n, &ptmp, content_judge_true);range.second = content_sort[index] + n;break;default:return;}}else{ptmp.str.assign(c2.begin() + tok + 2, c2.end() - 1);range = equal_range(content_sort[index], content_sort[index] + n, &ptmp, content_judge_false);}row.clear();for(pstr_int **i = range.first; i != range.second; ++i){row.push_back(*i - chunk[index]);}sort(row.begin(), row.end());co.clear();for(string::iterator p = c1.begin(), q; p != c1.end(); p = q != c1.end()? q + 1: q){q = find(p, c1.end(), ',');ctmp.colname.assign(p, q);co.push_back(*lower_bound(cname, cname + m, &ctmp, column_name_judge) - col);}sort(co.begin(), co.end());output();}}int main(){init();transact();return 0;}

 
原创粉丝点击