(7) cmake 编译C++程序(二)

文章目录

概要

在ubuntu下,通过cmake编译一个稍微复杂的管理程序

整体代码结构

整体代码

boss.cpp

cpp 复制代码
#include "boss.h"

Boss::Boss(int id, string name, int dId)
{
	this->Id = id;
	this->Name = name;
	this->DeptId = dId;

}

void Boss::showInfo()
{
	cout << "职工编号: " << this->Id
		<< " \t职工姓名: " << this->Name
		<< " \t岗位:" << this->getDeptName()
		<< " \t岗位职责:管理公司所有事务" << endl;
}

string Boss::getDeptName()
{
	return string("总裁");
}

boss.h

cpp 复制代码
#pragma once
#include<iostream>
using namespace std;
#include "worker.h"

//老板类
class Boss :public Worker
{
public:

	Boss(int id, string name, int dId);

	//显示个人信息
	virtual void showInfo();

	//获取职工岗位名称
	virtual string getDeptName();
};

employee.cpp

cpp 复制代码
#include "employee.h"

Employee::Employee(int id, string name, int dId)
{
	this->Id = id;
	this->Name = name;
	this->DeptId = dId;
}

void Employee::showInfo()
{
	cout << "职工编号: " << this->Id
		<< " \t职工姓名: " << this->Name
		<< " \t岗位:" << this->getDeptName()
		<< " \t岗位职责:完成经理交给的任务" << endl;
}


string Employee::getDeptName()
{
	return string("员工");
}

employee.h

cpp 复制代码
#pragma once 
#include<iostream>
using namespace std;
#include "worker.h"

//员工类
class Employee :public Worker
{
public:

	//构造函数
	Employee(int id, string name, int dId);

	//显示个人信息
	virtual void showInfo();

	//获取职工岗位名称
	virtual string getDeptName();
};

manager.cpp

cpp 复制代码
#include "manager.h"

Manager::Manager(int id, string name, int dId)
{
	this->Id = id;
	this->Name = name;
	this->DeptId = dId;

}

void Manager::showInfo()
{
	cout << "职工编号: " << this->Id
		<< " \t职工姓名: " << this->Name
		<< " \t岗位:" << this->getDeptName()
		<< " \t岗位职责:完成老板交给的任务,并下发任务给员工" << endl;
}

string Manager::getDeptName()
{
	return string("经理");
}

manager.h

cpp 复制代码
#pragma once
#include<iostream>
using namespace std;
#include "worker.h"

//经理类
class Manager :public Worker
{
public:

	Manager(int id, string name, int dId);

	//显示个人信息
	virtual void showInfo();

	//获取职工岗位名称
	virtual string getDeptName();
};

worker.h

cpp 复制代码
#pragma once
#include<iostream>
#include<string>
using namespace std;

//职工抽象基类
class Worker
{
public:

	//显示个人信息
	virtual void showInfo() = 0;
	//获取岗位名称
	virtual string getDeptName() = 0;

	int Id; //职工编号
	string Name; //职工姓名
	int DeptId; //职工所在部门名称编号
};

workerManager.cpp

cpp 复制代码
#include "workerManager.h"

workerManager::workerManager()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	//文件不存在情况
	if (!ifs.is_open())
	{
		cout << "文件不存在" << endl; //测试输出
		this->work_num = 0;  //初始化人数
		this->fileIsEmpty = true; //初始化文件为空标志
		ifs.close(); //关闭文件
		return;
	}

	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		cout << "文件为空!" << endl;
		this->work_num = 0;
		this->fileIsEmpty = true;
		ifs.close();
		return;
	}

	int num =  this->get_Num();
	this->work_num = num;  //更新成员属性

	init_Emp(); 
	for (int i = 0; i < this->work_num; i++)
	{
		cout << "职工号: " << this->works[i]->Id
			<< " 职工姓名: " << this->works[i]->Name
			<< " 部门编号: " << this->works[i]->DeptId << endl;
	}
}

workerManager::~workerManager()
{
	for (vector<Worker *>::iterator it = this->works.begin(); it != this->works.end(); it ++) 
    if (NULL != *it) 
    {
        delete *it; 
        *it = NULL;
    }
	this->works.clear();

}

void workerManager::Show_Menu()
{
	cout << "********************************************" << endl;
	cout << "*********  欢迎使用职工管理系统! **********" << endl;
	cout << "*************  0.退出管理程序  *************" << endl;
	cout << "*************  1.增加职工信息  *************" << endl;
	cout << "*************  2.显示职工信息  *************" << endl;
	cout << "*************  3.删除离职职工  *************" << endl;
	cout << "*************  4.修改职工信息  *************" << endl;
	cout << "*************  5.查找职工信息  *************" << endl;
	cout << "*************  6.按照编号排序  *************" << endl;
	cout << "*************  7.清空所有文档  *************" << endl;
	cout << "********************************************" << endl;
	cout << endl;
}

void workerManager::exitSystem()
{
	cout << "欢迎下次使用" << endl;
    cin.ignore();
	cin.get();
    system("clear");
	exit(0);
}

void workerManager::Add_Emp()
{
	cout << "请输入增加职工数量: " << endl;

	int addNum = 0;
	cin >> addNum;

	if (addNum > 0)
	{	
		int new_size = this->work_num + addNum;
		this->works.reserve(new_size);

		//输入新数据
		for (int i = 0; i < addNum; i++)
		{
			int id;
			string name;
			int dSelect;

			cout << "请输入第 " << i + 1 << " 个新职工编号:" << endl;
			cin >> id;

			cout << "请输入第 " << i + 1 << " 个新职工姓名:" << endl;
			cin >> name;


			cout << "请选择该职工的岗位:" << endl;
			cout << "1、普通职工" << endl;
			cout << "2、经理" << endl;
			cout << "3、老板" << endl;
			cin >> dSelect;


			Worker * worker = NULL;
			switch (dSelect)
			{
			case 1: //普通员工
				worker = new Employee(id, name, 1);
				break;
			case 2: //经理
				worker = new Manager(id, name, 2);
				break;
			case 3:  //老板
				worker = new Boss(id, name, 3);
				break;
			default:
				break;
			}


			this->works.push_back(worker);
		}

		this->work_num = new_size;
		this->fileIsEmpty = false;
		//提示信息
		cout << "成功添加" << addNum << "名新职工!" << endl;
		this->save();
	}
	else
	{
		cout << "输入有误" << endl;
	}

	std::cout << "Press Enter to continue...";
    cin.ignore();
	cin.get();
    system("clear");
}

void workerManager::save()
{
	ofstream ofs;
	ofs.open(FILENAME, ios::out);
	for (int i = 0; i < this->work_num; i++)
	{
		ofs << this->works[i]->Id << " " 
			<< this->works[i]->Name << " " 
			<< this->works[i]->DeptId << endl;
	}

	ofs.close();
}


int workerManager::get_Num()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	string line;

	int num = 0;

	while (getline(ifs, line)) { // 逐行读取文件内容
		if(line.empty()){
			continue;
		}
		num++;
    }
	ifs.close();

	return num;
}

void workerManager::init_Emp()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int id;
	string name;
	int dId;
	
	int index = 0;
	while (ifs >> id && ifs >> name && ifs >> dId)
	{
		Worker * worker = NULL;
		if (dId == 1)  // 1普通员工
		{
			worker = new Employee(id, name, dId);
		}
		else if (dId == 2) //2经理
		{
			worker = new Manager(id, name, dId);
		}
		else //总裁
		{
			worker = new Boss(id, name, dId);
		}
		// //存放在数组中
		works.push_back(worker);
		index++;
	}
}


void workerManager::Show_Emp()
{
	system("clear");
	if (this->fileIsEmpty)
	{
		cout << "文件不存在或记录为空!" << endl;
	}
	else
	{
		for (int i = 0; i < this->work_num; i++)
		{
			//利用多态调用接口
			this->works[i]->showInfo();
		}
	}

	std::cout << "Press Enter to continue...";
    cin.ignore();
	cin.get();
    system("clear");
}

int workerManager::IsExist(int id)
{
	int index = -1;

	for (int i = 0; i < this->work_num; i++)
	{
		if (this->works[i]->Id == id)
		{
			index = i;

			break;
		}
	}

	return index;
}

void workerManager::Del_Emp()
{
	system("clear");
	if (this->fileIsEmpty)
	{
		cout << "文件不存在或记录为空!" << endl;
	}
	else
	{
		//按职工编号删除
		cout << "请输入想要删除的职工号:" << endl;
		int id = 0;
		cin >> id;

		int index = this->IsExist(id);

		if (index != -1)  //说明index上位置数据需要删除
		{
			for (int i = index; i < this->work_num - 1; i++)
			{
				this->works[i] = this->works[i + 1];
			}
			this->work_num--;

			this->save(); //删除后数据同步到文件中
			cout << "删除成功!" << endl;
		}
		else
		{
			cout << "删除失败,未找到该职工" << endl;
		}
	}
	
	std::cout << "Press Enter to continue...";
    cin.ignore();
	cin.get();
    system("clear");
}

void workerManager::Mod_Emp()
{
	if (this->fileIsEmpty)
	{
		cout << "文件不存在或记录为空!" << endl;
	}
	else
	{
		cout << "请输入修改职工的编号:" << endl;
		int id;
		cin >> id;

		int ret = this->IsExist(id);
		if (ret != -1)
		{ 
			//查找到编号的职工
			int newId = 0;
			string newName = "";
			int dSelect = 0;

			cout << "查到: " << id << "号职工,请输入新职工号: " << endl;
			cin >> newId;

			cout << "请输入新姓名: " << endl;
			cin >> newName;

			cout << "请输入岗位: " << endl;
			cout << "1、普通职工" << endl;
			cout << "2、经理" << endl;
			cout << "3、老板" << endl;
			cin >> dSelect;

			this->works[ret]->Id = newId;
			this->works[ret]->Name = newName;
			this->works[ret]->DeptId = dSelect;
			
			cout << "修改成功!" << endl;

			//保存到文件中
			this->save();
		}
		else
		{
			cout << "修改失败,查无此人" << endl;
		}
	}

	std::cout << "Press Enter to continue...";
    cin.ignore();
	cin.get();
    system("clear");
}

void workerManager::Find_Emp()
{
	if (this->fileIsEmpty)
	{
		cout << "文件不存在或记录为空!" << endl;
	}
	else
	{
		cout << "请输入查找的方式:" << endl;
		cout << "1、按职工编号查找" << endl;
		cout << "2、按姓名查找" << endl;

		int select = 0;
		cin >> select;


		if (select == 1) //按职工号查找
		{
			int id;
			cout << "请输入查找的职工编号:" << endl;
			cin >> id;

			int ret = IsExist(id);
			if (ret != -1)
			{
				cout << "查找成功!该职工信息如下:" << endl;
				this->works[ret]->showInfo();
			}
			else
			{
				cout << "查找失败,查无此人" << endl;
			}
		}
		else if(select == 2) //按姓名查找
		{
			string name;
			cout << "请输入查找的姓名:" << endl;
			cin >> name;

			bool flag = false;  //查找到的标志
			for (int i = 0; i < this->work_num; i++)
			{
				if (works[i]->Name == name)
				{
					cout << "查找成功,职工编号为:"
						<< works[i]->Id
						<< " 号的信息如下:" << endl;
					
					flag = true;

					this->works[i]->showInfo();
				}
			}
			if (flag == false)
			{
				//查无此人
				cout << "查找失败,查无此人" << endl;
			}
		}
		else
		{
			cout << "输入选项有误" << endl;
		}
	}


	std::cout << "Press Enter to continue...";
    cin.ignore();
	cin.get();
    system("clear");
}


void workerManager::Sort_Emp()
{
	if (this->fileIsEmpty)
	{
		cout << "文件不存在或记录为空!" << endl;
		std::cout << "Press Enter to continue...";
		cin.ignore();
		cin.get();
		system("clear");
	}
	else
	{
		cout << "请选择排序方式: " << endl;
		cout << "1、按职工号进行升序" << endl;
		cout << "2、按职工号进行降序" << endl;

		int select = 0;
		cin >> select;


		for (int i = 0; i < this->work_num; i++)
		{
			int minOrMax = i;
			for (int j = i + 1; j < this->work_num; j++)
			{
				if (select == 1) //升序
				{
					if (this->works[minOrMax]->Id > this->works[j]->Id)
					{
						minOrMax = j;
					}
				}
				else  //降序
				{
					if (this->works[minOrMax]->Id < this->works[j]->Id)
					{
						minOrMax = j;
					}
				}
			}

			if (i != minOrMax)
			{
				Worker * temp = this->works[i];
				this->works[i] = this->works[minOrMax];
				this->works[minOrMax] = temp;
			}

		}

		cout << "排序成功,排序后结果为:" << endl;
		this->save();
		this->Show_Emp();
	}

}

void workerManager::Clean_File()
{
	cout << "确认清空?" << endl;
	cout << "1、确认" << endl;
	cout << "2、返回" << endl;

	int select = 0;
	cin >> select;

	if (select == 1)
	{
		//打开模式 ios::trunc 如果存在删除文件并重新创建
		ofstream ofs(FILENAME, ios::trunc);
		ofs.close();

		if (this->works.size() > 0)
		{
            for (int i = 0; i < this->works.size(); i++)
			{
				if (this->works[i] != NULL)
				{
					delete this->works[i];
				}
			}
			this->work_num = 0;
			this->fileIsEmpty = true;
		}
		cout << "清空成功!" << endl;
	}

	std::cout << "Press Enter to continue...";
    cin.ignore();
	cin.get();
    system("clear");
}

workerManager.h

cpp 复制代码
#pragma once
#include<iostream>
#include<vector>
#include <fstream>
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"
using namespace std;

#define  FILENAME "empFile.txt"

class workerManager
{
private:
    int work_num = 0;
    bool fileIsEmpty;
    vector<Worker*> works;

public:
    workerManager();

    void Show_Menu();

    int get_Num();

    void exitSystem();

    void Add_Emp();

    void init_Emp();

    void Show_Emp();

    void Del_Emp();

    void Mod_Emp();

    void Find_Emp();

    void Sort_Emp();

    void Clean_File();

    int IsExist(int id);

    void save();
    
    ~workerManager();
};

main.cpp

cpp 复制代码
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;

#include "src/workerManager.h"
#include "src/worker.h"
#include "src/employee.h"
#include "src/manager.h"
#include "src/boss.h"

void test()
{
	Worker * worker = NULL;
	worker = new Employee(1, "张三", 1);
	worker->showInfo();
	delete worker;
	
	worker = new Manager(2, "李四", 2);
	worker->showInfo();
	delete worker;

	worker = new Boss(3, "王五", 3);
	worker->showInfo();
	delete worker;
}


int main(){
	// test();
    workerManager wm;
	int choice = 0;
	while (true)
	{
		//展示菜单
		wm.Show_Menu();
		cout << "请输入您的选择:" << endl;
		cin >> choice;

		switch (choice)
		{
		case 0: //退出系统
			wm.exitSystem();
			break;
		case 1: //添加职工
			wm.Add_Emp();
			break;
		case 2: //显示职工
			wm.Show_Emp();
			break;
		case 3: //删除职工
			wm.Del_Emp();
			break;
		case 4: //修改职工
			wm.Mod_Emp();
			break;
		case 5: //查找职工
			wm.Find_Emp();
			break;
		case 6: //排序职工
			wm.Sort_Emp();
			break;
		case 7: //清空文件
			wm.Clean_File();
			break;
		default:
			system("clear");
			break;
		}
	}

	std::cout << "Press Enter to continue...";
    cin.ignore();
	cin.get();
    system("clear");

	return 0;

}

CMakeLists.txt

bash 复制代码
cmake_minimum_required(VERSION 2.6) # cmake最低版本
project(manager_project) #项目名称
set(CMAKE_CXX_STANDARD 11) #设置C++编译版本
set(CMAKE_BUILD_TYPE "Debug") # 默认是Release模式,设置为Debug才能调试

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) #设置可执行文件生产的路径
file(GLOB SOURCES "${PROJECT_SOURCE_DIR}/src/*.cpp")

aux_source_directory(. SRC_LISTS) #.下所有的cpp文件打包到变量SRC_LISTS中
add_executable(demo ${SOURCES} ${SRC_LISTS}) #生成可执行文件demo

run.sh

bash 复制代码
#!/bin/bash

mkdir -p build
cd build
cmake ..
make
cd ../bin
./demo

小结

好像还有点小bug没调试,整体应该是能运行

相关推荐
唐诺3 小时前
几种广泛使用的 C++ 编译器
c++·编译器
冷眼看人间恩怨4 小时前
【Qt笔记】QDockWidget控件详解
c++·笔记·qt·qdockwidget
红龙创客4 小时前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
Lenyiin4 小时前
第146场双周赛:统计符合条件长度为3的子数组数目、统计异或值为给定值的路径数目、判断网格图能否被切割成块、唯一中间众数子序列 Ⅰ
c++·算法·leetcode·周赛·lenyiin
yuanbenshidiaos6 小时前
c++---------数据类型
java·jvm·c++
十年一梦实验室6 小时前
【C++】sophus : sim_details.hpp 实现了矩阵函数 W、其导数,以及其逆 (十七)
开发语言·c++·线性代数·矩阵
taoyong0016 小时前
代码随想录算法训练营第十一天-239.滑动窗口最大值
c++·算法
这是我586 小时前
C++打小怪游戏
c++·其他·游戏·visual studio·小怪·大型·怪物
fpcc6 小时前
跟我学c++中级篇——C++中的缓存利用
c++·缓存
呆萌很7 小时前
C++ 集合 list 使用
c++