运行期(runtime)判断
下面的程序可以在运行期判断 endianess:
int IsBigEndian (void) { static const int v =1; return *(char*)&v?0:1; } |
熟读而精思,循序而渐进,厚积而薄发。
运行期(runtime)判断
下面的程序可以在运行期判断 endianess:
int IsBigEndian (void) { static const int v =1; return *(char*)&v?0:1; } |
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.
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; } |
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 }; |
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; } |
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
*/ |
/*
*Filename: Gauss_Elimination.cpp
*Description a solution to the linear formulations
*author: HouFenglin@物理0605/DUT
*Time: 13/04/2009
*/ |
1 2 3 4 5 6 | int atoi (const char *str) { //异常时返回零,觉得不是很合适,但又没想到什么好的方法,标准函数也是这么定义的 //... return result * flag; //确定符号并返回 } |
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: