Archive for ‘之语言特性’ Category

April 27, 2009

运行期(runtime)判断
下面的程序可以在运行期判断 endianess:

int IsBigEndian (void)
{
        static const int v =1;
        return *(char*)&v?0:1;
}
Tags: ,. 21 views
April 25, 2009

Introduction

A good way to get into an argument with a computer programmer is to attempt to explain why the language they are using is not as good as the one you are using. Most of the programmers I know are positively religious over their Operating System (see my other article), their development language and finally their text editor.

Tags: . 10 views
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool fun(unsigned n)
{
    assert(n >0); //乘方不为正
    if (!(n & (n-1))) //n的二进制只有一个1,即n==2^m;
    {
        int offset = 0;//"1"位的偏移量
        while (n != 1)
        {
            n>>=1;
            offset++;
         }
         if (offset && !(offset&1))//偏移量为偶数,即 n == 2^(2*m),亦即n == 4^m;
        {
            return true;
         }
      }
      return false;
}
Tags: . 7 views
April 23, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
 
using namespace std;
//! declaration and implementation of class Matrix
class Matrix
{
	public:
		Matrix(int n = 2);
		Matrix(const Matrix& rf);
		int& operator [][](int x, int y);//! operator[][]
		int& operator [][](int x, int y) const;//! operator[][]const
		Matrix& operator *=(Matrix& rt);//! operator *=
	private:
		int *p_Matrix;//! pointer to the 2D array
		int m_n;//! dimension of the matrix
};
Tags: ,. 8 views
April 22, 2009

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
 
using namespace std;
double Sqrt(double x);
int main()
{
	double x;
	cin>>x;
	cout<<Sqrt(x)<<endl;
	return 0;
}
 
double Sqrt(double x)
{
	const double eps = 1E-6;
	double x0 = x / 2, x1 = (x0 + x / x0) / 2;
	while(fabs(x1 - x0) > eps)
	{
		x0 = x1;
		x1 = (x0 + x / x0) / 2;
	}
	return x0;
}
Tags: ,. 8 views
April 19, 2009
1
2
3
4
5
6
/*
 *Filename:		tri_Diagonal
 *Description	a solution to the tri-diagonal linear formulation
 *author:		HouFenglin@物理0605/DUT
 *Time:			13/04/2009
 */
Tags: . 22 views
/*
 
 *Filename:		Gauss_Elimination.cpp
 
 *Description	a solution to the linear formulations
 
 *author:		HouFenglin@物理0605/DUT
 
 *Time:			13/04/2009
 
 */
Tags: . 62 views
  • type * p 表示当*p出现在一个表达式中,它表示一个type的值。Type是指针p的基类型。
  • type * p[] 表示当*p[]将被求值为一个type类型的值。
  • type **p **p表示一个type的值。
  • type (*p)[] 这个表达式则表示*p表示一个type [] 类型的值。这是因为括号有更高优先级,故求值运算符仅作用在p上,而不是p[]上。
Tags: . 47 views
1
2
3
4
5
6
int
atoi (const char *str)
{				//异常时返回零,觉得不是很合适,但又没想到什么好的方法,标准函数也是这么定义的
    //...
    return result * flag;	//确定符号并返回
}
Tags: . 47 views
April 18, 2009

Access specifiers(public, protected, and private) can be used to express and enforce higher-level constraints on how a type may be used. The most common of these techniques is to disallow copying of an object by declaring its copy operations to be private and not defining them:

Tags: . 21 views
Page 9 of 9123456789