您的位置:首页 > 编程语言 > C语言/C++

c++ 编程规范

2016-05-13 19:08 316 查看
以前敲ACM时习惯了单文件,全局变量的风格,要努力向规范看齐

这是一个的数据库中间件程序,软件杯题目

自己以前的风格

上面的是中间件程序,下面的是单元测试

memDB.cpp

#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include<time.h>
using namespace std;
#define MAX 200000
struct Index{ // 卡口每次的记录索引
int time;
int index;
public:
Index(int time, int index){
this -> time = time;
this -> index = index;
}
};
struct Record { // 卡口每次记录的数据类型
string car;
int posx;
int posy;
int time;
void addrd(string car, int x, int y, int time){
this -> car = car;
this -> posx = x;
this -> posy = y;
this -> time = time;
}
};
typedef struct {  // 记录5分钟内的所有记录
Record rd[MAX * 10]; // record
}Record_Of_Five;
struct Global{
int idx = 0; // 五分钟内的记录索引
Record_Of_Five *rdf; // record_of_five
Record_Of_Five *rdfbk; //另外备份的5分钟指针
map<int, string> mapstr; // 将车牌号 映射为整形
vector<Index> vec[MAX]; //根据车牌号索引卡口记录
map<string, vector<Index> > mapvec; // 为每辆车创建一个vector,存放它经过卡口的记录的索引
int term_five; // 记录是第几个五分钟
};
void init(Global &global){
global.rdf = new Record_Of_Five();
global.rdfbk = new Record_Of_Five();
// 初始化 map<int ,string> 将 车牌号和 整型映射
char buf[10];
for(int i = 0; i < MAX; i++)
{
int j = 1000000 + i + 1;
sprintf(buf, "%d", j);
string str = "A";
for(int ii = 1; ii < 7; ii++)
str += buf[ii];
global.mapstr[i] = str;
}
// 初始化 map<string, vector>
for(int i = 0; i < MAX; i++){
global.mapvec[(global.mapstr)[i]] = (global.vec)[i];
}
}
bool insert(Global &global, string str, int x, int y, int time)
{
int term_five_tmp = time / 300;
if(term_five_tmp != global.term_five){ // 判断当前的5分钟是否满了
global.term_five = term_five_tmp;
Record_Of_Five *tmp = global.rdf;
global.rdf = global.rdfbk;
global.rdfbk = tmp;
global.idx = 0;
}
global.rdf -> rd[(global.idx)++].addrd(str, x, y, time);
Index index(time, global.idx - 1);
global.mapvec[str].push_back(index);
return true;
}
vector<Index> select(Global &global, string car)
{
vector<Index> tmp = global.mapvec[car];
int len = tmp.size();
for(int i = 0; i < len; i++){
cout << tmp[i].index;
}
return global.mapvec[car];
}


unitTest.cpp

#include <cstdio>
#include "middleware.cpp"
using namespace std;
int main()
{
Global global;
init(global);
clock_t start_time=clock();
for(int i = 0; i < 200000; i++){
string car = global.mapstr[i];
//        cout << car << endl;
for(int j = 0; j < 5; j++) { // 共测试 i.max * j.max 次数据
insert(global, car, j, 1, 1);
}
}
cout << "当前已经insert数目: " << global.idx << endl;
//    insert("A000001", 1, 1, 1);
//    insert("A000002", 1, 1, 1);
//    insert("A000001", 1, 1, 1);
//    insert("A000001", 1, 1, 1);
//    cout << mapvec["A000001"].at(1).index << endl;
//    cout << rdf -> rd[199998].time << endl;
clock_t end_time=clock();
cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//输出运行时间
vector<Index> tmp = select(global, "A000002");
return 0;
}


规范代码

出自叶大神之手,后来我改了一些

memDB.h

一些结构体的先后顺序别弄乱了

#pragma once
#include <vector>
#include <map>
#define MAX 200000
namespace memDB {
using namespace std;
struct Record {
string car;
int posx;
int posy;
int time;
void addrd(string car, int x, int y, int time) {
this->car = car;
this->posx = x;
this->posy = y;
this->time = time;
}
};
struct Record_Of_Five{  // 记录5分钟内的所有记录
Record rd[MAX * 10]; // record
};
struct Index {
int time;
int index;
public:
Index(int time, int index) {
this->time = time;
this->index = index;
}
};
struct Global{
int idx = 0; // 五分钟内的记录索引
Record_Of_Five *rdf; // record_of_five
Record_Of_Five *rdfbk; //另外备份的5分钟指针
map<int, string> mapstr; // 将车牌号 映射为整形
vector<Index> vec[MAX]; //根据车牌号索引卡口记录
map<string, vector<Index> > mapvec; // 为每辆车创建一个vector,存放它经过卡口的记录的索引
int term_five; // 记录是第几个五分钟
};
void init();
bool insert(string str, int x, int y, int time);
vector<Index> select(string car);
}


memDB.cpp

// memDB.cpp : Defines the exported functions for the static library.
//
#include "memDB.h"
#include <iostream>
using namespace memDB;
void init(Global &global){
global.rdf = new Record_Of_Five();
global.rdfbk = new Record_Of_Five();
// 初始化 map<int ,string> 将 车牌号和 整型映射
char buf[10];
for(int i = 0; i < MAX; i++)
{
int j = 1000000 + i + 1;
sprintf(buf, "%d", j);
string str = "A";
for(int ii = 1; ii < 7; ii++)
str += buf[ii];
global.mapstr[i] = str;
}
// 初始化 map<string, vector>
for(int i = 0; i < MAX; i++){
global.mapvec[(global.mapstr)[i]] = (global.vec)[i];
}
}
bool insert(Global &global, string str, int x, int y, int time)
{
int term_five_tmp = time / 300;
if(term_five_tmp != global.term_five){ // 判断当前的5分钟是否满了
global.term_five = term_five_tmp;
Record_Of_Five *tmp = global.rdf;
global.rdf = global.rdfbk;
global.rdfbk = tmp;
global.idx = 0;
}
global.rdf -> rd[(global.idx)++].addrd(str, x, y, time);
Index index(time, global.idx - 1);
global.mapvec[str].push_back(index);
return true;
}
vector<Index> select(Global &global, string car)
{
vector<Index> tmp = global.mapvec[car];
int len = tmp.size();
for(int i = 0; i < len; i++){
cout << tmp[i].index;
}
return global.mapvec[car];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: