effector.cpp
来自「This is the second part of that lab manu」· C++ 代码 · 共 55 行
CPP
55 行
//: C05:Effector.cpp
// From "Thinking in C++, 2nd Edition, Volume 2"
// by Bruce Eckel & Chuck Allison, (c) 2001 MindView, Inc.
// Available at www.BruceEckel.com.
// Jerry Schwarz's "effectors"
//{L} ../TestSuite/Test
#include <iostream>
#include <cstdlib>
#include <string>
#include <climits> // ULONG_MAX
using namespace std;
// Put out a portion of a string:
class Fixw {
string str;
public:
Fixw(const string& s, int width)
: str(s, 0, width) {}
friend ostream&
operator<<(ostream& os, const Fixw& fw) {
return os << fw.str;
}
};
typedef unsigned long ulong;
// Print a number in binary:
class Bin {
ulong n;
public:
Bin(ulong nn) { n = nn; }
friend
ostream& operator<<(ostream&, const Bin&);
};
ostream& operator<<(ostream& os, const Bin& b) {
ulong bit = ~(ULONG_MAX >> 1); // Top bit set
while(bit) {
os << (b.n & bit ? '1' : '0');
bit >>= 1;
}
return os;
}
int main() {
char* string =
"Things that make us happy, make us wise";
for(int i = 1; i <= strlen(string); i++)
cout << Fixw(string, i) << endl;
ulong x = 0xCAFEBABEUL;
ulong y = 0x76543210UL;
cout << "x in binary: " << Bin(x) << endl;
cout << "y in binary: " << Bin(y) << endl;
} ///:~
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?