C++随手练习之职工管理系统

实现功能:
0、退出系统
1、添加信息
2、显示信息
5、查找信息
7、清空信息
删除修改排序没做,原理类似,读取初始文件作出修改,写入新的文件,貌似不能在txt文档直接做操作

#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>

using namespace std;

int main()
{
	while (true) {
		//初始界面
		cout << "**************************************\n";
		cout << "*************职工管理系统*************\n";
		cout << "*********** 0、退出管理系统***********\n";
		cout << "*********** 1、增加职工信息***&*******\n";
		cout << "*********** 2、显示职工信息***********\n";
		cout << "*********** 3、删除离职员工***********\n";
		cout << "*********** 4、修改职工信息***********\n";
		cout << "*********** 5、查找职工信息***********\n";
		cout << "*********** 6、按照编号排序***********\n";
		cout << "*********** 7、清空所有文档***********\n";
		cout << "**************************************\n\n";


		//输入操作编号
		cout << "请输入编号选择操作:\n";
		int choice;
		cin >> choice;

		if (choice == 0) {
			exit(0);
		}
		else if (choice == 1) {

			cout << "请输入职工编号:\n";
			string work_num;
			cin >> work_num;


			cout << "请输入职工姓名:\n";
			string work_name;
			cin >> work_name;


			cout << "请选择职工职位:\n0、经理\n1、组长\n2、普通员工\n";
			string work_degree;
			cin >> work_degree;
			if (work_degree == "0") {
				work_degree = "经理";
			}
			else if (work_degree == "1")
			{
				work_degree = "组长";
			}
			else
			{
				work_degree = "普通员工";
			}

			/*string file_name = "D:\manager.txt";
			ofstream file_writer(file_name, ios_base::out);
			file_writer << work_num << " " << work_name << " " << work_degree << "\n";
			file_writer.close();*/

			ofstream file;
			if (file.bad())
			{
				cout << "cannot open file" << endl;;
			}
			file.open("D:\manager.txt", ios::app);
			file << work_num << " " << work_name << " " << work_degree << "1" << "\n";
		}
		else if (choice == 2) {
			string file_name = "D:\manager.txt";
			ifstream file_reader(file_name);
			if (file_reader.is_open())
			{
				cout << "职工信息如下:\n";
				while (file_reader.peek() != EOF)
				{
					string line;
					getline(file_reader, line, '\n');
					if (line.substr(line.size() - 1), 1) {
						cout << line.substr(0, line.size() - 1) << "\n";
					}
					// do something
				}
				file_reader.close();
			}
			else
			{
				std::cerr << "Fail to open file !" << std::endl;
			}
			getchar();
			getchar();
		}
		else if (choice == 3) {
			string file_name = "D:\manager.txt";
			ofstream file_writer(file_name, ios_base::out);
		}
		else if (choice == 4) {

		}
		else if (choice == 5) {

			cout << "输入职工编号:\n";
			string work_num;
			cin >> work_num;

			string file_name = "D:\manager.txt";
			ifstream file_reader(file_name);
			if (file_reader.is_open())
			{
				while (file_reader.peek() != EOF)
				{
					string line;
					getline(file_reader, line, '\n');

					string num;
					int i = 0;
					for (; i < line.size(); i++) {
						if (line[i] == ' ') {
							break;
						}
					}
					num = line.substr(0, i);

					if (num == work_num) {
						cout << "职工信息如下:\n";
						cout << line.substr(0, line.size() - 1) << "\n";
						break;
					}
					// do something
				}
				file_reader.close();
			}
			else
			{
				std::cerr << "Fail to open file !" << std::endl;
			}

			getchar();
			getchar();
		}
		else if (choice == 6) {

		}
		else if (choice == 7) {
			string file_name = "D:\manager.txt";
			ofstream file_writer(file_name, ios_base::out);
		}
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章