Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Extract the filename from a URL

This ECMAScript extracts the filename from a URL. eg. the filename 'index4.svg' would be extracted from the document.URL with the value 'http://rorbuilder.info/r/whiteboardqueue/index4.svg'.
var regexp = /(\w|[-.])+$/
str = document.URL
a = regexp.exec(str)
alert(a[0])

Reference: Regular Expressions: Methods - Doc JavaScript [webreference.com]

Calculate Last Day of Last Month

VB/VBA/VB.NET one-liner to calculate the end of last month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.


DateAdd("d", -1.0 * DatePart("d", Today), Today)

Calculate First Day of Current Month

VB/VBA/VB.NET one-liner to calculate the start of the current month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.

DateAdd("D", -1.0 * DatePart("D", Today) + 1, Today)

Calculate First Day of Last Month

VB/VBA/VB.NET one-liner to calculate the start of the previous month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.

DateAdd("D", -1.0 * DatePart("D", Today) + 1, DateAdd("m", -1, Today))

Simple integer expression interpreter

This is a simple integer expression interpreter.

#include <iostream>
#include <string>
#include <map>
#include <cctype>

char look;
std::map<std::string, int> table;

void getChar() {
    look = std::cin.get();
}

void error(const std::string &s) {
    std::cout << std::endl;
    std::cout << "Error: " << s << "." << std::endl;
}

void abort(const std::string &s) {
    error(s);
    exit(1);
}

void expected(const std::string &s) {
    abort(s + " Expected");
}

bool isAlpha(char c) {
    return isalpha(c);
}

bool isDigit(char c) {
    return isdigit(c);
}

bool isAlNum(char c) {
    return isalnum(c);
}

bool isAddop(char c) {
    return ((c == '+') || (c == '-'));
}

bool isMulop(char c) {
    return (('*' == c) || ('/' == c));
}

bool isWhite(char c) {
    return ((' ' == c) || ('\t' == c));
}

void skipWhite() {
    while (isWhite(look))
        getChar();
}

void match(char x) {
    if (look != x)
        expected(std::string("\"") + x + "\"");
    else {
        getChar();
        skipWhite();
    }
}

void emit(const std::string &s) {
    std::cout << "\t"  << s;
}

void emitLn(const std::string &s) {
    emit(s);
    std::cout << std::endl;
}

void newLine() {
    if (look == '\n')
        getChar();
}

std::string getName() {
    if (!isAlpha(look))
        expected("Name");

    std::string name;
    while (isAlNum(look)) {
        name += look;
        getChar();
    }
    skipWhite();

    return name;
}

int getNum() {
    int value(0);

    if (!isDigit(look))
        expected("Integer");

    while (isDigit(look)) {
        value = value * 10 + look - '0';
        getChar();
    }
    skipWhite();

    return value;
}

int expression();

int factor() {
    int ret;

    if (look == '(') {
        match('(');
        ret = expression();
        match(')');
    } else if (isAlpha(look)) {
        std::string name = getName();
        if (table.count(name) == 0)
            table.insert(std::pair<std::string, int>(name, 0));
        ret = table[name];
    } else
        ret = getNum();

    return ret;
}

int term() {
    int value = factor();

    while (isMulop(look)) {
        if (look == '*') {
            match('*');
            value *= factor();
        } else if (look == '/') {
            match('/');
            value /= factor();
        }
    }

    return value;
}

int expression() {
    int value(0);

    if (!isAddop(look))
        value = term();

    while (isAddop(look)) {
        if (look == '+') {
            match('+');
            value += term();
        } else if (look == '-') {
            match('-');
            value -= term();
        }
    }

    return value;
}

void assignment() {
    std::string name = getName();
    match('=');
    if (table.count(name) == 1)
        table[name] = expression();
    else
        table.insert(std::pair<std::string, int>(name, expression()));

    std::cout << name << " = " << table[name] << std::endl;
}

void init() {
    getChar();
    skipWhite();
}

int main(int argc, char *argv[]) {
    init();

    do {
        assignment();
        newLine();
    } while ((look != '.') && (look != '\n'));

    return 0;
}

Expression tree builder

EvalExp(s) turns the string s into an expression tree and returns it.

Inorder, preorder and postorder can then be used to turn transform the tree into inorder, preorder or postorder expressions.

function inorder(T) {
  if ((T.cargo  == '+') || (T.cargo  == '-') || (T.cargo  == '*') || (T.cargo  == '/'))
    return [].concat(inorder(T.left)).concat(T.cargo).concat(inorder(T.right));

  return new Array(T.cargo);
}

function preorder(T) {
  if ((T.cargo  == '+') || (T.cargo  == '-') || (T.cargo  == '*') || (T.cargo  == '/'))
    return [].concat(T.cargo).concat(preorder(T.left)).concat(preorder(T.right));

  return new Array(T.cargo);
}

function postorder(T) {
  if ((T.cargo  == '+') || (T.cargo  == '-') || (T.cargo  == '*') || (T.cargo  == '/'))
    return [].concat(postorder(T.left)).concat(postorder(T.right)).concat(T.cargo);

  return new Array(T.cargo);
}

function Tree(cargo, left, right) {
  this.cargo = cargo;
  this.left = left;
  this.right = right;
}

function split(s) {
  var r = [];
  var cur = '';
  for (var i = 0; i < s.length; ++i) {
    var c = s.charAt(i);
    if ((c == '+') || (c == '-') || (c == '*') || (c == '/') || (c == '(') || (c == ')')) {
      cur.replace(" ", "");
      if (cur.length > 0) {
        r.push(cur);
        cur = '';
      }
      r.push(c);
    } else {
      cur += c;
    }
  }
  if (cur.length > 0)
    r.push(cur);

  return r;
}

function getToken(tokens, expected) {
  if (tokens[0] == expected) {
    tokens.splice(0, 1);
    return true;
  }
    return false;
}

function getVar(tokens) {
  if (getToken(tokens, '(')) {
    var a = getSum(tokens);
    getToken(tokens, ')');

    return a;
  } else {
    var aux = tokens[0];
    tokens.splice(0, 1);

    return new Tree(aux, undefined, undefined);
  }
}

function getProduct(tokens) {
  var a = getVar(tokens);

  if (getToken(tokens, '*')) {
    var b = getProduct(tokens);

    return new Tree('*', a, b);
  } else if (getToken(tokens, '/')) {
    var b = getProduct(tokens);

    return new Tree('/', a, b);
  }

  return a;
}

function getSum(tokens) {
  var a = getProduct(tokens);

  if (getToken(tokens, '+')) {
    var b = getSum(tokens);

    return new Tree('+', a, b);
  } else if (getToken(tokens, '-')) {
    var b = getSum(tokens);

    return new Tree('-', a, b);
  }

  return a;
}

function evalExp(s) {
  var tokens = split(s);

  //alert(tokens);

  return t = getSum(tokens);
}
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS