C++实现表格功能

#include<bits/stdc++.h>
using namespace std;

// 清屏函数
void cls() {
#ifdef _WIN32
	system("cls");
#else
	system("clear");
#endif
}

class Tbl {
	private:
		vector<vector<string>> dat;   // 表格数据
		vector<size_t> colW;          // 列宽
		size_t pad = 2;               // 边距
		bool lft = true;              // 左对齐

	public:
		// 创建空表格
		void crt(size_t r, size_t c) {
			dat.clear();
			colW.assign(c, 0);
			for (size_t i = 0; i < r; ++i) {
				vector<string> row(c, "");
				dat.push_back(row);
			}
		}

		// 设置表头
		void setHdr(const vector<string>& h) {
			if (!dat.empty()) {
				dat[0] = h;
				updW(h);
			} else {
				addR(h);
			}
		}

		// 添加行
		void addR(const vector<string>& r) {
			dat.push_back(r);
			updW(r);
		}

		// 添加空行
		void addER() {
			vector<string> r(colW.size(), "");
			dat.push_back(r);
		}

		// 添加空列
		void addEC() {
			for (auto& r : dat) {
				r.push_back("");
			}
			colW.push_back(0);
		}

		// 更新列宽
		void updW(const vector<string>& r) {
			for (size_t i = 0; i < r.size(); ++i) {
				if (colW.size() <= i) {
					colW.push_back(r[i].length());
				} else {
					colW[i] = max(colW[i], r[i].length());
				}
			}
		}

		// 设置单元格
		bool setC(size_t r, size_t c, const string& v) {
			if (r >= dat.size() || c >= dat[r].size()) {
				return false;
			}
			dat[r][c] = v;
			colW[c] = max(colW[c], v.length());
			return true;
		}

		// 删除行
		bool delR(size_t r) {
			if (r >= dat.size()) {
				return false;
			}
			dat.erase(dat.begin() + r);
			recW();
			return true;
		}

		// 删除列
		bool delC(size_t c) {
			if (c >= colW.size()) {
				return false;
			}
			for (auto& r : dat) {
				r.erase(r.begin() + c);
			}
			colW.erase(colW.begin() + c);
			return true;
		}

		// 重新计算列宽
		void recW() {
			colW.assign(colW.size(), 0);
			for (const auto& r : dat) {
				for (size_t i = 0; i < r.size(); ++i) {
					if (i < colW.size()) {
						colW[i] = max(colW[i], r[i].length());
					}
				}
			}
		}

		// 设置对齐
		void setA(bool a) {
			lft = a;
		}

		// 设置边距
		void setP(size_t p) {
			pad = p;
		}

		// 显示表格
		void disp() const {
			if (dat.empty()) {
				cout << "表格为空!" << endl;
				return;
			}

			// 计算总宽度
			size_t totW = 1;
			for (size_t w : colW) {
				totW += w + 2 * pad + 1;
			}

			// 打印边框
			prtB(totW);

			// 打印每行
			for (size_t i = 0; i < dat.size(); ++i) {
				const auto& r = dat[i];
				cout << "|";
				for (size_t j = 0; j < r.size(); ++j) {
					string c = r[j];
					size_t w = (j < colW.size()) ? colW[j] : 0;

					if (lft) {
						cout << setw(pad) << "" << left
						     << setw(w) << c
						     << setw(pad) << "" << "|";
					} else {
						cout << setw(pad) << "" << right
						     << setw(w) << c
						     << setw(pad) << "" << "|";
					}
				}
				// 填充空单元格
				for (size_t j = r.size(); j < colW.size(); ++j) {
					cout << setw(colW[j] + 2 * pad + 1) << "|";
				}
				cout << endl;

				// 表头分隔线
				if (i == 0) {
					prtS(totW);
				}
			}

			// 打印底部边框
			prtB(totW);
		}

		// 打印边框
		void prtB(size_t w) const {
			cout << "+";
			for (size_t i = 0; i < w - 2; ++i) {
				cout << "-";
			}
			cout << "+" << endl;
		}

		// 打印分隔线
		void prtS(size_t w) const {
			cout << "|";
			size_t p = 1;
			for (size_t wd : colW) {
				for (size_t i = 0; i < wd + 2 * pad; ++i) {
					cout << "-";
					p++;
				}
				if (p < w - 1) {
					cout << "+";
					p++;
				}
			}
			cout << "|" << endl;
		}

		// 获取行数
		size_t rCnt() const { return dat.size(); }
		// 获取列数
		size_t cCnt() const { return colW.size(); }
};

// 显示菜单
void shwM() {
	cout << "\n===== 表格菜单 =====" << endl;
	cout << "1. 显示表格" << endl;
	cout << "2. 添加行" << endl;
	cout << "3. 添加列" << endl;
	cout << "4. 修改单元格" << endl;
	cout << "5. 删除行" << endl;
	cout << "6. 删除列" << endl;
	cout << "7. 设置对齐" << endl;
	cout << "8. 设置边距" << endl;
	cout << "9. 重置表格" << endl;
	cout << "0. 退出" << endl;
	cout << "================" << endl;
	cout << "选择: ";
}

// 读取整数
int rdI() {
	int v;
	while (!(cin >> v)) {
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cout << "输入无效,请重试: ";
	}
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	return v;
}

int main() {
	Tbl t;
	int ch;
	do {
		cls();
		shwM();
		ch = rdI();

		switch (ch) {
			case 1:  // 显示表格
				cls();
				t.disp();
				cout << "\n按Enter返回...";
				cin.get();
				break;

			case 2: {  // 添加行
				size_t c = t.cCnt();
				vector<string> r(c);
				cout << "输入新行数据 (" << c << "列):" << endl;
				for (size_t i = 0; i < c; ++i) {
					cout << "列 " << i+1 << ": ";
					getline(cin, r[i]);
				}
				t.addR(r);
				cout << "行添加成功!" << endl;
				cout << "\n按Enter继续...";
				cin.get();
				break;
			}

			case 3:  // 添加列
				t.addEC();
				cout << "列添加成功!" << endl;
				cout << "\n按Enter继续...";
				cin.get();
				break;

			case 4: {  // 修改单元格
				cls();
				t.disp();
				cout << "\n修改单元格" << endl;
				cout << "行号 (0-" << t.rCnt()-1 << "): ";
				size_t r = rdI();
				cout << "列号 (0-" << t.cCnt()-1 << "): ";
				size_t c = rdI();
				cout << "新内容: ";
				string v;
				getline(cin, v);
				if (t.setC(r, c, v)) {
					cout << "单元格更新成功!" << endl;
				} else {
					cout << "位置无效!" << endl;
				}
				cout << "\n按Enter继续...";
				cin.get();
				break;
			}

			case 5: {  // 删除行
				cls();
				t.disp();
				cout << "\n删除行" << endl;
				cout << "行号 (0-" << t.rCnt()-1 << "): ";
				size_t r = rdI();
				if (t.delR(r)) {
					cout << "行删除成功!" << endl;
				} else {
					cout << "行号无效!" << endl;
				}
				cout << "\n按Enter继续...";
				cin.get();
				break;
			}

			case 6: {  // 删除列
				cls();
				t.disp();
				cout << "\n删除列" << endl;
				cout << "列号 (0-" << t.cCnt()-1 << "): ";
				size_t c = rdI();
				if (t.delC(c)) {
					cout << "列删除成功!" << endl;
				} else {
					cout << "列号无效!" << endl;
				}
				cout << "\n按Enter继续...";
				cin.get();
				break;
			}

			case 7:  // 设置对齐
				cout << "对齐方式 (0=右, 1=左): ";
				t.setA(rdI() != 0);
				cout << "对齐方式已设置!" << endl;
				cout << "\n按Enter继续...";
				cin.get();
				break;

			case 8:  // 设置边距
				cout << "边距大小: ";
				t.setP(rdI());
				cout << "边距已设置!" << endl;
				cout << "\n按Enter继续...";
				cin.get();
				break;

			case 9:  // 重置表格
				cout << "行数: ";
				size_t r = rdI();
				cout << "列数: ";
				size_t c = rdI();
				t.crt(r, c);
				cout << "表格已重置为 " << r << "x" << c << "!" << endl;
				
				cout << "设置表头? (1=是, 0=否): ";
				if (rdI() == 1) {
					cout << "输入 " << c << " 个表头,空格分隔:" << endl;
					vector<string> h(c);
					for (size_t i = 0; i < c; ++i) {
						cin >> h[i];
					}
					t.setHdr(h);
					cout << "表头设置成功!" << endl;
				}
				cin.ignore(numeric_limits<streamsize>::max(), '\n');
				cout << "\n按Enter继续...";
				cin.get();
				break;

			case 0:  // 退出
				cout << "程序已退出!" << endl;
				break;

			default:
				cout << "无效选择!" << endl;
				cout << "\n按Enter继续...";
				cin.get();
		}
	} while (ch != 0);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值