x = 1 y = 1 z = x + y
x = 1 y = 1 z = x + y x = 1 y = 1 z = x + y
x = 1000000000000000000000000
x = 1000000000000000000000000 y = 1000000000000000000000000 z = x + y
200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 | 1000 | 1100 | 1300 | 1900 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | |||||||||||
10 | 1 | 1 | ||||||||||
20 | 1 | 1 | ||||||||||
30 | 1 | 4 | 1 | |||||||||
40 | 1 | 1 | 1 | 1 | ||||||||
50 | 1 | |||||||||||
60 | 1 | |||||||||||
70 | 1 | |||||||||||
80 | 1 | 1 | 1 | |||||||||
90 | 1 | 1 | ||||||||||
Totals | 1 | 3 | 6 | 2 | 1 | 1 | 1 | 3 | 1 | 1 | 2 | 1 |
Statistics derived from all files ending in .cc
, .cpp
,
.CC
, .C
, and .h
.
000 | 100 | 200 | 300 | 400 | 500 | 600 | |
---|---|---|---|---|---|---|---|
0 | 1 | ||||||
10 | 1 | ||||||
20 | 1 | 2 | 2 | ||||
30 | 1 | 1 | 1 | ||||
40 | |||||||
50 | |||||||
60 | 1 | ||||||
70 | 1 | ||||||
80 | 1 | 1 | 1 | 1 | |||
90 | 3 | 1 | 2 | 1 | |||
Totals | 1 | 8 | 4 | 2 | 4 | 3 | 1 |
Statistics derived from all files ending in .cc
, .cpp
,
.CC
, .C
, and .h
.
total | pages per procedure | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
procs | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
3 | 2 | 1 | ||||||||||
4 | 3 | 1 | ||||||||||
6 | 1 | 1 | 1 | 1 | 1 | 1 | ||||||
8 | 4 | 1 | 2 | 1 | ||||||||
9 | 7 | 1 | 1 | |||||||||
10 | 7 | 1 | 2 | |||||||||
10 | 8 | 1 | 1 | |||||||||
11 | 6 | 1 | 3 | 1 | ||||||||
12 | 11 | 1 | ||||||||||
12 | 8 | 2 | 1 | 1 | ||||||||
13 | 10 | 1 | 1 | 1 | ||||||||
13 | 6 | 1 | 5 | 1 | ||||||||
14 | 4 | 4 | 4 | 1 | 1 | |||||||
15 | 8 | 5 | 1 | 1 | ||||||||
20 | 19 | 1 | ||||||||||
24 | 15 | 8 | 1 | |||||||||
28 | 27 | 1 | ||||||||||
34 | 26 | 4 | 2 | 2 | ||||||||
Statistics derived from all files ending in .cc
, .cpp
,
.CC
, .C
, and .h
.
File Counts | Count | Average Lines/File | Avg Semicolons per File |
---|---|---|---|
1 | 9 | 484 | 217 |
2 | 3 | 341 | 147 |
3 | 6 | 277 | 120 |
4 | 1 | 108 | 34 |
5 | 1 | 168 | 83 |
6 | 1 | 173 | 82 |
7 | 2 | 233 | 88 |
Statistics derived from all files ending in .cc
, .cpp
,
.CC
, .C
, and .h
.
#include "equation-set.h" int main() { equation_set eset; while (eset.read(cin)) { eset.simplify(); eset.write(cout); cout << "\n"; } }
void main() { LList list; cout << "Please enter a sequence of sets of " << "algebraic eq. (ctrl+d) to end" << endl; int setCount = readInput(list); printResults(list, setCount); }
or
void main() { LList list; cout << "Please enter a sequence of sets of " << "algebraic eq. (ctrl+d) to end" << endl; int setCount = readInput(list); printResults(list, setCount); }
or
int main() { equation_set eset; while (eset.read(cin)) { eset.simplify(); eset.write(cout); cout << "\n"; } }
main()
doing?
main()
is correct, which is wrong?
main()
tells you more?
main()
say?
class equation_set { public: bool read(istream &); void simplify(void); void write(ostream &) const; };
class equation_set { private: typedef vector<string> eqn_set; typedef eqn_set::iterator eset_iter; typedef eqn_set::const_iterator eset_citer; eqn_set equations; };
read()
do?
bool equation_set::read(istream & is) { string l; equations.clear(); while (true) { if (!getline(is, l)) return false; if (!is_blank(l)) break; } do equations.push_back(l); while (getline(is, l) && !is_blank(l)); return true; }
write()
do?
void equation_set::write(ostream & os) const { std::copy(equations.begin(), equations.end(), ostream_iter<string>(os, "\n")); }
simplify()
do?
void equation_set::simplify(void) { }
equations.clear()
.
is_nonblank()
instead of is_blank()
.
is_blank()
tests.
class equation_set { private: typedef vector<equation> eqn_set; typedef eqn_set::iterator eset_iter; typedef eqn_set::const_iterator eset_citer; eqn_set equations; };
bool equation_set::read(istream & is) { string l; equations.clear(); while (true) { if (!getline(is, l)) return false; if (!is_blank(l)) break; } do equations.push_back(equation(l)); while (getline(is, l) && !is_blank(l)); return true; } void equation_set::write(ostream & os) const { for (eset_citer i = equations.begin(); i != equations.end(); i++) *i->write(os); }
typedef vector<equation> eqn_set; typedef eqn_set::iterator eset_iter; typedef eqn_set::const_iterator eset_citer; eqn_set equations;
or
vector<vector<string> > process_set( vector<vector<string> >, int, int); int No_solution( vector<vector<string> >, vector<vector<string> >, vector<string>); vector<vector<string> > calculation( vector<vector<string> >, vector<vector<string> >, vector<string>);
class equation { public: equation(const string &); void write(ostream &) const; };
class equation { private: string eqn; };
equation::equation(const string & str) : eqn(str) { } equation::equation(ostream & os) { os << eqn << "\n"; }
split()
doesn't work on x=7+z
.
fragment_equation() fragment_expression() check_for("=") fragment_expression() fragment_expression() fragment_value() while check_for("*-+/") fragment_value() fragment_value() if !fragment_number() fragment_variable()
typedef vector<string> fragments; fragments fragment_eqn(const string & str) { fragments pieces; str_citer s = str.begin(); fragment_expr(s, str.end(), pieces); fragment_char("=", s, str.end(), pieces); fragment_expr(s, str.end(), pieces); return pieces; } void fragment_expr(s, e, pieces) { fragment_value(s, e, pieces); while (fragment_char("+-*/", s, e, pieces)) fragment_value(s, e, pieces); } void fragment_value(s, e, pieces) { if (!fragment_var(s, e, pieces)) fragment_number(s, e, pieces); } bool fragment_var(s, e, pieces) { return fragment_char("A...Za...z", s, e, pieces); } bool fragment_number(s, e, pieces) { if (!skip_space(s, e) || !isdigit(*s)) return false; str_citer n = std::find_if(s, e, is_not_digit); pieces.push_back(string(s, n)); s = n; return true; } bool fragment_char(chars, s, e, pieces) { if (!skip_space(s, e) || (index(chars, *s) == NULL)) return false; pieces.push_back(string(s, s + 1)); s++; return true; }
class equation { private: fragments eqn; }; equation::equation(const string & str) : eqn(fragment_equation(str)) { } equation::equation(ostream & os) { copy(eqn.start(), eqn.end(), ostream_iter(os, " ")); os << "\n"; }
Who cares? Just make some decisions and hide them.
class bignum { public: bignum(const string & str) : val(atoi(str.c_str())) { } bignum operator + (const bignum & bn) { return bignum(val + bn.val); } bignum operator - (const bignum & bn) { return bignum(val - bn.val); } bignum operator * (const bignum & bn) { return bignum(val * bn.val); } bignum operator / (const bignum & bn) { return bignum(val / bn.val); } private: bignum(int v) : val(v) { } int val; };
bignum
s.
...x + y * z...
,
y
?
void convertToPostfix(string infix, string & postfix) { StackS; int infixIndex = 0; S.Push('('); infix += ")"; while ( ! S.isEmpty() ) { if ( isspace(infix[infixIndex]) ) { infixIndex++; continue; } if ( isdigit(infix[infixIndex]) ) postfix += infix[infixIndex++]; else if ( infix[infixIndex] == '(') S.Push(infix[infixIndex++]); else if ( isOperator(infix[infixIndex]) ) { while ( ! S.isEmpty() && isOperator(S.Top() ) && precedence(S.Top(), infix[infixIndex]) >= 0 ) postfix += S.Pop();p S.Push(infix[infixIndex++]); } else if ( infix[infixIndex] == ')' ) { while ( S.Top() != '(' ) postfix += S.Pop(); S.Push(infix[infixIndex++]); } else if ( infix[infixIndex] == ')' ) { while ( S.Top() != '(' ) postfix += S.Pop(); infixIndex++ ; char temp = S.Pop(); } } }
class expression { public: enum type { number, variable, operation }; expression(const tkns_citer & s, const tkns_citer & e); expression(const expression &); ~expression(); expression & operator = (const expression &); void write(ostream &) const; private: type expr_t; string prep; bignum val; expression * left; expression * right; };
void expression::write(ostream & os) const { if (expr_t == operation) { left->write(os); os << " " << prep << " "; right->write(os); } else if (expr_t == number) val.write(os); else { assert(expr_t == name); os << prep; } }
class equation { private: expression lhs, rhs; }; equation::equation(const string & str) { fragments tkns = fragment(str); tkns_citer gets = std::find_if(tkns.begin(), tkns.end(), is_gets); assert((gets != tkns.begin()) && (gets != tkns.end())); lhs = expression(tkns.begin(), gets); gets++; rhs = expression(gets, tkns.end()); } void equation::write(ostream & os) { lhs.write(os); os << " = "; rhs.write(os); }
$ wc -l *[ch] 50 bignum.cc 53 bignum.h 47 equation-set.cc 44 equation-set.h 85 equation.cc 59 equation.h 114 expression.cc 69 expression.h 115 fragment.cc 20 fragment.h 12 main.cc 698 total $
fragment.cc
is the biggest; let's take a look.
is_not_digit Lines 6 is_not_space Lines 6 skip_space Lines 9 fragment_char Lines 15 fragment_number Lines 17 fragment_var Lines 10 fragment_value Lines 5 fragment_expr Lines 7 fragment Lines 12
expression.cc
's big too.
is_muldiv Lines 3 is_addsub Lines 3 expression::expression Lines 32 expression::expression Lines 14 expression::~expression Lines 4 expression::operator = Lines 14 expression::write Lines 13
expression::expression( const tkns_citer & s, const tkns_citer & e) { tkns_citer op = std::find_if(s, e, is_muldiv); if (op == e) op = std::find_if(s, e, is_addsub); if (op == e) { assert(s->size() > 0); assert(s + 1 == e); if (isdigit((*s)[0])) { expr_t = number; val = bignum(s->c_str()); } else { assert(isalpha((*s)[0])); expr_t = variable; } prep = *s; left = right = 0; } else { expr_t = operation; prep = *op; left = new expression(s, op); op++; right = new expression(op, e); } }
isalpha((*s)[0])
.
x = 100
.
x = 100 y = x + 3
substitute for x
x = 100 y = 100 + 3
and constant fold to get
x = 100 y = 103
class expression { public: bool fold_constants(void); } bool expression::fold_constants(void) { if (expr_t != operation) return false; const bool b1 = left->fold_constants(); const bool b2 = right->fold_constants(); if ((left->expr_t != number) || (right->expr_t != number)) return b1 || b2; expr_t = number; if (prep == "+") val = left->val + right->val; else if (prep == "-") val = left->val - right->val; else if (prep == "*") val = left->val*right->val; else if (prep == "/") val = left->val/right->val; else assert(!"unknown operation in expression::fold_constant()"); delete left; delete right; left = right = 0; return true; }
class equation { public: bool fold_constants(void); } bool equation::fold_constants(void) { const bool b1 = lhs->fold_constants(); const bool b2 = rhs->fold_constants(); return b1 || b2; }
class equation_set { private: bool fold_constants(void); } bool equation_set::fold_constants(void) { bool b = false; for (eset_iter i = equations.begin(); i != equations.end(); i++) b = i->fold_constants() || b; return b; } void equation_set::simplify(void) { fold_constants(); }
bool expression::simplify( const string & var, const bignum & bn) { if (expr_t == number) return false; if (expr_t == operation) { const bool b1 = left->simplify(); const bool b2 = right->simplify(); return b1 || b2; } if (var != prep) return false; expr_t = number val = bn return true; }
bool equation::is_known(void) const { if (lhs.is_type(equation_name) && rhs.is_type(equation::number)) return true; if ((lhs.type(equation::number) && (rhs.type(equation::name)) { equation t = lhs; lhs = rhs; rhs = t; return true; } return false; }
This page last modified on 15 November 2001.