4.3.2.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "defines.h"
  4. #include <string.h>
  5. int getch(void);
  6. void ungetch(int);
  7. int isletter(char c);
  8. /* returns 1 if c is the character, 0 otherwise */
  9. int cis(char ,char );
  10. int isword(int c, char w[] );
  11. int cmpword(char[], char[] );
  12. /* getop: get next character or numeric operand */
  13. int getop(char s[])
  14. {
  15. if (!isdigit(s[0]) && s[0] != '.')
  16. {
  17. if (isletter(s[0]))
  18. {
  19. //can't help but feel that this could be a switch.
  20. if (cmpword(s, "exp"))
  21. return EXP;
  22. if (cmpword(s, "peek"))
  23. return PEEK;
  24. if (cmpword(s, "pow"))
  25. return POW;
  26. if (cmpword(s, "sin"))
  27. return SINE;
  28. if (cmpword(s, "set"))
  29. return SET;
  30. if (cmpword(s, "get"))
  31. return GET;
  32. if (cis(s[0],'S'))
  33. return SWAP;
  34. if (cis(s[0],'C'))
  35. return CLEAR;
  36. if (cis(s[0],'D'))
  37. return DUPE;
  38. //none of the above
  39. }
  40. return s[0];
  41. }
  42. return NUMBER;
  43. }
  44. int cis(char c,char b)
  45. {
  46. if (c >'A' && c <='Z')
  47. return ( (c==b) || (b==c+32 ) );
  48. else if (c >'a' && c<= 'z')
  49. return ( (c==b) || (b==c-32 ) );
  50. else return 0;
  51. }
  52. int isletter(char c)
  53. {
  54. return ((c> 'A' && c<='Z') || (c>'a' && c <='z')) ;
  55. }
  56. //checks if two strings are equal, if so returns 1 otherwise 0 */
  57. int cmpword(char source[], char target[])
  58. {
  59. return strcmp(source,target)==0;
  60. }
  61. //deprecated
  62. int isword(int c, char w[] )
  63. {
  64. char k=c;
  65. // var ;how far did we get;
  66. // count letters in word w = n
  67. // for i = 0, to i = n
  68. // getch
  69. // check if it matches
  70. // if so? keep checking
  71. // if not? ungetch() that many times
  72. // for j = 0 ; j < thatmanytimes; j++
  73. //ungetch()
  74. int howmuch=0,l=0,m=0;
  75. for(int i=0; w[i]!='\0'; i++)
  76. {
  77. howmuch=i;
  78. l=i;
  79. }
  80. for(int i=0; i <l+1; i++)
  81. {
  82. if ( isletter(k) && cis(k,w[i]))
  83. {
  84. k = getch();
  85. m++;
  86. }
  87. }
  88. //@might be off by one here.
  89. // if (m>0 && k>0)
  90. // ungetch(k); // <- this is totally unnecessary. it grabs the end of 'sine'
  91. return (m==l+1);
  92. }