请教一下各位高手,关于C++的习惯使用方法,如何能够写出漂亮的代码

zapdos 2009-01-21 10:28:53
大家好,小弟平常不怎么用C++,所以写起来好像有点不太习惯
特意来请教一下各位高手,关于C++的习惯使用方法,如何能够写出漂亮的代码
我就先贴一个自己写的代码,希望大家能指出其中的问题和毛病
所有关于语法和习惯方面的代码都可以提出

比方说:“Node n2 = *new Node(*cr);这样写很白痴呀!原因是xxx...”
“以map作为全局函数、map的数据初始化放在main里是弱智的行为,因为xxx...”
“定义函数名一定要以返回类型开头,因为xxx...”
...

希望各位能指点一下,谢谢,祝大家新年快乐
年后结贴


#include <iostream>
#include <fstream>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <sstream>
#include <stack>
#include <deque>
#include <map>
#include <cctype>
#define BRACKET 5
#define ALPHA 4
#define LOGIC_SIGN 3
#define SIGN_2 2
#define NUMRIC 1
#define NULL_NODE 0;
using std::cin;
using std::string;
using std::vector;
using std::stringstream;
using std::ifstream;
using std::endl;
using std::ofstream;
//using std::cout;
using std::stack;
using std::deque;
using std::map;
using std::pair;
using std::isalnum;
using std::isdigit;
using std::isalpha;
ofstream cout("result.txt");
typedef double (*fx)(double,double);
typedef bool (*fz)(double,double);
map<string,int> sign_map;
map<string,int> priority_map;
map<string,fx> fx_map;
map<string,fz> fz_map;
template<typename T> void display(T t){
for(T::iterator it = t.begin();
it!=t.end();
++it)
cout<<it->ctx<<" ";
cout<<" "<<endl;
}
double f1(double d1,double d2){
return d1+d2;
}
double f2(double d1,double d2){
return d1-d2;
}
double f3(double d1,double d2){
return d1*d2;
}
double f4(double d1,double d2){
return d1/d2;
}
double f5(double d1,double d2){
return d1-floor(d1/d2)*d2;
}
bool f6(double d1,double d2){
return d1>d2;
}
bool f7(double d1,double d2){
return d1<d2;
}
bool f8(double d1,double d2){
return d1==d2;
}
bool f9(double d1,double d2){
return d1!=d2;
}
bool f10(double d1,double d2){
return d1>=d2;
}
bool f11(double d1,double d2){
return d1<=d2;
}
class Node{
public:
Node(const char c){
ctx = c;
if(isdigit(c)||c=='.')
type = NUMRIC;
else if(isalpha(c))
type = ALPHA;
else if(c=='('||c==')')
type = BRACKET;
else{
if(sign_map.find(ctx)!=sign_map.end())
type = sign_map[ctx];
else{
cout<<"unable to generate Node"<<endl;
type = NULL_NODE;
delete this;
}
}
if(type==SIGN_2||type==LOGIC_SIGN)
priority = priority_map[ctx];
cout<<"construct:"<<ctx<<endl;
}
Node(Node &n1,Node &n2){
if(n1.type==n2.type){
type = n1.type;
ctx = n1.ctx+n2.ctx;
if(type==SIGN_2||type==LOGIC_SIGN)
priority = priority_map[ctx];
}else{
cout<<"unable to generate Node"<<endl;
type = NULL_NODE;
delete this;
}
cout<<"construct:"<<ctx<<endl;
}
~Node(){
cout<<"destruct:"<<ctx<<endl;
}
operator string(){
char tmp[10];
return ctx;
}
double numric;
string ctx;
int priority;
int type;
int *n;
};
class NumricFilter{
public:
NumricFilter(const string &s):numric(0){
if(!parseInput(s))
return;
parseNum(0);
}
~NumricFilter(){
if(numric!=0)
delete [] numric;
}
void show(){
cout<<numricDesc<<endl;
for(vector<string>::iterator it = s_rule.begin();
it!=s_rule.end();
it++)
cout<<*it<<endl;
cout<<n;
}
private:
bool parseInput(const string &s){
stringstream ss;
string s1;
ss<<s;
if(!(ss>>s1))
return 0;
numricDesc = s1;
while(ss>>s1)
s_rule.push_back(s1);
const char *c = numricDesc.c_str();
for(int i=0;;i++)
if(c[i]=='\0'){
numric = new int[i];
n = i;
break;
}else{
numric_map[c[i]] = i;
}
for(unsigned int i=0;i<s_rule.size();i++)
rule.push_back(*parseRule(s_rule[i]));
cout<<"numric description : "<<numricDesc<<endl;
return 1;
}
bool checkNum(){
for(vector<vector<Node> >::iterator it = rule.begin();
it!=rule.end();
++it){
stack<double> st1;
stack<string> st2;
unsigned int i = 0;
while(i<it->size()){
Node n = (*it)[i];
if(n.type==SIGN_2||n.type==LOGIC_SIGN){
double d2 = st1.top();
st1.pop();
double d1 = st1.top();
st1.pop();
if(n.type==SIGN_2){
double d3 = fx_map[n.ctx](d1,d2);
st1.push(d3);
}else{
if(!fz_map[n.ctx](d1,d2))
return 0;
else
break;
}
}else if(n.type==NUMRIC||n.type==ALPHA){
if(n.type==NUMRIC)
st1.push(n.numric);
else
st1.push(*n.n);
}
i++;
}
}
return 1;
}
void showNum(){
for(int j=0;j<n;j++)
cout<<numric[j];
cout<<endl;
}
void parseNum(int i){
if(i>=n){
if(checkNum())
showNum();
return;
}
for(int j=0;j<10;j++){
numric[i] = j;
parseNum(i+1);
}
}
vector<Node> *parseRule(string &s){
const char * c = s.c_str();
vector<Node> *v = new vector<Node>;

//词法分析
int i = 0;
deque<Node> tmp;
tmp.push_back(*new Node(c[i++]));
while(1){
const char *cr = &c[i++];
if(*cr=='\0')
break;
Node n1 = tmp.back();
Node n2 = *new Node(*cr);
tmp.pop_back();
if(n1.type==n2.type){
Node *n3 = new Node(n1,n2);
tmp.push_back(*n3);
}else{
tmp.push_back(n1);
tmp.push_back(n2);
}
}
display<deque<Node> >(tmp);

//语法分析
stack<Node> st;
Node *n,*n2;
int pri = 0;
while(tmp.size()>0){
n = &tmp.front();
tmp.pop_front();
if(n->type==BRACKET){
if(n->ctx=="(")
pri += 10;
else if(n->ctx==")")
pri -=10;
continue;
}else if(n->type==NUMRIC||n->type==ALPHA){
v->push_back(*n);
}else if(n->type==SIGN_2||n->type==LOGIC_SIGN){
n->priority += pri;
if(st.size()==0)
st.push(*n);
else{
while(st.size()>0){
n2 = &st.top();
if(n2->priority>=n->priority){
st.pop();
v->push_back(*n2);
}else{
break;
}
}
st.push(*n);
}
}
}
while(st.size()>0){
n2 = &st.top();
st.pop();
v->push_back(*n2);
}

//数据初始化
for(vector<Node>::iterator it = v->begin();
it != v->end();
++it){
const char *sx = (it->ctx).c_str();
if(it->type==NUMRIC)
it->numric = atof(sx);
else if(it->type==ALPHA){
it->n = numric+numric_map[sx[0]];
}
}
//display<vector<Node> >(*v);
return v;
}
string numricDesc;
vector<string> s_rule;
vector<int> result;
vector<vector<Node> > rule;
int *numric;
int n;
map<char,int> numric_map;
};

int main(){
sign_map["+"] = SIGN_2;
sign_map["-"] = SIGN_2;
sign_map["*"] = SIGN_2;
sign_map["/"] = SIGN_2;
sign_map["%"] = SIGN_2;
sign_map[">"] = LOGIC_SIGN;
sign_map["<"] = LOGIC_SIGN;
sign_map["="] = LOGIC_SIGN;
sign_map["!"] = LOGIC_SIGN;
sign_map["!="] = LOGIC_SIGN;
sign_map[">="] = LOGIC_SIGN;
sign_map["<="] = LOGIC_SIGN;
priority_map["+"] = 2;
priority_map["-"] = 2;
priority_map["*"] = 4;
priority_map["/"] = 4;
priority_map["%"] = 4;
priority_map[">"] = 0;
priority_map["<"] = 0;
priority_map["="] = 0;
priority_map["!"] = 0;
priority_map["!="] = 0;
priority_map[">="] = 0;
priority_map["<="] = 0;
fx_map["+"] = f1;
fx_map["-"] = f2;
fx_map["*"] = f3;
fx_map["/"] = f4;
fx_map["%"] = f5;
fz_map[">"] = f6;
fz_map["<"] = f7;
fz_map["="] = f8;
fz_map["!="] = f9;
fz_map[">="] = f10;
fz_map["<="] = f11;
string input;
std::cout<<"usage:\n numricDescription [rules...]\nexample:\n abc a%3!=1 (a+c)*2+3*(a-b)=9"<<endl;
//getline(cin,input);
input = "abc a%3!=1 (a+c)*2+3*(a-b)=9";//test
NumricFilter nf(input);
//nf.show();
cout<<"end"<<endl;
return 0;
}
...全文
114 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoyisnail 2009-01-21
  • 打赏
  • 举报
回复
1.看别人的优秀的代码
2.看书,《C++编程规范》effective系列

首先要格式清晰,风格一致
booksky508 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 nullah 的回复:]
引用 2 楼 zapdos 的回复:
偶平时不用写C++,所以很难积累
但是又想学好C++
所以希望请教一下其他人的经验..

帮顶上去
可以看看effective系列
另外林锐的高质量也可以看看
[/Quote]

不错,可以看看这些书。
本人菜鸟一个,现在正在看这几本书,收获不少。
zapdos 2009-01-21
  • 打赏
  • 举报
回复
恩,看了下<高质量C++编程指南>似乎不错,谢了哈,结贴
shyli 2009-01-21
  • 打赏
  • 举报
回复
你main函数里对map赋值时,可以用for循环呀。
用两个数组分别保存key和value。
OenAuth.Core 2009-01-21
  • 打赏
  • 举报
回复
看一下《高质量C++编程指南》有介绍
tanmeining 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 nullah 的回复:]
引用 2 楼 zapdos 的回复:
偶平时不用写C++,所以很难积累
但是又想学好C++
所以希望请教一下其他人的经验..

帮顶上去
可以看看effective系列
另外林锐的高质量也可以看看
[/Quote]

或许看看代码大全会有更大的进步...
taodm 2009-01-21
  • 打赏
  • 举报
回复
想学好C++就买本C++ Primer,书上的习题一条条都编译运行出来。
还没会走就别学跑。
tanmeining 2009-01-21
  • 打赏
  • 举报
回复
把类的声明和定义分开吧,这样看到好累,不利于查Bug,同时也不美观
nullah 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zapdos 的回复:]
偶平时不用写C++,所以很难积累
但是又想学好C++
所以希望请教一下其他人的经验..
[/Quote]
帮顶上去
可以看看effective系列
另外林锐的高质量也可以看看
yellowhwb 2009-01-21
  • 打赏
  • 举报
回复
有自己的风格就好,代码要体现出人的个性,否则,就是印度人写的代码,格式都一样,多没意思啊!
nullah 2009-01-21
  • 打赏
  • 举报
回复
楼主函数名最好还是要能表达出函数的功能三
你的f1 f2 呵呵
zapdos 2009-01-21
  • 打赏
  • 举报
回复
偶平时不用写C++,所以很难积累
但是又想学好C++
所以希望请教一下其他人的经验..
nullah 2009-01-21
  • 打赏
  • 举报
回复
这些还是慢慢垒代码中积累的经验吧

64,649

社区成员

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

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