Archive for ‘边走编程’ Category

June 24, 2009

比较下面两段代码:

1
2
3
4
for(int i = 0; i < n; i++)
{
     //~ do something;
}
1
2
3
4
for(int i = 0; i < n; ++i)
{
    //~ the same thing;
}
Tags: . 41 views
June 23, 2009
1
2
3
4
5
6
7
8
9
10
11
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <string>
#include <sstream>
 
 
#include "matrix.hpp"
#include "vector.hpp"
Tags: ,. 81 views
June 21, 2009

与其说计划,倒不如说是给自己制定一个目标和任务。
大约三周的生产实现,料定不会有什么新花样,看透了“培养计划”里面的这些课程,从来都不会有实质性的东西。sucks!但是无论如何这都是必须要完成的任务,积极面对吧。无聊的“生产实习”:I love you game!
三周,一定要把操作系统过一遍,一定能弄清楚很多以前觉得模模糊糊、似懂非懂的概念,第一遍能有一个整体的把握即可。昨天在当当定了一本《深入理解Linux内核》,经典制作,学完操作系统后再从这本书中挑出几个模块来学习,肯定受益良多!线性代数,接下来的一个月内也必须得吃透了,有很多东西确实是忘的一干二净了。

Tags: . 74 views
June 8, 2009

一个double类型的vector,没有把它实现成模板,专门用做普通的数值运算,接口定义在vector.hpp, 实现在vector.cpp,Vector类的整体放在了命名空间(namesapce)dutor内,在该命名空间外还实现了一些常用的全局的运算符。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef DUTOR_VECTOR_HPP
#define DUTOR_VECTOR_HPP
#include <iostream>
 
namespace dutor 
{
    class Vector 
    {
      public:
        Vector(int dim = 1); //构造函数,默认dim = 1
        Vector(double v0, double v1);  //~ 以两个值v0, v1初始化一个二维点 
        Vector(double v0, double v1, double v2); //~ 以三个值v0, v1, v2初始化一个三维点 
        Vector(const Vector& dv); //~ 复制构造函数
        //~ ......
Tags: ,. 40 views
June 5, 2009

这些都是对一个设计良好的类的最基本的要求,每当完成一个类的设计时,都要把这些问题都审视一遍,养成好的习惯。

  1. 构造函数
  2. 数据成员是私有的
  3. 无参数的构造函数
  4. 初始化所有数据
  5. Destructor
  6. 有可能被继承的话一定 destructor 要virtual,否则,尽量不用virtual
  7. Copy Constructor
  8. x=x 能正常工作?(自己赋值给自己)
  9. 逻辑关系运算符?
  10. Copy Constructor/operator= 使用 const reference
  11. 函数参数用reference 还是 const reference 还是 object
  12. 不修改成员的函数一定得要const,否则作为const reference 无法调用。
Tags: ,. 12 views
May 30, 2009
从单个源文件生成可执行程序

下面是一个保存在文件 helloworld.cpp 中一个简单的 C++ 程序的代码:

1
2
3
4
5
6
7
/* helloworld.cpp */
#include <iostream>
int main(int argc,char *argv[])
{
    std::cout << "hello, world\n";
    return(0);
}
Tags: ,. 2,517 views
May 25, 2009

1、 并行计算:

并行计算是指同时对多个任务或多条指令、或对多个数据项进行处理。完成此项处理的计算机系统称为并行计算机系统,它是将多个处理器通过网络连接以一定的方式有序地组织起来。

Tags: . 91 views
May 24, 2009

简介

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//! 需要包含de头文件
#include <sys/types.h>
#include <sys/stat.h>
 
int stat(const char *filename, struct stat *buf); //! prototype,原型
 
struct stat
{
    dev_t       st_dev;     /* ID of device containing file -文件所在设备的ID*/
    ino_t       st_ino;     /* inode number -inode节点号*/
    mode_t      st_mode;    /* protection -保护模式?*/
    nlink_t     st_nlink;   /* number of hard links -链向此文件的连接数(硬连接)*/
    uid_t       st_uid;     /* user ID of owner -user id*/
    gid_t       st_gid;     /* group ID of owner - group id*/
    dev_t       st_rdev;    /* device ID (if special file) -设备号,针对设备文件*/
    off_t       st_size;    /* total size, in bytes -文件大小,字节为单位*/
    blksize_t   st_blksize; /* blocksize for filesystem I/O -系统块的大小*/
    blkcnt_t    st_blocks;  /* number of blocks allocated -文件所占块数*/
    time_t      st_atime;   /* time of last access -最近存取时间*/
    time_t      st_mtime;   /* time of last modification -最近修改时间*/
    time_t      st_ctime;   /* time of last status change - */
};
Tags: ,. 1,401 views
May 23, 2009

Description:

There is a special card game that one who wins least rounds gets the money from people who win more. Suppose there are M people, including you, playing this game. First of all, each player gets N cards. The number on each card is a positive integer which is at most N*M. Additionally, the number of the cards is unique. The game consists of N rounds; for each round every player chooses one card to compare with cards from others. The player whose card with the biggest number wins the set, and then the next round begins. After N sets, when all the cards have been chosen, the player who has won the least rounds domains the game.

Tags: . 10 views
May 22, 2009

这是对C++高效编程的一个总结, 很有指导作用.

一、#include “filename.h”和#include 的区别

#include “filename.h”是指编译器将从当前工作目录上开始查找此文件
#include 是指编译器将从标准库目录中开始查找此文件

二、头文件的作用

加强安全检测
通过头文件可能方便地调用库功能,而不必关心其实现方式

三、* , &修饰符的位置

1
2
3
4
int *i,j; // better for read
 i = new int(0);
 j = 0;
 int *&y = i; // pointer's reference

对于*和&修饰符,为了避免误解,最好将修饰符紧靠变量名

四、if语句

不要将布尔变量与任何值进行比较,那会很容易出错的。
整形变量必须要有类型相同的值进行比较
浮点变量最好少比相等,可以通过求差与较小的数比较
指针变量要和NULL进行比较,不要和布尔型和整形比较

Tags: . 62 views