<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rocker &#187; Calculator</title>
	<atom:link href="http://www.shishirsharma.com/tag/calculator/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shishirsharma.com</link>
	<description>Everything bloggable around Shishir Sharma.</description>
	<lastBuildDate>Fri, 06 Jan 2012 09:47:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Desktop Calculator</title>
		<link>http://www.shishirsharma.com/2008/08/13/desktop-calculator/</link>
		<comments>http://www.shishirsharma.com/2008/08/13/desktop-calculator/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 06:38:27 +0000</pubDate>
		<dc:creator>Shishir Sharma 'criss'</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Calculator]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[GNU General Public Licence]]></category>
		<category><![CDATA[Standard C++]]></category>

		<guid isPermaLink="false">http://shishirsharma.com/?p=118</guid>
		<description><![CDATA[I always appreciate coding in standard C++. Desktop Calculator is a application coded in Standard C++. It is a application based on the example 6.1 in the book &#8220;The C++ Programming Language&#8221;, Third Edition by Bjarne Stroustrup. Desktop Calculator is &#8230; <a href="http://www.shishirsharma.com/2008/08/13/desktop-calculator/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I always appreciate coding in standard C++. Desktop Calculator is a application coded in Standard C++. It is a application based on the example 6.1 in the book &#8220;The C++ Programming Language&#8221;, Third Edition by <strong>Bjarne Stroustrup</strong>. Desktop Calculator is a GPL software.</p>
<p>To download full code click here.<br />
<span id="more-118"></span><br />
<code></p>
<pre class="brush: cpp">
/*
 *    This file is part of Desktop Calculator.
 *
 *    Desktop Calculator is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Desktop Calculator is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 * Standard
 *    You should have received a copy of the GNU General Public License
 *    along with Desktop Calculator.  If not, see < http://www.gnu.org/licenses/ >.
 *
 *	   Shishir Sharma criss < mail AT DOMAIN shishirsharma DOT com >
 */
//////////////////////////////////////////////////////////
//                                                      //
//  File name   :   dc                                  //
//  Comments    :   desk calculator                     //
//  Versions    :   Saturday, September 23, 2006        //
//  Coded by    :   Shishir Sharma 'C R I I S'          //
//                  mail.mastermind@gmail.com           //
//                                                      //
//////////////////////////////////////////////////////////

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

using namespace std;

enum Token_value
{
  FUNCTION, NAME, NUMBER, END,
  PLUS = '+', MINUS = '-', MUL = '*', DIV = '/',
  PRINT = ';', ASSIGIN = '=', LP = '(', RP = ')',
  EXPO = '^', MLP = '[', MRP = ']', COMMA = ','
};

Token_value curr_tok = PRINT;

double number_value;
string string_value;
int no_of_errors;

map < string, double >table;

double error (const string &#038; s);
double message (const string &#038; s);
Token_value get_token ();
double prim (bool get);
double term (bool get);
double expr (bool get);

const char help[] = "DC: Desk Calculetor\n\npress ctrl+Z to quit\n";

int
main (int argc, char *argv[])
{
  // Standard variables
  table["pi"] = 3.1415926535897932385;
  table["e"] = 2.7182818284590452354;
  // Default help message
  message (help);

  // Main loop
  while (cin) {
    get_token ();
    if (curr_tok == END)
      break;
    if (curr_tok == PRINT)
      continue;
    cout < < expr (false) << '\n';
  }

  return no_of_errors;
}

double
message (const string &#038; s)
{
  cout << s << '\n';
  return 0;
}

double
error (const string &#038; s)
{
  no_of_errors++;
  cerr << "Error:" << s << '\n';
  return 1;
}

double
expr (bool get)
{
  double left = term (get);
  for (;;)
    switch (curr_tok) {
    case PLUS:
      {
	left += term (true);
	break;
      }
    case MINUS:
      {
	left -= term (true);
	break;
      }
    default:
      return left;
    }
}

double
term (bool get)
{
  double left = prim (get);
  for (;;)
    switch (curr_tok) {
    case MUL:
      {
	left *= term (true);
	break;
      }
    case DIV:
      {
	if (double d = prim (true)) {
	  left /= d;
	  break;
	}
	return error ("divide by 0");
      }
    case EXPO:
      {
	left = pow (left, prim (true));
	break;
      }
    default:
      return left;
    }
}

double
prim (bool get)
{
  if (get)
    get_token ();

  switch (curr_tok) {
  case MINUS:
    {
      return (-expr (true));
    }
  case NUMBER:
    {
      double v = number_value;
      get_token ();
      return v;
    }
  case NAME:
    {
      //help
      if (string_value == "help" || string_value == "Help"
	  || string_value == "HELP") {
	return message (help);
      }
      else if (string_value == "clear" || string_value == "Clear"
	       || string_value == "CLEAR") {
	cout.flush ();
	return message (help);
      }
      else
	//trignometry functions
      if (string_value == "sin") {
	if (get_token () == LP)
	  return sin (expr (true));
      }
      else if (string_value == "cos") {
	if (get_token () == LP)
	  return cos (expr (true));
      }
      else if (string_value == "tan") {
	if (get_token () == LP)
	  return tan (expr (true));
      }
      else if (string_value == "asin") {
	if (get_token () == LP)
	  return asin (expr (true));
      }
      else if (string_value == "acos") {
	if (get_token () == LP)
	  return acos (expr (true));
      }
      else if (string_value == "atan") {
	if (get_token () == LP)
	  return atan (expr (true));
      }
      // exponential functions
      else if (string_value == "exp") {
	if (get_token () == LP)
	  return exp (expr (true));
      }
      else if (string_value == "log") {
	if (get_token () == LP)
	  return log (expr (true));
      }
      else if (string_value == "log10") {
	if (get_token () == LP)
	  return log10 (expr (true));
      }
      else if (string_value == "sum") {
	get_token ();
	if (curr_tok == LP) {
	  double sum = 0;
	  sum += expr (true);
	  while (curr_tok != RP) {
	    if (curr_tok == COMMA)
	      sum += expr (true);
	  }
	  return sum;
	}
      }
      // new variables
      else {
	double &#038;v = table[string_value];
	if (get_token () == ASSIGIN)
	  v = expr (true);
	return v;
      }
    }
  case LP:
    {
      double e = expr (true);
      if (curr_tok != RP)
	return error ("')' expected");
      get_token ();
      return e;
    }
  default:
    return error ("primary expected");
  }
}

Token_value
get_token ()
{
  char ch;

  do {
    if (!cin.get (ch))
      return curr_tok = END;
  } while (ch != '\n' &#038;&#038; isspace (ch));

  switch (ch) {
  case '\n':
  case ';':
    return curr_tok = PRINT;
  case '^':
  case '*':
  case '/':
  case '+':
  case '-':
  case '(':
  case ')':
  case '[':
  case ']':
  case ',':
  case '=':
    return curr_tok = Token_value (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)) {
      string_value = ch;
      while (cin.get (ch) &#038;&#038; isalnum (ch))
	string_value.push_back (ch);
      cin.putback (ch);
      return curr_tok = NAME;
    }
    error ("bad token");
    return curr_tok = PRINT;
  }
}

/* eof */
</cmath></cctype></string></iostream></pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shishirsharma.com/2008/08/13/desktop-calculator/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

