Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Fortran > Re: OT retro-st...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 12 of 34 Topic 8154 of 8775
Post > Topic >>

Re: OT retro-styled HP calculator

by "Gerry Ford" <gerry@[EMAIL PROTECTED] > Apr 21, 2008 at 05:32 AM

"Les" <l.neilson@[EMAIL PROTECTED]
> wrote in message 
news:fuhl6b$eu3$1@[EMAIL PROTECTED]
>
> "glen herrmannsfeldt" <gah@[EMAIL PROTECTED]
> wrote in message 
> news:oKudnZYcsua1QpbVnZ2dnUVZ_gSdnZ2d@[EMAIL PROTECTED]
>> Walter Spector wrote:
>> (snip)
>>
>>>> Kids of today don't even know what RPN is, what more appreciated it.
>>
>> Complaints every time I offer my calculator for use.
>>
>>> Every student in the (middle/high) school system here is essentially
>>> required to own TI 30XIIS - which, of course, has no RPN.  The local
>>> office supply stores sell 'em by the thousands.  I had to hold my nose
>>> when buying them for my kids.  But at least they were cheap.  And RPN
>>> aside, they are a pretty decent choice.
>>
>> Schools I know now require the TI-83 series by the time they
>> start algebra.  The ability to graph a function is part of the
>> current teaching system.  They are completely programmable such
>> that one could write an RPN calculator for them, though I don't
>> know that anyone has done that.
>>
>> -- glen
>>
>
> I still have my Thornton slide rule c1967 with manual.
> No buttons. No Enter key. no RPN. Real easy to use. :-)
> Almost unreadable now even with my gl***** :-(
>
> Alas I don't have my book of log and other tables.
>
> I must say I was slightly miffed when my nephew was allowed to use his 
> scientific graphical calculator in his school exams, while we had to
make 
> do with school-provided log tables (to prevent cheating, in case we had 
> written formulae etc. in *our* own books) In those days we had to be
able 
> to "show from first principles". In the intervening 40 years I can't 
> remember any occasion I've ever needed to "show from first principles" 
> again!
>
> Les
>
>
>
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include<math.h>
#include <string.h>

#define MAXOP 100
#define NUMBER       0
#define IDENTIFIER   1
#define TRUE 1
#define FALSE 0

/*

  The new additions deal with adding functions from math.h to the
  calculator.

  In anticipation of the following exercise the code deals with an
  identifier in the following manner:

  If the identifier is recognised as one of the sup****ted mathematical
  functions then that function from the library is called. If the
  identifier is not one of the sup****ted functions, even if it is a
  valid function from math.h it is ignored.

  The main changes are the introduction of another define value
  (IDENTIFIER) along with its associated case in the switch statement.
  Getop has also been changed to deal with reading in alphabetical
  characters.

  This is exercise 4-5 from Kernighan & Ritchie, page 79.

 From  C_D reamer's site.

*/

int Getop(char s[]);
void push(double val);
double pop(void);
void showTop(void);
void duplicate(void);
void swapItems(void);
void clearStack();
void dealWithName(char s[]);

int main(void)
{
   int type;
   double op2;
   char s[MAXOP];
   int flag = TRUE;

   while((type = Getop(s)) != EOF)
   {
      switch(type)
      {
      case NUMBER:
         push(atof(s));
         break;
      case IDENTIFIER:
         dealWithName(s);
         break;
      case '+':
         push(pop() + pop());
         break;
      case '*':
         push(pop() * pop());
         break;
      case '-':
         op2 = pop();
         push(pop()- op2);
         break;
      case '/':
         op2 = pop();
         if(op2)
            push(pop() / op2);
         else
            printf("\nError: division by zero!");
         break;
      case '%':
         op2 = pop();
         if(op2)
            push(fmod(pop(), op2));
         else
            printf("\nError: division by zero!");
         break;
      case '?':
         showTop();
         break;
      case '#':
         duplicate();
         break;
      case '~':
         swapItems();
         break;
      case '!':
         clearStack();
      case '\n':
         printf("\n\t%.8g\n", pop());
         break;
      default:
         printf("\nError: unknown command %s.\n", s);
         break;
      }
   }
   return EXIT_SUCCESS;
}

#define MAXVAL 100

int sp = 0;          /* Next free stack position. */
double val[MAXVAL];  /* value stack. */

/* push: push f onto stack. */
void push(double f)
{
   if(sp < MAXVAL)
      val[sp++] = f;
   else
      printf("\nError: stack full can't push %g\n", f);
}

/*pop: pop and return top value from stack.*/
double pop(void)
{
   if(sp > 0)
      return val[--sp];
   else
   {
      printf("\nError: stack empty\n");
      return 0.0;
   }
}

void showTop(void)
{
   if(sp > 0)
      printf("Top of stack contains: %8g\n", val[sp-1]);
   else
      printf("The stack is empty!\n");
}

/*
Alternatively:
void showTop(void)
{
double item = pop();
printf("Top of stack contains: %8g\n", item);
push(item);
}
*/


void duplicate(void)
{
   double temp = pop();

   push(temp);
   push(temp);
}

void swapItems(void)
{
   double item1 = pop();
   double item2 = pop();

   push(item1);
   push(item2);
}

void clearStack(void)
{
   sp = 0;
}

/* deal with a string/name this may be either a maths function or for
future exercises: a variable */
void dealWithName(char s[])
{
   double op2;

   if( 0 == strcmp(s, "sin"))
      push(sin(pop()));
   else if( 0 == strcmp(s, "cos"))
      push(cos(pop()));
   else if (0 == strcmp(s, "exp"))
      push(exp(pop()));
   else if(!strcmp(s, "pow"))
   {
      op2 = pop();
      push(pow(pop(), op2));
   }
   else
      printf("%s is not a sup****ted function.\n", s);
}

int getch(void);
void unGetch(int);

/* Getop: get next operator or numeric operand. */
int Getop(char s[])
{
   int i = 0;
   int c;
   int next;
   /*size_t len;*/

   /* Skip whitespace */
   while((s[0] = c = getch()) == ' ' || c == '\t')
      ;
   s[1] = '\0';

   if(isalpha(c))
   {
      i = 0;
      while(isalpha(s[i++] = c ))
         c = getch();
      s[i - 1] = '\0';
      if(c != EOF)
         unGetch(c);
      return IDENTIFIER;
   }

   /* Not a number but may contain a unary minus. */
   if(!isdigit(c) && c != '.' && c != '-')
      return c;

   if(c == '-')
   {
      next = getch();
      if(!isdigit(next) && next != '.')
      {
         return c;
      }
      c = next;
   }
   else
      c = getch();

   while(isdigit(s[++i] = c))
      c = getch();
   if(c == '.')                 /* Collect fraction part. */
      while(isdigit(s[++i] = c = getch()))
         ;
      s[i] = '\0';
      if(c != EOF)
         unGetch(c);
      return NUMBER;
}

#define BUFSIZE 100

char buf[BUFSIZE];
int bufp = 0;

/* Getch: get a ( possibly pushed back) character. */
int getch(void)
{
   return (bufp > 0) ? buf[--bufp]: getchar();
}

/* unGetch: push character back on input. */
void unGetch(int c)
{
   if(bufp >= BUFSIZE)
      printf("\nUnGetch: too many characters\n");
   else
      buf[bufp++] = c;
}
// gcc -o polish kr45.c
// gcc -o polish -v kr45.c 2>text36.txt >text37
// kr text33.txt text36.txt 2>text41.txt >text42.txt

-- 
"A belief in a supernatural source of evil is not necessary; men alone
are quite capable of every wickedness."

~~  Joseph Conrad (1857-1924), novelist
 




 34 Posts in Topic:
OT retro-styled HP calculator
robert.corbett@[EMAIL PRO  2008-04-19 02:14:49 
Re: OT retro-styled HP calculator
glen herrmannsfeldt <g  2008-04-19 01:46:07 
Re: OT retro-styled HP calculator
robert.corbett@[EMAIL PRO  2008-04-19 02:41:02 
Re: OT retro-styled HP calculator
glen herrmannsfeldt <g  2008-04-19 02:37:36 
Re: OT retro-styled HP calculator
Walter Spector <w6ws_x  2008-04-19 07:28:58 
Re: OT retro-styled HP calculator
dpb <none@[EMAIL PROTE  2008-04-19 10:07:31 
Re: OT retro-styled HP calculator
Walter Spector <w6ws_x  2008-04-19 09:28:24 
Re: OT retro-styled HP calculator
dpb <none@[EMAIL PROTE  2008-04-19 12:01:49 
Re: OT retro-styled HP calculator
glen herrmannsfeldt <g  2008-04-20 16:31:50 
Re: OT retro-styled HP calculator
dpb <none@[EMAIL PROTE  2008-04-20 21:24:24 
Re: OT retro-styled HP calculator
"Les" <l.nei  2008-04-21 10:11:54 
Re: OT retro-styled HP calculator
"Gerry Ford" &l  2008-04-21 05:32:42 
Re: OT retro-styled HP calculator
"Kevin G. Rhoads&quo  2008-04-21 20:43:38 
Re: OT retro-styled HP calculator
nospam@[EMAIL PROTECTED]   2008-04-21 15:36:39 
Re: OT retro-styled HP calculator
dpb <none@[EMAIL PROTE  2008-04-21 18:22:23 
Re: OT retro-styled HP calculator
Dan Nagle <dannagle@[E  2008-04-21 23:26:18 
Re: OT retro-styled HP calculator
"Les" <l.nei  2008-04-22 10:32:38 
Re: OT retro-styled HP calculator
"Les" <l.nei  2008-04-22 09:42:01 
Re: OT retro-styled HP calculator
Catherine Rees Lay <ca  2008-04-23 14:31:20 
Re: OT retro-styled HP calculator
glen herrmannsfeldt <g  2008-04-23 05:59:10 
Re: OT retro-styled HP calculator
"FX" <couder  2008-04-23 15:00:39 
Re: OT retro-styled HP calculator
"Dr Ivan D. Reid&quo  2008-04-25 10:00:21 
Re: OT retro-styled HP calculator
"Jim Backus" &l  2008-04-24 19:22:02 
Re: OT retro-styled HP calculator
Ken Plotkin <kplotkin@  2008-04-19 11:43:36 
Re: OT retro-styled HP calculator
nospam@[EMAIL PROTECTED]   2008-04-19 08:53:51 
Re: OT retro-styled HP calculator
David Rowell <djrpubli  2008-04-19 12:28:21 
Re: OT retro-styled HP calculator
Ken Plotkin <kplotkin@  2008-04-19 12:48:28 
Re: OT retro-styled HP calculator
Gary Scott <garylscott  2008-04-19 11:59:50 
Re: OT retro-styled HP calculator
Gary Scott <garylscott  2008-04-19 11:58:01 
Re: OT retro-styled HP calculator
dpb <none@[EMAIL PROTE  2008-04-19 12:20:11 
Re: OT retro-styled HP calculator
Lorenzo `paulatz' Paulatt  2008-04-20 02:42:50 
Re: OT retro-styled HP calculator
"Gerry Ford" &l  2008-04-19 22:26:07 
Re: OT retro-styled HP calculator
Charles Coldwell <cold  2008-04-20 10:44:47 
Re: OT retro-styled HP calculator
Charles Coldwell <cold  2008-04-20 11:00:47 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 3:28:19 CDT 2008.