Справочное руководство по C++ - Бьярн Страустрап
Шрифт:
Интервал:
Закладка:
int size() { return sz; }
void set_size(int);
int& operator[](int);
int& elem(int i) { return v[i]; }
};
vector::vector(int s)
{
if (s‹=0) error("bad vector size");
sz = s;
v = new int[s];
}
int& vector::operator[](int i)
{
if (i‹0 || sz‹=i) error("vector index out of range");
return v[i];
}
vector::~vector()
{
delete v;
}
// 1.14
class Vec: public vector {
public:
Vec(int s): (s) {}
Vec(Vec&);
~Vec() {}
void operator=(Vec&);
void operator*=(Vec&);
void operator*=(int);
};
Vec::Vec(Vec& a): (a.size())
{
int sz = a.size();
for (int i = 0; i‹sz; i++) elem(i) =a.elem(i);
}
void Vec::operator=(Vec& a)
{
int s = size();
if (s != a.size()) error("bad vector size for =");
for (int i =0; i‹s; i++) elem(i)=a.elem(i);
}
Vec operator+(Vec& a, Vec& b)
{
int s = a.size();
if (s!= b.size()) error("bad vector size for +");
Vec sum(s);
for (int i=0; i‹s; i++)
sum.elem(i) = a.elem(i) + b.elem(i);
return sum;
}
void error(char* p)
{
cerr ‹‹ p ‹‹ "n";
exit (1);
}
void vector::set_size(int) {}
main()
{
Vec a(10);
Vec b(10);
for (int i=0; i‹a.size(); i++) a[i] = i;
b = a;
Vec c = a+b;
for (i=0; i‹c.size(); i++) cout ‹‹ c[i] ‹‹ "n";
}
b1__16.cxx
#include ‹vector.hxx›
declare(vector,int);
implement(vector,int);
main()
{
vector(int) vv(10);
vv[2] = 3;
vv[10] = 4; // range error
}
b2_1_3.cxx
#include ‹stream.hxx›
int a = 1;
void f()
{
int b = 1;
static int c = 1;
cout ‹‹ " a = " ‹‹ a++
‹‹ " b = " ‹‹ b++
‹‹ " c = " ‹‹ c++ ‹‹ "n";
}
main ()
{
while (a ‹ 4) f();
}
b2_3.cxx
#include ‹stream.hxx›
main()
{
int* p = new int;
cout ‹‹ "sizeof(int) = " ‹‹ sizeof(int) "n";
}
b2_3_6a.cxx
#include ‹stream.hxx›
extern int strlen(char*);
char alpha[] = "abcdefghijklmnopqrstuvwxyz";
main ()
{
int sz = strlen(alpha);
for (int i=0; i‹sz; i++) {
char ch = alpha[i];
cout ‹‹ "'" ‹‹ chr(ch) ‹‹ "'"
‹‹ " = " ‹‹ ch
‹‹ " = 0" ‹‹ oct(ch)
‹‹ " = 0x" ‹‹ hex(ch) ‹‹ "n";
}
}
b2_3_6b.cxx
#include ‹stream.hxx›
char v[2][5] = {
'a', 'b', 'c', 'd', 'e',
'0', '1', '2', '3', '4'
};
main() {
for (int i = 0; i‹2; i++) {
for (int j = 0; j ‹5; j++)
cout ‹‹ "v[" ‹‹ i ‹‹ "][" ‹‹ j
‹‹ "]=" ‹‹ chr(v[i][j]) ‹‹ " ";
cout ‹‹ "n";
}
}
b2_3_7.cxx
#include ‹stream.hxx›
main()
{
char cv[10];
int iv[10];
char* pc = cv;
int* pi = iv;
cout ‹‹ "char* " ‹‹ long(pc+1)-long(pc) ‹‹ "n";
cout ‹‹ "int* " ‹‹ long(pi+1)-long(pi) ‹‹ "n";
}
b2_3__10.cxx
#include ‹stream.hxx›
struct pair {
char* name;
int val;
};
extern int strlen(char*);
extern int strcpy(char*, char*);
extern int strcmp(char*, char*);
const large = 1024;
static pair vec[large];
pair* find(char* p)
{
for (int i=0; vec[i].name; i++)
if (strcmp(p,vec[i].name)==0) return &vec[i];
if (i == large) return &vec[large-1];
return &vec[i];
}
int& value(char* p)
{
pair* res = find(p);
if (res-›name == 0) {
res-›name = new char[strlen(p)+1];
strcpy(res-›name,p);
res-›val = 0;
}
return res-›val;
}
const MAX = 256;
main ()
{
char buf [MAX];
while (cin››buf) value(buf)++;
for (int i=0; vec[i].name; i++)
cout ‹‹ vec[i].name ‹‹ ":" ‹‹ vec[i].val ‹‹ "n";
}
b3_1all.cxx
#include ‹xstream.hxx›
#include ‹ctype.h›
enum token_value {
NAME, NUMBER, END,
PLUS = '+', MINUS = '-', MUL='*', DIV='/',
PRINT=';', ASSIGN='=', LP='(', RP=')'
};
token_value curr_tok;
struct name {
char* string;
name* next;
double value;
};
const TBLSZ = 23;
name* table[TBLSZ];
int no_of_errors;
double error(char* s) {
cerr ‹‹ "error: " ‹‹ s ‹‹ "n";
no_of_errors++;
return 1;
}
extern int strlen(const char*);
extern int strcmp(const char*, const char*);
extern char* strcpy(char*, const char*);
name* look(char* p, int ins = 0)
{
int ii= 0;
char *pp = p;
while (*pp) ii = ii‹‹1 ^ *pp++;
if (ii ‹ 0) ii = -ii;
ii %= TBLSZ;
for (name* n=table [ii]; n; n=n-›next)
if (strcmp(p,n-›string) == 0) return n;
if (ins == 0) error("name not found");
name* nn = new name;
nn-›string = new char[strlen(p) + 1];
strcpy(nn-›string,p);
nn-›value = 1;
nn-›next = table[ii];
table[ii] = nn;
return nn;
}
inline name* insert(char* s) { return look (s,1); }
token_value get_token();
double term();
double expr()
{
double left = term();
for (;;)
switch (curr_tok) {
case PLUS:
get_token();
left += term();
break;
case MINUS:
get_token();
left -= term();
break;
default:
return left;
}
}
double prim();
double term()
{
double left = prim();
for (;;)
switch (curr_tok) {
case MUL:
get_token();
left *= prim();
break;
case DIV:
get_token();
double d = prim();
if (d == 0) return error("divide by 0");
left /= d;
break;
default:
return left;
}
}
int number_value;
char name_string[80];
double prim()
{
switch (curr_tok) {
case NUMBER:
get_token();
return number_value;
case NAME:
if (get_token() == ASSIGN) {
name* n = insert(name_string);
get_token();
n-›value = expr();
return n-›value;
}
return look(name_string)-›value;
case MINUS:
get_token();
return -prim();
case LP:
get_token();
double e = expr();
if (curr_tok != RP) return error(") expected");
get_token();
return e;
case END:
return 1;
default:
return error ("primary expected");
}
}
token_value get_token()
{
char ch = 0;
do {
if (!cin.get(ch)) return curr_tok = END;
} while (ch !='n' && isspace(ch));
switch (ch) {
case ';':
case 'n':
cin ›› WS;
return curr_tok=PRINT;
case '*':
case '/':
case '+':
case '-':
case '(':
case ')':
case '=':
return curr_tok=ch;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '.':
cin.putback(ch);
cin ›› number_value;
return curr_tok=NUMBER;
default:
if (isalpha(ch)) {
char* p = name_string;
*p++ = ch;
while (cin.get(ch) && isalnum(ch)) *p++ = ch;
cin.putback(ch);
*p = 0;
return curr_tok=NAME;
}
error ("bad token");
return curr_tok=PRINT;
}
}
int main(int argc, char* argv[])
{
switch (argc) {