[课程相关]homework-06

一、c++11

lambda

/* * File: main.cpp * Author: liangjie * * Created on 2013年11月23日, 下午12:02 */ #include <cstdlib> #include <iostream>



using namespace std; /* * */

int main(int argc, char** argv) { string aa = "Hello World!"; int b = 0, c = 0; for_each(aa.begin(), aa.end(), [&b, &c](char a){if (a == 'l') {b++;} else if (a == 'e'){c++;};}); cout<< "Number of e: " << c<<endl; cout<<"Number of l: "<<b<<endl; return 0; }

lambda,看了一下就搞懂了。

 

smart_ptr

/* * File: main.cpp * Author: liangjie * * Created on 2013年11月23日, 下午12:16 */ #include <cstdlib> #include <iostream> #include <memory>



using namespace std; /* * */



void move_print(int n){ unique_ptr<string> aa(new string("Hello World!")); for (int i=aa.length() - n; i<aa.length();i++){ cout<<aa[i]; } for (int i=0;i<aa.length()-n;i++){ cout<<aa[i]; } } int main(int argc, char** argv) { int n = 1; move_print(n); return 0; }

smart_prt,自动清理申请的空间。概念不错。

 

二、围棋程序

01年就有C#了?搜了一下居然是00年发布的。我第一次听说c#大概是11年吧。原来也不是一个很新的语言了。

 

playPrev函数

public void playPrev(GoMove gm) { Point thepoint = gm.Point; Grid[thepoint.X,thepoint.Y].removeStone(); if (gm.DeadGroup) { int thecount = gm.DeadGroup.Count; thecount = gm.DeadGroup.Capacity; System.Collections.IEnumerator theenum = gm.DeadGroup.GetEnumerator(); while (theenum.MoveNext()) { thepoint = (Point)theenum.Current; Grid[thepoint.X,thepoint.Y].setStone(gm.DeadGroupColor); } } m_gmLastMove = gameTree.peekPrev(); if (m_gmLastMove) { repaintOneSpotNow(m_gmLastMove.Point); setLabelsOnBoard(m_gmLastMove); setMarksOnBoard(m_gmLastMove); } optRepaint(); m_colorToPlay = nextTurn(m_colorToPlay); textBox1.Clear(); if (m_gmLastMove) textBox1.AppendText(m_gmLastMove.Comment); }

编码风格:

个人很少用c系的语言,所以说不上什么好坏。不过单从代码角度来说的话,变量名还可以,能传递一部分的意思。不过注释感觉有些过多,很多时候变量名以及代码结构已经足以表达清楚意思了。在我的观点里,一个函数最多只需要一行注释,说明函数的用途及用法。至于函数内部的实现应该靠代码就足以表述清楚——如果表述不清楚,那就是函数写的有问题。

一大串if else我觉得switch比较好。当然我不是用c的,貌似switch只能处理字符?那还不如叫switch_only_for_char。

 

程序架构:

1500多行,实在没有太多耐心来看。想必其中大部分代码都是画界面的。

MVC的话,感觉并没有很明确的体现出来。各种函数的调用好像并没有什么明显的规则,比如nextTurn()这种,如果按照MVC的话,应该是不能直接调用的。

不过话说话来,MVC还是用在大型程序上比较好,这种小程序完全没必要。大炮大蚊子明显没有用手打有效。

 

错误处理:

trycatch好像有点太笼统了。如果能细致一点指出具体的错误就更好了。

特殊情况最好归到trycatch里,统一管理。写到外面容易被当做正常情况来看待。

 

 注释:

加到代码里了,太长就不贴到这了。

你可能感兴趣的:(home)