C++ 程序,在Linux下使用了C++11的特性,也用到了Linux的库。
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<sstream>
#include<fstream>
#include<stdlib.h>
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
using namespace std;
// usename password
vector<string> split(string sourceStr){
string buff = "";
vector<string> strVector;
for(char c : sourceStr){
if(c == ' '){
if(buff != ""){
strVector.push_back(buff);
buff = "";
}
}else{
buff += c;
}
}
if(buff != ""){
strVector.push_back(buff);
}
return strVector;
}
enum LogLevel{
Debug,
Info,
Warn,
Error,
OK
};
class Log{
public:
LogLevel logLevel;
// constructor
Log(LogLevel lv){
logLevel = lv;
if(lv == Error){
cout << RED << "Error" << RESET << endl;
}else if(lv == Info){
cout << GREEN << "Info" << RESET << endl;
}else if(lv == Warn){
cout << YELLOW << "Warn" << RESET << endl;
}else{
cout << BLUE << "Ok" << RESET << endl;
}
}
Log(){}
void info(string str){
cout << GREEN << str << RESET << endl;
}
void warn(string str){
cout << YELLOW << str << RESET << endl;
}
void error(string str){
cout << RED << str << RESET << endl;
}
~Log(){
}
};
enum STATUS{
IN_STOCK,
ON_LOAN
};
class Book{
public:
int id;
string name;
STATUS status;
Book(int inputID, string inputName){
id = inputID;
name = inputName;
status = IN_STOCK;
}
Book(int inputID, string inputName, STATUS inputStatus){
id = inputID;
name = inputName;
status = inputStatus;
}
};
class BookSystem{
public:
Log log;
BookSystem(){
lastID = 0;
}
string getDBPath(){
string path = getcwd(NULL, 0);
string dbName = "book.db";
return path + "/" + dbName;
}
void runCommand(string input){
vector<string> strVector = split(input);
string commandName = strVector[0];
if("reg" == commandName){
if(strVector.size() < 3){
log.error("Syntax error:: reg command requires 2 parameters: Username and password.");
return;
}
reg(strVector);
}else if("login" == commandName){
if(strVector.size() < 3){
log.error("Syntax error:: login command requires 2 parameters: Username and password.");
return;
}
login(strVector);
}else if("addbook" == commandName){
addBook(strVector);
}else if("listbook" == commandName){
listBook(strVector);
}else if("savedb" == commandName){
saveDB(strVector);
}
}
void login(vector<string> strVector){
string userName = strVector[1];
string password = strVector[2];
if(userDatabase.find(userName) == userDatabase.end()){
log.error("The user " + userName + " is not found.");
}else{
if(userDatabase[userName] == password){
log.info("The user " + userName + " logged in successfully.");
}else{
log.error("Username or Password is not correct.");
}
}
}
void reg(vector<string> strVector){
string userName = strVector[1];
string password = strVector[2];
if(userDatabase.find(userName) == userDatabase.end()){
userDatabase[userName] = password;
}else{
log.error("The user " + userName + " has been registered.");
}
return;
}
void addBook(vector<string> strVector){
string name = strVector[1];
string countStr = strVector[2];
//string to int
int int_temp = 0;
stringstream stream(countStr);
stream>>int_temp;
//new a Book object
for(int i=0; i<int_temp; i++){
lastID++;
Book newBook(lastID, name);
bookDB.push_back(newBook);
}
}
void listBook_internal(){
int count = bookDB.size();
// Book book;
for(int i=0; i<count; i++){
auto book = bookDB[i];
cout << book.id << ":" << book.name << endl;
}
}
void listBook(vector<string> strVector){
listBook_internal();
}
void loadDB(vector<string> strVector){
loadDB_internal();
}
void loadDB_internal(){
string dbPath = getDBPath();
ifstream input(dbPath);
while(!input.eof()){
string currLine;
getline(input, currLine);
auto chunks = split(currLine);
if(0 == chunks.size()){
break;
}
if(chunks[0] == "USER"){
string userName = chunks[1];
string password = chunks[2];
userDatabase[userName] = password;
}else if(chunks[0] == "BOOK"){
string idStr = chunks[1];
string name = chunks[2];
string statusStr = chunks[3];
int id;
istringstream(idStr) >> id;
STATUS status;
//plus & on behalf of reference
istringstream(statusStr) >> (int&)status;
Book currBook(id, name, status);
bookDB.push_back(currBook);
}
}
input.close();
}
void saveDB_internal(){
string dbPath = getDBPath();
//output file stream
//C++ ofstream
ofstream output(dbPath);
for(map<string, string>::iterator i=userDatabase.begin(); i!=userDatabase.end(); i++){
output << "USER " << i->first << " " << i->second << endl;
//cout << i->first << ":" << i->second << endl;
}
for(int i=0; i<bookDB.size(); i++){
auto book = bookDB[i];
output << "BOOK " << book.id << " " << book.name << " " << book.status << endl;
}
output.close();
}
void saveDB(vector<string> strVector){
saveDB_internal();
}
map<string, string> userDatabase;
vector<Book> bookDB;
int lastID;
};
int main(){
//cout << Log(Info) << "info" << endl;
Log log;
BookSystem bookSys;
bookSys.loadDB_internal();
string inputBuffer = "";
do{
cout << ("BOOKSSYS >");
//cin >> inputBuffer;
getline(cin, inputBuffer);
bookSys.runCommand(inputBuffer);
}while(inputBuffer != "exit");
return 0;
}
Makefile
edit:main.cpp
g++ -std=c++0x -g -O0 main.cpp -o main
#g++ -std=c++0x -g -o main main.cpp