degree.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "stdafx.h"
  2. #include "defs.h"
  3. void
  4. eval_degree(void)
  5. {
  6. push(cadr(p1));
  7. eval();
  8. push(caddr(p1));
  9. eval();
  10. p1 = pop();
  11. if (p1 == symbol(NIL))
  12. guess();
  13. else
  14. push(p1);
  15. degree();
  16. }
  17. //-----------------------------------------------------------------------------
  18. //
  19. // Find the degree of a polynomial
  20. //
  21. // Input: tos-2 p(x)
  22. //
  23. // tos-1 x
  24. //
  25. // Output: Result on stack
  26. //
  27. // Note: Finds the largest numerical power of x. Does not check for
  28. // weirdness in p(x).
  29. //
  30. //-----------------------------------------------------------------------------
  31. #define POLY p1
  32. #define X p2
  33. #define DEGREE p3
  34. void
  35. degree(void)
  36. {
  37. save();
  38. X = pop();
  39. POLY = pop();
  40. DEGREE = zero;
  41. yydegree(POLY);
  42. push(DEGREE);
  43. restore();
  44. }
  45. void
  46. yydegree(U *p)
  47. {
  48. if (equal(p, X)) {
  49. if (iszero(DEGREE))
  50. DEGREE = one;
  51. } else if (car(p) == symbol(POWER)) {
  52. if (equal(cadr(p), X) && isnum(caddr(p)) && lessp(DEGREE, caddr(p)))
  53. DEGREE = caddr(p);
  54. } else if (iscons(p)) {
  55. p = cdr(p);
  56. while (iscons(p)) {
  57. yydegree(car(p));
  58. p = cdr(p);
  59. }
  60. }
  61. }