5.10.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int eval_plus (char *arg[]);
  4. int eval_times (char *arg[]);
  5. int main(int argc, char *argv[])
  6. {
  7. char** current = argv;
  8. while (*current != 0)
  9. {
  10. current++;
  11. }
  12. // we are now at the end;
  13. int operationstack[1000]; //1 for *
  14. //2 for +
  15. int current_op=0;
  16. current --;
  17. int outcome=0;
  18. while (argc != 0)
  19. {
  20. switch ((int)*current[0])
  21. {
  22. case '*':
  23. operationstack[current_op]=2;
  24. current--;
  25. argc--;
  26. current_op++;
  27. continue;
  28. case '+':
  29. operationstack[current_op]=1;
  30. current--;
  31. argc--;
  32. current_op++;
  33. continue;
  34. case '0':
  35. case '1':
  36. case '2':
  37. case '3':
  38. case '4':
  39. case '5':
  40. case '6':
  41. case '7':
  42. case '8':
  43. case '9':
  44. if (outcome!=0) //something in the accumulator, we only need one number.
  45. {
  46. if (current_op !=0) //we need to do something to it.
  47. {
  48. current_op--;
  49. if (operationstack[current_op] == 2)
  50. // *
  51. {
  52. outcome=outcome*atoi(*current);
  53. current--;
  54. argc--;
  55. continue;
  56. }
  57. if (operationstack[current_op] == 1) // +
  58. {
  59. outcome=outcome+atoi(*current);
  60. current--;
  61. argc--;
  62. continue;
  63. }
  64. }
  65. else
  66. {
  67. // we keep the most recent number
  68. outcome=atoi(*current);
  69. argc--;
  70. current--;
  71. continue;
  72. }
  73. }
  74. else
  75. {
  76. // first number
  77. outcome=atoi(*current);
  78. argc--;
  79. current--;
  80. continue;
  81. }
  82. printf ("%s ascii outcome \n" , *current);
  83. printf ("%d current op \n" , current_op);
  84. argc--;
  85. current--;
  86. default:
  87. printf ("final outcome: %d \n",outcome);
  88. // printf ("current %s \n",*current);
  89. argc--;
  90. current--;
  91. break;
  92. }
  93. }
  94. return (0);
  95. }
  96. int eval_times (char *arg[])
  97. {
  98. return 0;
  99. //check if -1 argument is *
  100. //check if -1 argument is +
  101. //check if -1 argument is a number
  102. }
  103. int eval_plus (char *arg[])
  104. {
  105. return 0;
  106. }