Advertisement
rmoronsv

Untitled

Dec 23rd, 2019
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.90 KB | None | 0 0
  1. #define NUMBER 1
  2. #define OFF 0
  3. #define ON 1
  4.  
  5. int getop(char[]); //processes data into a number that is pushed onto a stack or an operand
  6. void push(double); //pushes a number onto a stack
  7. double pop(void); //pops the top number in the stack
  8. double atofx(char[]); //converts the string op[] into a number
  9. int getline(char[], int); //gets a line. characters followed by \n \0
  10.  
  11. int index = 0; //index of the stack of numbers
  12. double values[MAXARRAYSIZE]; //stack of numbers
  13.  
  14. char ungets_string[MAXARRAYSIZE]; //an array used to push back an entire line of numbers and operands onto the input
  15. int ungets_index = 0; //index of the ungets array
  16.  
  17. char getlinearray[MAXARRAYSIZE]; //the array getline stores a line within
  18. int ix = 0; //the index of the array that stores the information obtained whilst getline runs
  19.  
  20. main()
  21. {
  22. int operator; //variable for holding a number popped from the stack of numbers values[index]
  23. double operator2; //second variable for holding a number popped from the stack of numbers values[index]
  24. double temp; //variable for holding a value temporarily
  25. double temp2; //variable for holding a value temporarily
  26. char op[MAXARRAYSIZE]; //array used to store numbers that may be negative and floating-point. Used with atof to produce a number that is pushed onto values[index]
  27.  
  28. double a = 0; /* variables for holding values and a flag to indicate if they have been defined*/
  29. int a_def = NO;
  30. double b = 0;
  31. int b_def = NO;
  32. double x = 0;
  33. int x_def = NO;
  34. double y = 0;
  35. int y_def = NO;
  36.  
  37. char undef = 0; //variable that holds the character of the variable being undefined a, b, x, y so it can be redefined as a new value
  38.  
  39. double mrp = 0.0; //most recently printed value
  40.  
  41. int k = 0;
  42. char temparray[MAXARRAYSIZE];
  43. int placeholder = 0;
  44.  
  45. start:
  46. getline(getlinearray, MAXARRAYSIZE); //gets a line and then the program goes to process the line using getop() and switch cases. all of it is commented
  47.  
  48. ungets: //this label
  49.  
  50. while ((operator = getop(op)) != EOF)
  51. {
  52. switch (operator) {
  53. case 'a':
  54. if (a_def == NO)
  55. {
  56. if (index > 0)
  57. a = pop(), a_def = YES;
  58. else
  59. a = 0;
  60. }
  61. else
  62. push(a);
  63. break;
  64. case 'b':
  65. if (b_def == NO)
  66. {
  67. if (index > 0)
  68. b = pop(), b_def = YES;
  69. else
  70. b = 0;
  71. }
  72. else
  73. push(b);
  74. break;
  75. case 'x':
  76. if (x_def == NO)
  77. {
  78. if (index > 0)
  79. x = pop(), x_def = YES;
  80. else
  81. x = 0;
  82. }
  83. else
  84. push(x);
  85. break;
  86. case 'y':
  87. if (y_def == NO)
  88. {
  89. if (index > 0)
  90. y = pop(), y_def = YES;
  91. else
  92. y = 0;
  93. }
  94. else
  95. push(y);
  96. break;
  97.  
  98. case 'z': //undefine a variable using za zb zx zy. Then the next time the variable is used it either becomes the topmost value in the stack or 0 if the stack is empty
  99. undef = getchar();
  100. switch (undef) {
  101. case 'a':
  102. a_def = NO;
  103. break;
  104. case 'b':
  105. b_def = NO;
  106. break;
  107. case 'x':
  108. x_def = NO;
  109. break;
  110. case 'y':
  111. y_def = NO;
  112. break;
  113. default: printf("Error: The character after z is supposed to be a variable. The variable are a, b, x, and y. The correct syntax to undefine a variable is za, zb, zx, and zy.\n");
  114. break;
  115. }
  116. break;
  117.  
  118. case NUMBER: //stores the number that is stored in the array op into the stack values[index] using the function push() and atof()
  119. push(atof(op));
  120. break;
  121.  
  122. case '+': /* basic operations */
  123. push(pop() + pop());
  124. break;
  125. case '*':
  126. push(pop() * pop());
  127. break;
  128. case '-':
  129. operator2 = pop();
  130. push(pop() - operator2);
  131. break;
  132. case '/':
  133. operator2 = pop();
  134. if (operator2 != 0.0)
  135. push(pop() / operator2);
  136. else
  137. printf("Error: Operator 2 is 0. Cannot divide by 0.\n");
  138. break;
  139. case '%':
  140. operator2 = pop();
  141. if (operator2 != 0)
  142. push((int)pop() % (int)operator2);
  143. else
  144. printf("Error: Operator 2 is 0. Cannot divide by 0.\n");
  145. break;
  146.  
  147. case 'm': //prints mrp
  148. printf("%f\n", mrp);
  149. break;
  150. case 'p': //prints topmost value of the stack values[index]. useful if you want to define a variable
  151. temp = pop();
  152. push(temp);
  153. printf("%f", temp);
  154. break;
  155. case 'd': //duplicated the topmost value in the stack values[index]
  156. temp = pop();
  157. push(temp);
  158. push(temp);
  159. break;
  160. case 's': //switches the topmost 2 values in the stack values[index]
  161. temp = pop();
  162. temp2 = pop();
  163. push(temp);
  164. push(temp2);
  165. break;
  166. case 'c': //clears the stack values[index]
  167. values[0] = '\0';
  168. index = 0;
  169. break;
  170.  
  171. case 'q': //sin(x)
  172. push(sin(pop()));
  173. break;
  174. case 'w': //cos(x)
  175. push(cos(pop()));
  176. break;
  177. case 'e': //power(x, y)
  178. operator2 = pop();
  179. push(pow(pop(), operator2));
  180. break;
  181.  
  182. case 'u': //ungets: 'u': places every character that has been read before 'u' into the array getlinearray[ix] being processed in getop() and creating evaluations of the switch cases
  183. //Example: 1 2 + u 1 \n should store 1 store 2 add 1 and 2 storing 3 then store 1 store 2 add 1 and 2 storing three store 1 then \n print out 1 and values 3 and 3 should be in values[index]
  184. //if two 'u' are in the array getlinearray() then this is a label for the operation ungets 'u' to repeat and replace the character 'u' with every character before it. Example: 1 2 u 3 u 1 \n should store 1 store 2 store 1 store 2 store 3 store 1 store 2 store 1 store 2 store 3 store 1 \n print 1
  185. //and values[index] should be 1 2 1 2 3 1 2 1 2 3
  186. moreungets:
  187. k = 0;
  188. placeholder = ix - 1; //placeholder = ix - 1 is where 'u' is.
  189. printf("%s\n", ungets_string); //prints out the ungets_string so I see what string is placed onto getlinearray[] processed by getop()
  190.  
  191. while (getlinearray[placeholder] != '\0') //stores everything after 'u' into temp
  192. {
  193. temparray[k] = getlinearray[placeholder];
  194. k++, placeholder++;
  195. }
  196. temparray[k] = '\0';
  197.  
  198. while (ungets_index >= 0) //inserts ungets_string[] into getlinearray[] beginning where 'u' was stored
  199. {
  200. getlinearray[placeholder] = ungets_string[ungets_index];
  201. placeholder++, ungets_index--;
  202. }
  203. ungets_string[0] = '\0', ungets_index = 0; //resets ungets_string[] for next use 'u'
  204.  
  205. k = 0;
  206. while (temparray[k] != '\0') //restores the rest of the array getlinearray[] before the insertion of ungets_string[]
  207. {
  208. getlinearray[placeholder] = temparray[k];
  209. k++, placeholder++;
  210. }
  211. getlinearray[placeholder++] = '\0';
  212.  
  213. ix = 0;
  214. while (getlinearray[ix] != '\0') //checks getlinerarray[] for any more 'u'
  215. {
  216. if (getlinearray[ix] == 'u') //IF 'u' is found repeat
  217. {
  218. ix++;
  219. goto moreungets;
  220. }
  221. else //else store everything into ungets_string for the next time 'u' may be read. //ungets_string[] can be cleared with 'r'.
  222. {
  223. ungets_string[ungets_index] = getlinearray[ix];
  224. ix++, ungets_index++;
  225. }
  226. }
  227. goto ungets;
  228. break;
  229. case 'r': //clears ungets
  230. ungets_string[0] = '\0', ungets_index = 0;
  231. break;
  232.  
  233. case '\n': //prints the topmost value of the stack values[index], clears getlinearray[] so a new line can be read and processed and saves mrp.
  234. printf("\t%.8f\n", mrp = pop());
  235. getlinearray[0] = '\0', ix = 0;
  236. goto start;
  237. break;
  238. case '!': //terminates
  239. goto terminate;
  240. default: //default: error
  241. printf("Error: The program read a character using getchar() that was not a digit or a character with a switch case defined. The character is %s.\n", op);
  242. break;
  243. }
  244. }
  245. terminate:
  246. return 0;
  247. }
  248. void push(double OperandValue)
  249. {
  250. if (index < MAXARRAYSIZE) //IF THERE IS SPACE IN THE STACK values[index] STORE THE VALUE SENT TO THE FUNCTION else error
  251. values[index++] = OperandValue;
  252. else
  253. printf("Error: Stack full. Cannot push operand value %g.\n", OperandValue);
  254. }
  255.  
  256. double pop(void)
  257. {
  258. if (index > 0) //IF THERE IS A VALUE IN values[index] POP/RETURN THE VALUE TO THE FUNCTION CALL pop() else error
  259. return values[--index];
  260. else
  261. {
  262. printf("Error: There are 0 operands stored in the stack containing numbers.\n");
  263. return 0.0;
  264. }
  265. }
  266.  
  267. int getop(char s[]) //processes getline(getlinearray[], MAXARRAYSIZE) and ungets
  268. {
  269. int j = 0;
  270.  
  271. while (getlinearray[ix] == ' ' || getlinearray[ix] == '\t' || getlinearray[ix] == '\0') //skips white space
  272. ix++;
  273.  
  274. if (getlinearray[ix] != '\n' && getlinearray[ix] != 'u') //save character or number into op "s[]" and ungets_string for ungets
  275. s[j++] = ungets_string[ungets_index++] = getlinearray[ix];
  276.  
  277. else if (getlinearray[ix] == '\n' || getlinearray[ix] == 'u') //don't save 'u' and '\n' into ungets
  278. ungets_string[ungets_index++] = ' ';
  279.  
  280. if (!isdigit(getlinearray[ix]) && getlinearray[ix] != '.' && getlinearray[ix] != '-') //return character
  281. {
  282. ungets_string[ungets_index++] = ' ';
  283. return getlinearray[ix++];
  284. }
  285. if (getlinearray[ix] == '-' && !isdigit(getlinearray[ix + 1])) //return '-'
  286. {
  287. ungets_string[ungets_index++] = ' ';
  288. return getlinearray[ix++];
  289. }
  290. else if (getlinearray[ix] == '-' && isdigit(getlinearray[ix+1])) //negative number
  291. {
  292. ix++;
  293. s[j] = getlinearray[ix]; //save digit
  294. ungets_string[ungets_index] = getlinearray[ix];
  295. j++, ungets_index++;
  296. }
  297.  
  298. if (isdigit(getlinearray[ix + 1])) //save number
  299. {
  300. ix++;
  301. ungets_string[ungets_index] = s[j] = getlinearray[ix];
  302. ix++, j++, ungets_index++;
  303. while (isdigit(getlinearray[ix]))
  304. {
  305. ungets_string[ungets_index] = s[j] = getlinearray[ix];
  306. ix++, j++, ungets_index++;
  307. }
  308. }
  309. if (getlinearray[ix] == '.') //save fractional part of number
  310. {
  311. ix++;
  312. ungets_string[ungets_index++] = s[j++] = getlinearray[ix];
  313. ix++;
  314. while (isdigit(getlinearray[ix]))
  315. {
  316. ungets_string[ungets_index] = s[j] = getlinearray[ix];
  317. ix++, j++, ungets_index++;
  318. }
  319. }
  320. ix++;
  321. s[j] = '\0';
  322. ungets_string[ungets_index++] = ' ';
  323.  
  324. return NUMBER; //return to getop() and store op[] into values[index] using atof()
  325. }
  326.  
  327. double atofx(char s[]) //converts a string to a floating point number
  328. {
  329. int i = 0;
  330. int sign = 1;
  331. int val = 0;
  332. int power = 1;
  333. int e = 0;
  334. double exp = 0;
  335. if (s[i] == '-')
  336. {
  337. sign = -1;
  338. i++;
  339. }
  340. else if (s[i] == '+')
  341. i++;
  342. while (isdigit(s[i]))
  343. val = val * 10 + (s[i++] - '0');
  344. if (s[i] == '.')
  345. {
  346. i++;
  347. while (isdigit(s[i]))
  348. {
  349. val = val * 10 + (s[i++] - '0');
  350. power *= 10;
  351. }
  352. }
  353. val = sign * val / power;
  354. if (s[i] == 'e' || s[i] == 'E')
  355. {
  356. i++;
  357. if (s[i] == '-')
  358. {
  359. i++;
  360. while (isdigit(s[i]))
  361. e = e * 10 + (s[i] - '0');
  362. while (e-- > 0)
  363. val /= 10;
  364. return val;
  365. }
  366. else if (s[i] == '+')
  367. {
  368. i++;
  369. while (isdigit(s[i]))
  370. e = e * 10 + (s[i] - '0');
  371. while (e-- > 0)
  372. val *= 10;
  373. return val;
  374. }
  375. }
  376. else
  377. return val;
  378. }
  379.  
  380. int getline(char s[], int lim) //stores characters into an array, ends with '\n' '\0'
  381. {
  382. int c;
  383. int i = 0;
  384. while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
  385. s[i++] = c;
  386. if (c == '\n')
  387. s[i++] = c;
  388. s[i] = '\0';
  389. return i;
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement