Справочное руководство по C++
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) {
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;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.