c++ 编程规范

  • 以前敲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];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值