编码风格问题求解

ww884203 2010-09-29 08:16:43
今天小小的写了个XX管理系统,所以就上坛子来看看,刚好看见 复制帝 写出了一手非常漂亮的代码,再对比一下我自己的,羞愧得无地自容。复制帝写的代码真的十分干净漂亮,我也希望能写出这么漂亮的代码,有什么好的编码风格方面的建议都说一下,谢谢!
复制帝写的那个漂亮代码原帖地址:
http://topic.csdn.net/u/20100927/19/913b3a3b-094d-4180-a55d-69b385dbd330.html?63190
下面附我自己今天写的代码的一小部分,针对这个提些意见,谢谢!

#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <conio.h>
#include "GlobalFun.h"

typedef std::list<Message> lsm;

using std::cout;
using std::cin;

void OpenFile(lsm & lsM)
{
using std::ifstream;
cout<<"默认文件名为sav.dat\n";
ifstream in;
in.open("sav.dat");
if(!in)
{
cout<<"无默认文件!\n";
}
else
{
Message tempm;
std::string temps;
int temp=in.get();
if(!in.eof())
{
in.putback(temp);
}
int count=0;
while(!in.eof())
{
count++;
in>>temps;
tempm.SetName(temps);
in>>temps;
tempm.SetPhone(temps);
lsM.push_back(tempm);
temp=in.get();
temp=in.get();
if(!in.eof())
{
in.putback(temp);
}
}
cout<<"读取完毕,共"<<count<<"条记录\n";
in.close();
}
}

void OpenFileS(lsm & lsM)
{
using std::cin;
using std::cout;
std::string temps;
cout<<"请输入文件名:";
cin>>temps;
std::ifstream in(temps);
if(!in)
{
cout<<"无该文件!\n";
}
else
{
Message tempm;
int temp=in.get();
if(!in.eof())
{
in.putback(temp);
}
int count=0;
while(!in.eof())
{
count++;
in>>temps;
tempm.SetName(temps);
in>>temps;
tempm.SetPhone(temps);
lsM.push_back(tempm);
temp=in.get();
temp=in.get();
if(!in.eof())
{
in.putback(temp);
}
}
cout<<"读取完毕,共"<<count<<"条记录\n";
in.close();
}
}

void SaveFile(lsm & lsM)
{
using std::ofstream;
cout<<"默认文件名为sav.dat\n";
ofstream out;
out.open("sav.dat",std::ios_base::out | std::ios_base::app);
Message tempm;
std::string temps;
int count=0;
lsm templ;
lsM.sort();
std::unique_copy(lsM.begin(),lsM.end(),std::back_inserter(templ));
for(lsm::const_iterator iter =templ.begin() ; iter!=templ.end() ; ++iter)
{
++count;
temps=iter->GetName();
out<<temps<<" ";
temps=iter->GetPhone();
out<<temps<<"\n";
}
cout<<"保存完毕,共"<<count<<"条记录\n";
out.close();
}

void SaveFileS(lsm & lsM)
{
using std::ofstream;
using std::cin;
using std::cout;
std::string temps;
cout<<"请输入文件名:";
cin>>temps;
ofstream out;
out.open(temps,std::ios_base::out | std::ios_base::app);
Message tempm;
int count=0;
lsm templ;
lsM.sort();
std::unique_copy(lsM.begin(),lsM.end(),std::back_inserter(templ));
for(lsm::const_iterator iter =templ.begin() ; iter!=templ.end() ; ++iter)
{
++count;
temps=iter->GetName();
out<<temps<<" ";
temps=iter->GetPhone();
out<<temps<<"\n";
}
cout<<"保存完毕,共"<<count<<"条记录\n";
out.close();
}

void InsertMessage(lsm & lsM)
{
using std::cout;
using std::cin;
std::string tempname,tempphone;
cout<<"请输入对方姓名:";
cin>>tempname;
cout<<"请输入对方的号码:";
cin>>tempphone;
Message tempm(tempname,tempphone);
lsM.push_back(tempm);
}

bool Find (lsm & lsM,Message & msg)
{
using std::cout;
cout<<"请输入查找方式(输入其他字符时返回)\n"
<<"a.按姓名查找\n"
<<"b.按电话查找\n";
char choice=getch();
std::string temps;
if('a'==choice)
{
PrintAll(lsM);
cout<<"请输入要删除的对象的姓名:";
cin>>temps;
lsm::const_iterator iter = lsM.begin();
for(; iter!=lsM.end() ; ++iter)
{
if(temps==iter->GetName())
{
break;
}
}
if(lsM.end()==iter)
{
cout<<"找不到相应的记录!\n";
return false;
}
else
{
msg=*iter;
return true;
}
}
else if('b'==choice)
{
PrintAll(lsM);
cout<<"请输入要删除的对象的电话:";
cin>>temps;
lsm::const_iterator iter = lsM.begin();
for(; iter!=lsM.end() ; ++iter)
{
if(temps==iter->GetPhone())
{
break;
}
}
if(lsM.end()==iter)
{
cout<<"找不到相应的记录!\n";
return false;
}
else
{
msg=*iter;
return true;
}
}
return false;
}

void EarseMessage(lsm & lsM)
{
Message temp;
if(Find(lsM,temp))
{
lsM.remove(temp);
}
}

void FindMessage(lsm & lsM)
{
Message temp;
Find(lsM,temp);
}

void PrintAll(lsm & lsM)
{
using std::cout;
if(lsM.empty())
{
cout<<"无任何记录!\n";
}
else
{
int count=0;
for(lsm::const_iterator iter = lsM.begin();iter != lsM.end() ; ++iter)
{
count++;
cout<<iter->GetName()<<"\t\t"<<iter->GetPhone()<<std::endl;
}
cout<<"共有"<<count<<"条记录\n";
}
}


然后下面是复制帝写出的漂亮代码:

#include <algorithm>
#include <iostream>
#include <functional>
#include <cstring>
using namespace std;
int main() {
char *Alphabet = "abcdefghijklmnopqrstuvwxyz" ;
char *Vowels = "aeiou" ;
char *AlphaNum = "0123456789abcdef" ;
char result[45] ;
char *last ;

int lenA = strlen(Alphabet) ;
int lenV = strlen(Vowels ) ;
int lenAN = strlen(AlphaNum) ;

cout << "Alphabet = " << Alphabet << endl ;
cout << "Vowels = " << Vowels << endl ;
cout << "AlphaNum = " << AlphaNum << endl ;

cout << "\nusing non-predicate versions" << endl ;

//non-predicate set_difference
last = set_difference(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result) ;
*last = 0 ;
cout << "set_difference(Alphabet, AlphaNum) = " << result << endl ;

//non-predicate set_intersection
last = set_intersection(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result) ;
*last = 0 ;
cout << "set_intersection(Alphabet, AlphaNum) = " << result << endl ;

//non-predicate set_symmetric_difference
last = set_symmetric_difference(Alphabet, Alphabet+lenA,
Vowels , Vowels +lenV,
result) ;
*last = 0 ;
cout << "set_symmetric_difference(Alphabet, Vowels) = " << result << endl ;

//non-predicate set_union
last = set_union(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result) ;
*last = 0 ;
cout << "set_union(Alphabet, AlphaNum) = " << result << endl ;

cout << "\nusing predicate versions" << endl ;

//predicate set_difference
last = set_difference(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result , less<char>()) ;
*last = 0 ;
cout << "set_difference(Alphabet, AlphaNum) = " << result << endl ;

//predicate set_intersection
last = set_intersection(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result , less<char>()) ;
*last = 0 ;
cout << "set_intersection(Alphabet, AlphaNum) = " << result << endl ;

//predicate set_symmetric_difference
last = set_symmetric_difference(Alphabet, Alphabet+lenA,
Vowels , Vowels +lenV,
result , less<char>()) ;
*last = 0 ;
cout << "set_symmetric_difference(Alphabet, Vowels) = " << result << endl ;

//predicate set_union
last = set_union(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result , less<char>()) ;
*last = 0 ;
cout << "set_union(Alphabet, AlphaNum) = " << result << endl ;

return 0 ;
}


...全文
72 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ww884203 2010-09-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lthyxy 的回复:]
得向高手们学习呀~~
[/Quote]
极是
liutengfeigo 2010-09-29
  • 打赏
  • 举报
回复
得向高手们学习呀~~
  • 打赏
  • 举报
回复
能一目了然的风格就是好风格
cattycat 2010-09-29
  • 打赏
  • 举报
回复
多看好书,学人家那样写,编码风格可以学好。
ww884203 2010-09-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chinazhangjie 的回复:]
多看看牛人的书,学习人家的编码风格。比如《C++标准程序库》,《STL源码剖析》
[/Quote]
我刚好这两天在看这两本书,先看第一本再看effective stl再看第二本,呵呵
ww884203 2010-09-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 namelij 的回复:]
把using namespace std;放最前面,在每个函数里面都有Using太罗嗦

函数参数里面的东西,如果不会被修改,建议加上const

比如
PrintAll(const lsm & lsM)
[/Quote]
额,因为我是用函数指针统一调用这些函数的,所以这些函数的接口必须都是一样的
  • 打赏
  • 举报
回复
输入输出之间有空格 比如你的cout<<..<<
建议 cout << .. <<
操作符中间加上空格

比如你的 i+1;
建议 i + 1;
  • 打赏
  • 举报
回复
把using namespace std;放最前面,在每个函数里面都有Using太罗嗦

函数参数里面的东西,如果不会被修改,建议加上const

比如
PrintAll(const lsm & lsM)
独酌逸醉 2010-09-29
  • 打赏
  • 举报
回复
多看看牛人的书,学习人家的编码风格。比如《C++标准程序库》,《STL源码剖析》

64,651

社区成员

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

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