This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's Calculator Programming subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. General Coding and Design => Calculator Programming
Author Message
sgm


Calc Guru


Joined: 04 Sep 2003
Posts: 1265

Posted: 24 Dec 2005 04:19:55 pm    Post subject:

I'm bored.


Code:
#include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <stack>
#include <climits>

#ifdef __DJGPP__
#include <crt0.h>

extern "C" char **__crt0_glob_function (char*) {return 0;}
extern "C" void __crt0_load_environment_file (char *) { }
extern "C" void __crt0_setup_arguments (void) { }
#endif

enum opsym_t {OP_NEG, OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_GRP};

double eval(std::string&) throw (std::string&);
void stack_eval(std::stack<std::pair<opsym_t,int>, std::list<std::pair<opsym_t,int> > >&,
  std::stack<double,std::list<double> >&);
void show_stacks(std::stack<std::pair<opsym_t,int>, std::list<std::pair<opsym_t,int> > >,
  std::stack<double,std::list<double> >);

double opf_negate(double, double);
double opf_add(double, double);
double opf_sub(double, double);
double opf_mul(double, double);
double opf_div(double, double);

const int op_precs[] = {INT_MAX, 0, 0, 1, 1, INT_MIN};
double (*op_funcs[])(double, double) = {opf_negate, opf_add, opf_sub, opf_mul, opf_div, 0};

int main()
{
   std::ios::sync_with_stdio(false);

   double result = 0.0;
   char* buf = new char [BUFSIZ];

   std::cout << "Enter an arithmetic expression: ";
   std::cin.get(buf, BUFSIZ);

   std::string str(buf);
   delete [] buf;

   try
   {
  result = eval(str);
   }
   catch (std::string& err_msg)
   {
  std::cerr << err_msg << ".\n";
  return 1;
   }

   std::cout << "= " << result << std::endl;
   std::cin.get();
   return 0;
   
}

void show_stacks(std::stack<std::pair<opsym_t,int>, std::list<std::pair<opsym_t,int> > > ops,
  std::stack<double,std::list<double> > vals)
{
#ifndef NDEBUG
   char opers[] = {'-', '+', '-', '*', '/', '('};

   std::endl(std::cerr);

   if (ops.empty())
  std::cerr << "Empty";
   else
  while (!ops.empty())
  {
     std::cerr << opers[ops.top().first] << " : ";
     ops.pop();
  }

   std::endl(std::cerr);
   
   if (vals.empty())
  std::cerr << "Empty";
   else
  while (!vals.empty())
  {
     std::cerr << vals.top() << " : ";
     vals.pop();
  }

   std::endl(std::cerr);
#endif
}

void stack_eval(std::stack<std::pair<opsym_t,int>, std::list<std::pair<opsym_t,int> > >& ops,
  std::stack<double,std::list<double> >& vals)
{
   show_stacks(ops, vals);

   double v1, v2 = 0, r;
   opsym_t o;

   o = ops.top().first;
   ops.pop();

   v1 = vals.top();
   vals.pop();

   if (o != OP_NEG)
   {
  v2 = vals.top();
  vals.pop();
   }

   r = op_funcs[o](v1, v2);
   vals.push(r);

#ifndef NDEBUG
   char opers[] = {'-', '+', '-', '*', '/', '('};
   std::cerr << "(" << v1 << ", " << v2 << ", " << opers[o] << ") -> " << r << std::endl;
#endif
}

double eval(std::string& s) throw (std::string&)
{
   std::istringstream expr(s);
   std::stack<std::pair<opsym_t,int>, std::list<std::pair<opsym_t,int> > > op_stack;
   std::stack<double,std::list<double> > val_stack;

   char c;
   enum {VAL_OR_UNARY_OP, BINARY_OP} state = VAL_OR_UNARY_OP;
   double temp;

   while (expr >> std::ws >> c)
   {
  show_stacks(op_stack, val_stack);

  if (state == VAL_OR_UNARY_OP)
  {
     if (c == '-')
    op_stack.push(std::pair<opsym_t,int>(OP_NEG, op_precs[OP_NEG]));
     else if (c == '(')
     {
    op_stack.push(std::pair<opsym_t,int>(OP_GRP, op_precs[OP_GRP]));
     }
     else
     {
    expr.seekg(-1, ios::cur);
    expr >> temp;
    if (!expr)
       throw std::string("Invalid number or unary operator");

    val_stack.push(temp);
    state = BINARY_OP;
     }
  }
  else
  {
     if (c == ')')
     {
    while (op_stack.top().first != OP_GRP)
    {
       if (op_stack.empty())
      throw std::string("Imbalanced `)'");
       stack_eval(op_stack, val_stack);
    }
    op_stack.pop();
    continue;
     }

     std::pair<opsym_t,int> op;

     switch (c)
     {
    case '+':
       op.first = OP_ADD;
       break;
    case '-':
       op.first = OP_SUB;
       break;
    case '*':
       op.first = OP_MUL;
       break;
    case '/':
       op.first = OP_DIV;
       break;
    default:
       throw std::string("Invalid operator");
     }

     op.second = op_precs[op.first];
     if (!op_stack.empty())
    if (op_stack.top().second > op.second)
       stack_eval(op_stack, val_stack);

     op_stack.push(op);
     state = VAL_OR_UNARY_OP;
  }
   }

   if (state == VAL_OR_UNARY_OP)
  throw (std::string("Dangling binary operator");

   while (!op_stack.empty())
   {
  if (op_stack.top().first == OP_GRP)
     op_stack.pop();
  else
     stack_eval(op_stack, val_stack);
   }

   temp = val_stack.top();
   val_stack.pop();
   return temp;
}

double opf_negate(double val, double)
{
   return -val;
}

double opf_add(double val1, double val2)
{
   return val2 + val1;
}

double opf_sub(double val1, double val2)
{
   return val2 - val1;
}

double opf_mul(double val1, double val2)
{
   return val2 * val1;
}

double opf_div(double val1, double val2)
{
   return val2 / val1;
}


Last edited by Guest on 26 Dec 2005 03:09:17 pm; edited 1 time in total
Back to top
Brazucs
I have no idea what my avatar is.


Super Elite (Last Title)


Joined: 31 Mar 2004
Posts: 3349

Posted: 24 Dec 2005 05:10:12 pm    Post subject:


Code:
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int  left, right;
    char op;  
    int  result;  
    
    while (cin >> left >> op >> right) {
        switch (op) {
            case '+': result = left + right;
                      break;
            case '-': result = left - right;
                      break;
            case '*': result = left * right;
                      break;
            case '/': result = left / right;
                      break;
            default : cout << "Bad op '" << op << "'" << endl;
                      continue;  
        }
        cout << result << endl << endl;
    }

    return 0;
}


Last edited by Guest on 24 Dec 2005 05:11:34 pm; edited 1 time in total
Back to top
sgm


Calc Guru


Joined: 04 Sep 2003
Posts: 1265

Posted: 24 Dec 2005 05:13:53 pm    Post subject:

Mine's cooler.

Also, the topic title is "Six-Function Calculator", and yours is only four. Technically, you're off-topic. Smile


Last edited by Guest on 24 Dec 2005 05:15:02 pm; edited 1 time in total
Back to top
DarkerLine
ceci n'est pas une |


Super Elite (Last Title)


Joined: 04 Nov 2003
Posts: 8328

Posted: 24 Dec 2005 05:28:20 pm    Post subject:

Four (or six) function calculators are the reason I hated Comp.Sci. I don't even remember how many I wrote, the last one involved (eww) expression trees.
Back to top
JoeImp
Enlightened


Active Member


Joined: 24 May 2003
Posts: 747

Posted: 24 Dec 2005 05:58:39 pm    Post subject:

Did you learn java before C++ by any chance?

And uhhh looking through this code again ..... Neutral Neutral Neutral


Last edited by Guest on 24 Dec 2005 06:00:15 pm; edited 1 time in total
Back to top
CoBB


Active Member


Joined: 30 Jun 2003
Posts: 720

Posted: 25 Dec 2005 06:29:06 am    Post subject:

There are better ways to be offtopic. :biggrin:


Code:
start :-
   repeat,
   read(X),
   (  X \= quit ->
      Y is X,
      write(Y),
      nl,
      flush_output,
      fail
   ;  !,
      true
   ).
Back to top
kermmartian
Site Admin Kemetech


Calc Guru


Joined: 20 Mar 2004
Posts: 1220

Posted: 25 Dec 2005 08:19:49 am    Post subject:

CoBB wrote:
There are better ways to be offtopic. :biggrin:


Code:
start :-
   repeat,
   read(X),
   (  X \= quit ->
      Y is X,
      write(Y),
      nl,
      flush_output,
      fail
  ;  !,
      true
   ).

[post="65228"]<{POST_SNAPBACK}>[/post]


.....Confused
Back to top
Rezek
Better Than You


Calc Guru


Joined: 24 Apr 2005
Posts: 1229

Posted: 25 Dec 2005 11:21:05 am    Post subject:

CoBB just likes to remind us all he's smarter than us by confusing us.


SWI-Prolog Smile
Back to top
CoBB


Active Member


Joined: 30 Jun 2003
Posts: 720

Posted: 25 Dec 2005 12:20:44 pm    Post subject:

Why SWI? There isn't anything platform specific in this code.
Back to top
Rezek
Better Than You


Calc Guru


Joined: 24 Apr 2005
Posts: 1229

Posted: 25 Dec 2005 12:58:52 pm    Post subject:

Whatever, you're still just as off-topic :P


Code:
make var
readnum var
print var


So it's four-function. Shoot me already!

Don't recognize it? Of course not! After all, I made it up. The interpreter is written in java and is dubbed JILL for Java Interpreted Language, the last L being for styLe ; ). It's a quite horrible attempt at smashing PHP and COBOL togethor, with a hint of python.


Last edited by Guest on 25 Dec 2005 01:13:29 pm; edited 1 time in total
Back to top
Brazucs
I have no idea what my avatar is.


Super Elite (Last Title)


Joined: 31 Mar 2004
Posts: 3349

Posted: 25 Dec 2005 03:46:19 pm    Post subject:

It's still a calculator. Are we off topic? Perhaps the question is, "Is the cup half-empty or half-full?"
Back to top
AlienCC
Creative Receptacle!


Know-It-All


Joined: 24 May 2003
Posts: 1927

Posted: 25 Dec 2005 04:00:04 pm    Post subject:

Brazucs wrote:
It's still a calculator. Are we off topic? Perhaps the question is, "Is the cup half-empty or half-full?"[post="65253"]<{POST_SNAPBACK}>[/post]

You do not want me to answer that question...really you don't.

--AlienCC
Back to top
Rezek
Better Than You


Calc Guru


Joined: 24 Apr 2005
Posts: 1229

Posted: 25 Dec 2005 05:53:47 pm    Post subject:

Quote:
Are we off topic?


Honestly, I really and truly think that was a joke on Sigma's part. Don't take my word for it though. Carry on Smile
Back to top
alexrudd
pm me if you read this


Bandwidth Hog


Joined: 06 Oct 2004
Posts: 2335

Posted: 25 Dec 2005 07:44:58 pm    Post subject:

Brazucs wrote:
It's still a calculator. Are we off topic? Perhaps the question is, "Is the cup half-empty or half-full?"  [post="65253"]<{POST_SNAPBACK}>[/post]
Well, if you count the water vapour in the air above the half-way mark, the glass is over half full. And it was poorly designed glass too, if the % error was 50.


EDIT: @Sigma: Make one of these calculators on a TI-83+. Then I'd be impressed.


Last edited by Guest on 25 Dec 2005 07:48:16 pm; edited 1 time in total
Back to top
Rezek
Better Than You


Calc Guru


Joined: 24 Apr 2005
Posts: 1229

Posted: 25 Dec 2005 08:07:40 pm    Post subject:

Um:


Code:
PROGRAM:CALC
:prompt A
:A


Unless of course you mean assembly. In that case just use the b_call Smile
Back to top
alexrudd
pm me if you read this


Bandwidth Hog


Joined: 06 Oct 2004
Posts: 2335

Posted: 25 Dec 2005 09:43:06 pm    Post subject:

"H" + first word of Rezek's post + "or" = alexrudd's post's intent.
Back to top
Rezek
Better Than You


Calc Guru


Joined: 24 Apr 2005
Posts: 1229

Posted: 25 Dec 2005 10:19:22 pm    Post subject:

Which one, the first or the second?

I guess I take life to seriously for all the times I rag on others for not understanding my sarcasm.
Back to top
Brazucs
I have no idea what my avatar is.


Super Elite (Last Title)


Joined: 31 Mar 2004
Posts: 3349

Posted: 26 Dec 2005 12:02:12 am    Post subject:

H um or, I believe...

Well, I know he was being funny. So was I with
Quote:
Are we off topic? Perhaps the question is, "Is the cup half-empty or half-full?"


My H um oreter has been a bit down lately.
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are UTC - 5 Hours

 

Advertisement