大家帮我看看我的代码风格吗,我这里的代码的编程规范哪些地方不好,希望大家都批评一下,多多进步吧。

kevin820601 2009-01-01 10:49:04
/* publicdef.h*/
#ifndef _PUBLIC_DEF_H
#define _PUBLIC_DEF_H

#define B_TRUE true
#define B_FALSE false

typedef char CHAR;
typedef unsigned short USHORT;
typedef unsigned long ULONG;
typedef bool BOOL;

#define ASSERT(_bResult)\
{\
if (!_bResult)\
{\
printf("%s(%d)\n", __FILE__, __LINE__);\
}\
}\

BOOL Is_String_Contain(CHAR* pcStrMother, CHAR* pcStrChild);
#endif




/* main.cpp */
#include <stdio.h>
#include <string.h>
#include "publicdef.h"

BOOL Is_String_Contain(CHAR* pcStrMother, CHAR* pcStrChild)
{
ULONG ulMotherLen = 0;
ULONG ulChildLen = 0;
ULONG ulStartPos = 0;
ULONG ulCnt = 0;
ULONG ulMatchCount = 0;

if (NULL == pcStrMother || NULL == pcStrChild)
{
ASSERT(0);
return B_FALSE;
}

ulMotherLen = (ULONG)strlen(pcStrMother);
ulChildLen = (ULONG)strlen(pcStrChild);

if (ulMotherLen < ulChildLen)
{
return B_FALSE;
}

while (ulStartPos + ulChildLen <= ulMotherLen)
{
for (ulCnt = 0; ulCnt < ulChildLen ; ulCnt ++)
{
if(pcStrChild[ulCnt] == pcStrMother[ulStartPos + ulCnt])
{
ulMatchCount++;
}
else
{
break;
}
}

if(ulMatchCount == ulChildLen)
{
return B_TRUE;
}

ulMatchCount = 0;
ulStartPos++;
}

return B_FALSE;

}
int main()
{
CHAR* str1 = "abc";
CHAR* str2 = "bc";

if (B_TRUE == Is_String_Contain(str1, str2))
{
printf("contained");
}
else
{
printf("Not contained");
}

return 0;
}



...全文
206 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
dahua010 2009-01-04
  • 打赏
  • 举报
回复
大括号这个东西我觉得还是少用,所以如果是只有1句的if语句,往往我就不加大括号了。因为有的时候大括号过多代码的可读性就会比较差。

另外自己的一个风格:类似于条件嵌套循环这种反复嵌套的代码,如果需要超过3次嵌套,也就说3层大括号以后还需要嵌套的话,感觉还是另想别的算法解决,肯定会有解决的办法,嵌套过多太不利于理解了
dahua010 2009-01-04
  • 打赏
  • 举报
回复

//这里的
for (ulCnt = 0; ulCnt < ulChildLen ; ulCnt ++)
{
if(pcStrChild[ulCnt] == pcStrMother[ulStartPos + ulCnt])
{
ulMatchCount++;
}
else
{
break;
}
}
//我会写成
for (ulCnt = 0; ulCnt < ulChildLen ; ulCnt ++)
{
if(pcStrChild[ulCnt] != pcStrMother[ulStartPos + ulCnt])
break;

ulMatchCount++;

}

waguju 2009-01-04
  • 打赏
  • 举报
回复
路过了,顶,继续加油
fallening 2009-01-03
  • 打赏
  • 举报
回复
昨天写了一文章,里边有不少代码,楼主不妨过去提下意见;
hwjoseph 2009-01-03
  • 打赏
  • 举报
回复
还不错了
xuruichen 2009-01-03
  • 打赏
  • 举报
回复
还不错了。

我不喜欢printf,


楼主写的还是很漂亮的。

支持
ypb362148418 2009-01-03
  • 打赏
  • 举报
回复
建议你看看林锐的那本C&c++高质量编程,这里面全部是程序风格的写法
ch1oE 2009-01-03
  • 打赏
  • 举报
回复
LZ,我很想问你一个问题,希望可以得到回答~:
请你从哪里看到(书上还是什么地方?)的,需要写一个这样的publicdef.h?

谢谢~
agaric 2009-01-02
  • 打赏
  • 举报
回复
个人不喜欢又分大小写又带下划线的,一般全小写加下划线,或者是用驼峰式
hackers007 2009-01-02
  • 打赏
  • 举报
回复
楼主写的代码风格,已经很不错了
我提供点代码你参考
//
//摘要:
// 向排序表中插入数据,可以是相同的数据
//参数:
// _InsertValue 插入的数据值
//返回值:
// 返回插入数据后的排序表
Smart::SortedChain<_Ty,_Key>& Insert(const _Ty& _InsertValue);
//
//摘要:
// 搜索与_KeyValue匹配的元素并删除
// 结果放入_SearchValue
//参数:
// _Key_Value 查询的键值
// _DeleteValue 保存删除的值
//返回值:
// 返回删除后的排序表
//异常:
// 没有匹配的元素--Smart::Exception::BadInput()
Smart::SortedChain<_Ty,_Key>& Delete(const _Key& _KeyValue,_Ty& _DeleteValue);

template <class _Ty,class _Key>
Smart::SkipList<_Ty,_Key>& Smart::SkipList<_Ty,_Key>::Insert(
const _Ty &_InsertValue)
{
//If there is no repeat,then Insert _InsertValue
_Key _KeyValue=_InsertValue; //The key value extraction
if(_KeyValue>=_TailKey)
//The key value too
throw Smart::Exception::BadInput("The key value too ");

//check whether repeat
SkipNode<_Ty,_Key> *_p=SaveSearch(_KeyValue);
if(_p->data==_InsertValue)
//repeat key
throw Smart::Exception::BadInput("Repeat key value,");
//Do not repeat, in order to determine the level of the new node,

int _lev=Level();
//fix _lev to be <=levels+1
if(_lev>_Levels){
_lev=++_Levels;
_last[_lev]=_head;
}
// A new node and a new node inserted after the _p
SkipNode<_Ty,_Key> *New_Node=new SkipNode<_Ty,_Key>(_lev+1);
New_Node->data=_InsertValue;
for(int level_index=0;level_index<=_lev;level_index++){
//Insert the level_index level chain
New_Node->link[level_index]=\
_last[level_index]->link[level_index];
_last[level_index]->link[level_index]=New_Node;
}
return *this;
}

template <class _Ty,class _Key>
Smart::SkipList<_Ty,_Key>& Smart::SkipList<_Ty,_Key>::Delete(const
_Key &_KeyValue, _Ty &_DeleteValue)
{
//_KeyValue to delete and match elements, and will be deleted by the
//elements into _DeleteValue ,If there is no match with _KeyValue elements,
//lead to abnormal BadInput
if(_KeyValue>=_TailKey)
//The key value too
throw Smart::Exception::BadInput("The key value too ");

//_KeyValue and check whether there is an element that matches
SkipNode<_Ty,_Key> *_p=SaveSearch(_KeyValue);
if(_p->data!=_KeyValue)
//key value is not exist
throw Smart::Exception::BadInput("key value is not exist");
//SkipList delete nodes
for(int level_index=0;level_index<=_Levels&&
_last[level_index]->link[level_index]==_p;level_index++)
_last[level_index]->link[level_index]=_p->link[level_index];

//Revised levels
while(_Levels>0&&_head->link[_Levels]==_tail)
_Levels--;
_DeleteValue=_p->data;
delete _p;
return *this;
}
sagegz 2009-01-02
  • 打赏
  • 举报
回复
没风格,应该说是个人习惯以及自我感觉
sysabod 2009-01-02
  • 打赏
  • 举报
回复
学习
waizqfor 2009-01-01
  • 打赏
  • 举报
回复
呵呵 程序没看 不过感觉思路挺清晰!~ 加油
xiaoyisnail 2009-01-01
  • 打赏
  • 举报
回复
那些宏定义真的没必要,另外你的代码都是c的代码,既然这儿是 c++版,你还是应该用c++的标准库来写,用<iostream>、<string>这些,还有就是你的查找子串的函数可以用更精简的算法来写,而且你是在重新造车轮,有现成的库函数做这件事情了
kevin820601 2009-01-01
  • 打赏
  • 举报
回复
缩进更规范就好了

缩进本来还可以,贴上来就不行了,呵呵
waizqfor 2009-01-01
  • 打赏
  • 举报
回复
变量名最好再明确一点 缩进更规范就好了
#define B_TRUE true
#define B_FALSE false

感觉定义它没什么必要
kevin820601 2009-01-01
  • 打赏
  • 举报
回复
希望大家都能贴出自己的少量代码,让我见识一下大家的代码风格,呵呵

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧