第乙個有用的C 程式

2021-06-29 14:04:45 字數 4746 閱讀 8604

這是乙個雇員記錄系統。

這個程式包含的功能有:

(1)新增雇員的功能

(2)解雇雇員的功能

(3)雇員晉公升的功能

(4)檢視所有雇員的功能,包括過去以及現在的雇員

(5)新增所有當前雇員的功能

(6)新增所有以前雇員的功能

程式分為三個部分:employee類封裝了單個雇員的資訊,database類管理公司的所有雇員,乙個單獨的userinte***ce檔案提供程式的介面。

來看**:

employee.h:

#ifndef employee_h

#define employee_h

#include

namespace records ;

}#endif

employee.cpp(雇員實現類):

#include 

#include "employee.h"

using

namespace

std;

namespace records

void employee::promote(int inraiseamount)

void employee::demote(int indemeritamount)

void employee::hire()

void employee::fire()

void employee::display() const

string employee::getfirstname() const

void employee::setlastname(std::string inlastname)

std::string employee::getlastname() const

void employee::setsalary(int innewsalary)

int employee::getsalary() const

void employee::setemployeenumber(int inemployeenumber)

int employee::getemployeenumber() const

bool employee::getishired() const

}

到這短暫測試一下,來個測試**:

employeetest.cpp:

#include 

#include "employee.h"

using namespace std

;using namespace records;

int main()

測試一下:(注意這裡是g++)

g++ -o a employee.cpp employeetest.cpp

結果:

testing the employee class

employee: kleper, marni

………………………

current employee

employee number: 71

salary: $51050

測試成功。

再看database.h:

//資料庫會自動新雇員乙個雇員號,因此定義了了乙個常量作為編號的開始

#include

#include

#include "employee.h"

namespace records;

}

database.cpp:

#include 

#include

#include "database.h"

using

namespace

std;

namespace records

database::~database()

employee& database::addemployee(std::string infirstname,std::string inlastname)

employee& database::getemployee(int inemployeenumber)

cerr

<<"no employee with number "

<< inemployeenumber }

employee& database::getemployee(std::string infirstname,std::string inlastname)

cerr

<<"no employee with firstname named"

<< infirstname <<"and lastname named"

}void database::displayall() const

}void database::displaycurrent() const

}void database::displayformer() const

}}

同樣的道理,測試一下,

databasetest.cpp:

#include 

#include "database.h"

using

namespace

std;

using

namespace records;

int main()

測試一下:

g++ databasetest.cpp database.cpp employee.cpp

結果:

all employees:

employee: wallis, grep

……………………………

former employee

employee number: 1000

salary: $30000

employee: kleper, scott

……………………………

current employee

employee number: 1001

salary: $100000

employee: solter, nick

……………………………

current employee

employee number: 1002

salary: $100000

current employees:

employee: kleper, scott

……………………………

current employee

employee number: 1001

salary: $100000

employee: solter, nick

……………………………

current employee

employee number: 1002

salary: $100000

former employees:

employee: wallis, grep

……………………………

former employee

employee number: 1000

salary: $30000

測試成功。

最後的介面類,

userinte***ce.cpp:

#include

#include

#include "database.h"

using

namespace

std;

using

namespace records;

int displaymenu();

void dohire(database& indb);

void dofire(database& indb);

void dopromote(database& indb);

void dodemote(database& indb);

int main()

int displaymenu()

void dohire(database& indb)

catch(const

std::exception& )

catch(const

std::exception& )

{cerr

<<"unable to promote employee!"

《最終測試一下:

g++ userinte***ce.cpp database.cpp employee.cpp

結果:

employee database

……………..

(1) hire a new employee

(2) fire a new employee

(3) promote a new employee

(4) list all employees

(5) list all current employee

(6) hire all previous employee

(0) quit

這裡沒有將最後的測試展示出來,大家可以自己做成檔案測試一下。

總的來說,整個程式邏輯較為簡單,較為完全的展示了物件導向的概念。不算太難,只要有點基礎,應該就能理解。不過,整個**還是較多的,需要有點耐心。

第乙個C 程式

include using namespace std 宣告函式 void dosomething int start,int length,int center,bool order 入口 int main start 開始字元 length 字元長度 center 中間字元 order 順序還是...

第乙個c程式

include 新增必要標頭檔案 intmain 主函式,函式入口相信大部分人的第乙個c語言程式都是列印hello world!在這個函式中 第一行 是引用了乙個標準輸入輸出流的標頭檔案,這個可以說是寫 c 語言 必備的。在 c 語言中會有許多標準庫函式,這是 c 語言的開發者和許多大佬在編譯器中留...

第乙個C程式

bcpl new b c unix minix linux gcc gnu linux c語言誕生 1970年 1973年,在肯.湯普遜 丹尼斯.里奇 主 的主導下編寫完成,歸屬於美國貝爾實驗室。c語言的誕生專門用於編寫作業系統,所以天生適合硬體程式設計,也以速度快著稱,也非常適合資料結構和演算法 ...