Справочное руководство по C++ - Бьярн Страустрап
Шрифт:
Интервал:
Закладка:
switch (argc) {
case 1:
break;
case 2:
cin = *new istream(strlen(argv[1]),argv[1]);
break;
default:
error("too many arguments");
return 1;
}
// insert predefined names:
insert("pi")-›value = 3.1415926535897932385;
insert("e")-›value = 2.7182818284590452354;
while (1) {
get_token();
if (curr_tok == END) break;
if (curr_tok == PRINT) continue;
cout ‹‹ expr() ‹‹ "n";
}
return no_of_errors;
}
b3_2_6a.cxx
extern void strcpy(char *,char *);
extern void exit(int);
extern int strlen(char *);
char *save_string(char* p)
{
char* s = new char[strlen(p)+1];
strcpy(s,p);
return s;
}
int main (int argc, char* argv[])
{
if (argc ‹ 2) exit(1);
int size = strlen(argv[1])+1;
char* p = save_string (argv[1]);
delete[size] p;
}
b3_2_6b.cxx
#include ‹stream.hxx›
extern void exit(int);
void out_of_store()
{
cout ‹‹ "operator new failed: out of storen";
exit(1);
}
typedef void (*PF)();
extern PF set_new_handler(PF);
main()
{
set_new_handler(&out_of_store);
char *p = new char[100000000];
cout ‹‹ "done, p = " ‹‹ long(p) ‹‹ "n";
}
b4_6_8.cxx
// This version of the program does not assume sizeof(int) == sizeof(char*)!
#include ‹stream.hxx›
#include ‹stdarg.hxx›
extern void exit(int);
void error (int …);
main(int argc, char* argv[])
{
switch (argc) {
case 1:
error(0,argv[0],(char*)0);
break;
case 2:
error(0,argv[0],argv[1],(char*)0);
break;
default:
error(1,"with",dec(argc-1),"arguments",(char*)0);
}
}
void error(int n …)
{
va_list ap;
va_start(ap,n);
for (;;) {
char *p = va_arg(ap,char*);
if (p == 0) break;
cerr ‹‹ p ‹‹ " ";
}
va_end(ap);
cerr ‹‹ "n";
if (n) exit(n);
}
b4_6_9.cxx
#include ‹stream.hxx›
struct user {
char *name;
char* id;
int dept;
};
typedef user* Puser;
user heads[] = {
"Mcilroy M.D", "doug", 11271,
"Aho A.V.", "ava", 11272,
"Weinberger P.J.", "pjw", 11273,
"Schryer N.L.", "nls", 11274,
"Schryer N.L.", "nls", 11275,
"Kernighan B.W.", "bwk", 11276
};
typedef int (*CFT)(char*,char*);
void sort(char* base, unsigned n, int sz, CFT cmp)
{
for (int i=0; i‹n-1; i++)
for (int j=n-1; i‹j; j--) {
char* pj = base+j*sz;
char *pj1 = pj-sz;
if ((*cmp)(pj,pj1) ‹ 0)
// swap b[j] and b[j-1]
for (int k=0; k‹sz; k++) {
char temp = pj[k];
pj[k] = pj1[k];
pj1[k] = temp;
}
}
}
void print_id(Puser v, int n)
{
for (int i=0; i‹n; i++)
cout ‹‹ v[i].name ‹‹ "t"
‹‹ v[i].id ‹‹ "t"
‹‹ v[i].dept ‹‹ "n";
}
extern int strcmp(char*, char*);
int cmp1(char* p, char* q)
{
return strcmp(Puser(p)-›name, Puser(q)-›name);
}
int cmp2(char* p, char* q)
{
return Puser(p)-›dept - Puser(q)-›dept;
}
main ()
{
sort((char*)heads,6,sizeof(user),cmp1);
print_id(heads,6);
cout ‹‹ "n";
sort ((char*)heads,6,sizeof(user),cmp2);
print_id(heads,6); // in department number order
}
b5_3_2.cxx
#include ‹stream.hxx›
class intset {
int cursize, maxsize;
int *x;
public:
intset(int m, int n);
~intset();
int member(int t);
void insert(int t);
void iterate(int& i) { i = 0; }
int ok(int& i) { return i‹cursize; }
int next(int& i) { return x[i++]; }
};
extern void exit (int);
void error(char *s)
{
cout ‹‹ "set: " ‹‹ s ‹‹ "n";
exit(1);
}
extern int atoi(char *);
extern int rand();
int randint (int u) // in the range 1…u
{
int r = rand();
if (r ‹ 0) r = -r;
return 1 + r%u;
}
intset::intset(int m, int n)
{
if (m‹1 || n‹m) error("illegal intset size");
cursize = 0;
maxsize = m;
x = new int[maxsize];
}
intset::~intset()
{
delete x;
}
void intset::insert(int t)
{
if (++cursize › maxsize) error("too many elements");
int i = cursize-1;
x[i] = t;
while (i›0 && x[i-1]›x[i]) {
int t = x[i];
x[i] = x[i-1];
x[i-1] = t;
i--;
}
}
int intset::member(int t)
{
int l = 0;
int u = cursize-1;
int m =0;
while (l ‹= u) {
m = (l+u)/2;
if (t ‹ x[m])
u = m-1;
else if (t › x[m])
l = m+1;
else
return 1; // found
}
return 0; // not found
}
void print_in_order(intset* set)
{
int var;
set-›iterate(var);
while (set-›ok(var)) cout ‹‹ set-›next(var) ‹‹ "n";
}
main (int argc, char *argv[])
{
if (argc!= 3) error("two arguments expected");
int count = 0;
int m = atoi(argv[1]);
int n = atoi (argv[2]);
intset s(m,n);
int t = 0;
while (count ‹m) {
t = randint(n);
if (s.member(t)==0) {
s.insert(t);
count++;
}
}
print_in_order(&s);
}
b5_4_5.cxx
#include ‹stream.hxx›
struct cl
{
char* val;
void print(int x) { cout ‹‹ val ‹‹ x ‹‹ "n"; }
cl(char *v) { val = v; }
};
typedef void (cl::*PROC)(int);
main()
{
cl z1("z1 ");
cl z2("z2 ");
PROC pf1 = &cl::print;
PROC pf2 = &cl::print;
z1.print(1);
(z1.*pf1)(2);
z2.print(3);
((&z2)-›*pf2)(4);
}
b5_5_3.cxx
main() {
char *p = new char[100];
char *q = new char[100];
delete p;
delete p;
}
b6_3_2.cxx
#include "stream.hxx"
int error (char * p)
{
cout ‹‹ p ‹‹ "n";
return 1;
}
class tiny {
char v;
tiny assign(int i)
{v = (i&~63) ? (error("range error"),0) : i; return *this; }
public:
tiny (int i) { assign(i); }
tiny (tiny& t) { v = t.v; }
tiny operator=(tiny& t1) { v = t1.v; return *this; }
tiny operator=(int i) { return assign(i); }
int operator int() { return v; }
};
void main()
{
tiny c1 = 2;
tiny c2 = 62;
tiny c3 = (c2 - c1);
tiny c4 = c3;
int i = (c1 + c2);
c1 = (c2 + (2 * c1));
c2 = c1 - i;
c3 = c2;
}
b6_6.cxx
#include ‹stream.hxx›
extern int strcpy(char*, char*);
extern int strlen(char *);
struct string {
char *p;
int size;
inline string(int sz) { p = new char[size=sz]; }
string(char *);
inline ~string() { delete p; }
void operator=(string&);
string(string&);
};
string::string(char* s)
{
p = new char [size = strlen(s) + 1];
strcpy (p,s);
}
void string::operator=(string& a)
{
if (this == &a) return;
delete p;
p=new char[size=a.size];
strcpy(p,a.p);
}
string::string(string& a)
{
p=new char[size=a.size];
strcpy(p,a.p);
}
string g(string arg)
{
return arg;
}
main()
{
string s = "asdf";
s = g(s);
cout ‹‹ s.p ‹‹ "n";
}
b6_7.cxx
#include ‹stream.hxx›
#include ‹string.h›