rmoronsv

Untitled

Dec 23rd, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 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. b = pop(), b_def = YES;
  67. else
  68. push(b);
  69. break;
  70. case 'x':
  71. if (x_def == NO)
  72. x = pop(), x_def = YES;
  73. else
  74. push(a);
  75. break;
  76. case 'y':
  77. if (y_def == NO)
  78. y = pop(), y_def = YES;
  79. else
  80. push(a);
  81. break;
  82.  
  83. 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
  84. undef = getchar();
  85. switch (undef) {
  86. case 'a':
  87. a_def = NO;
  88. break;
  89. case 'b':
  90. b_def = NO;
  91. break;
  92. case 'x':
  93. x_def = NO;
  94. break;
  95. case 'y':
  96. y_def = NO;
  97. break;
  98. 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");
  99. break;
  100. }
  101. break;
  102.  
  103. case 'm': //prints mrp
  104. printf("%f\n", mrp);
  105. break;
  106.  
  107. case NUMBER: //stores the number that is stored in the array op into the stack values[index] using the function push() and atof()
  108. push(atof(op));
  109. break;
  110.  
  111. case '+': /* basic operations */
  112. push(pop() + pop());
  113. break;
  114. case '*':
  115. push(pop() * pop());
  116. break;
  117. case '-':
  118. operator2 = pop();
  119. push(pop() - operator2);
  120. break;
  121. case '/':
  122. operator2 = pop();
  123. if (operator2 != 0.0)
  124. push(pop() / operator2);
  125. else
  126. printf("Error: Operator 2 is 0. Cannot divide by 0.\n");
  127. break;
  128. case '%':
  129. operator2 = pop();
  130. if (operator2 != 0)
  131. push((int)pop() % (int)operator2);
  132. else
  133. printf("Error: Operator 2 is 0. Cannot divide by 0.\n");
  134. break;
  135.  
  136. case 'p': //prints topmost value of the stack values[index]. useful if you want to define a variable
  137. temp = pop();
  138. push(temp);
  139. printf("%f", temp);
  140. break;
  141. case 'd': //duplicated the topmost value in the stack values[index]
  142. temp = pop();
  143. push(temp);
  144. push(temp);
  145. break;
  146. case 's': //switches the topmost 2 values in the stack values[index]
  147. temp = pop();
  148. temp2 = pop();
  149. push(temp);
  150. push(temp2);
  151. break;
  152. case 'c': //clears the stack values[index]
  153. values[0] = '\0';
  154. index = 0;
  155. break;
  156. case 'q': //sin(x)
  157. push(sin(pop()));
  158. break;
  159. case 'w': //cos(x)
  160. push(cos(pop()));
  161. break;
  162. case 'e': //power(x, y)
  163. operator2 = pop();
  164. push(pow(pop(), operator2));
  165. break;
  166.  
  167. 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
  168. //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]
  169. //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
  170. //and values[index] should be 1 2 1 2 3 1 2 1 2 3
  171. moreungets:
  172. k = 0;
  173. placeholder = ix - 1; //placeholder = ix - 1 is where 'u' is.
  174. printf("%s\n", ungets_string); //prints out the ungets_string so I see what string is placed onto getlinearray[] processed by getop()
  175.  
  176. while (getlinearray[ix] != '\0') //stores everything after 'u' into temp
  177. temparray[k++] = getlinearray[ix++];
  178. temparray[k] = '\0';
  179.  
  180. while (ungets_index >= 0) //inserts ungets_string[] into getlinearray[] beginning where 'u' was stored
  181. getlinearray[placeholder++] = ungets_string[ungets_index--];
  182. ungets_string[0] = '\0', ungets_index = 0; //resets ungets_string[] for next use 'u'
  183.  
  184. k = 0;
  185. while (temparray[k] != '\0') //restores the rest of the array getlinearray[] before the insertion of ungets_string[]
  186. getlinearray[placeholder++] = temparray[k++];
  187. getlinearray[placeholder] = '\0';
  188.  
  189. ix = 0;
  190. while (getlinearray[ix] != '\0') //checks getlinerarray[] for any more 'u'
  191. {
  192. if (getlinearray[ix] == 'u') //IF 'u' is found repeat
  193. {
  194. ix++;
  195. goto moreungets;
  196. }
  197. else //else store everything into ungets_string for the next time 'u' may be read. //ungets_string[] can be cleared with 'r'.
  198. ungets_string[ungets_index++] = getlinearray[ix];
  199. }
  200. goto ungets;
  201. break;
  202. case 'r': //clears ungets
  203. ungets_string[0] = '\0', ungets_index = 0;
  204. break;
  205.  
  206. case '\n': //prints the topmost value of the stack values[index], clears getlinearray[] so a new line can be read and processed and save mrp.
  207. printf("\t%.8f\n", mrp = pop());
  208. getlinearray[0] = '\0', ix = 0;
  209. goto start;
  210. break;
  211. case '!': //terminates
  212. goto terminate;
  213. default: //default: error
  214. 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);
  215. break;
  216. }
  217. }
  218. terminate:
  219. return 0;
  220. }
  221. void push(double OperandValue)
  222. {
  223. if (index < MAXARRAYSIZE) //IF THERE IS SPACE IN THE STACK values[index] STORE THE VALUE SENT TO THE FUNCTION else error
  224. values[index++] = OperandValue;
  225. else
  226. printf("Error: Stack full. Cannot push operand value %g.\n", OperandValue);
  227. }
  228.  
  229. double pop(void)
  230. {
  231. if (index > 0) //IF THERE IS A VALUE IN values[index] POP/RETURN THE VALUE TO THE FUNCTION CALL pop() else error
  232. return values[--index];
  233. else
  234. {
  235. printf("Error: There are 0 operands stored in the stack containing numbers.\n");
  236. return 0.0;
  237. }
  238. }
  239.  
  240. int getop(char s[]) //processes getline(getlinearray[], MAXARRAYSIZE) and ungets
  241. {
  242. int c;
  243. int d = 0;
  244. int j = 0;
  245.  
  246. while (getlinearray[ix] == ' ' || getlinearray[ix] == '\t' || getlinearray[ix] == '\0') //skips white space
  247. ix++;
  248.  
  249. if (getlinearray[ix] != '\n' && getlinearray[ix] != 'u') //save character or number into op "s[]" and ungets_string for ungets
  250. s[j++] = ungets_string[ungets_index++] = getlinearray[ix];
  251. else if (getlinearray[ix] == '\n' || getlinearray[ix] == 'u') //don't save 'u' and '\n' into ungets
  252. ungets_string[ungets_index++] = ' ';
  253.  
  254. if (!isdigit(getlinearray[ix]) && getlinearray[ix] != '.' && getlinearray[ix] != '-') //return character
  255. {
  256. ungets_string[ungets_index++] = ' ';
  257. return getlinearray[ix++];
  258. }
  259. if (getlinearray[ix] == '-' && !isdigit(getlinearray[ix + 1])) //return '-'
  260. {
  261. ungets_string[ungets_index++] = ' ';
  262. return getlinearray[ix++];
  263. }
  264. else if (getlinearray[ix] == '-' && isdigit(getlinearray[ix+1])) //negative number
  265. {
  266. s[j++] = getlinearray[++ix]; //save digit
  267. ungets_string[ungets_index++] = getlinearray[ix];
  268. }
  269.  
  270. if (isdigit(getlinearray[ix + 1])) //save number
  271. {
  272. ungets_string[ungets_index++] = s[j++] = getlinearray[++ix];
  273. ix++;
  274. while (isdigit(getlinearray[ix]))
  275. ungets_string[ungets_index++] = s[j++] = getlinearray[ix++];
  276. }
  277. if (getlinearray[ix] == '.') //save fractional part of number
  278. {
  279. ungets_string[ungets_index++] = s[j++] = getlinearray[ix++];
  280. while (isdigit(getlinearray[ix]))
  281. ungets_string[ungets_index++] = s[j++] = getlinearray[ix++];
  282. }
  283.  
  284. s[j] = '\0';
  285. ungets_string[ungets_index] = ' ';
  286.  
  287. return NUMBER; //return to getop() and store op[] into values[index] using atof()
  288. }
  289.  
  290. double atofx(char s[]) //converts a string to a floating point number
  291. {
  292. int i = 0;
  293. int sign = 1;
  294. int val = 0;
  295. int power = 1;
  296. int e = 0;
  297. double exp = 0;
  298. if (s[i] == '-')
  299. {
  300. sign = -1;
  301. i++;
  302. }
  303. else if (s[i] == '+')
  304. i++;
  305. while (isdigit(s[i]))
  306. val = val * 10 + (s[i++] - '0');
  307. if (s[i] == '.')
  308. {
  309. i++;
  310. while (isdigit(s[i]))
  311. {
  312. val = val * 10 + (s[i++] - '0');
  313. power *= 10;
  314. }
  315. }
  316. val = sign * val / power;
  317. if (s[i] == 'e' || s[i] == 'E')
  318. {
  319. i++;
  320. if (s[i] == '-')
  321. {
  322. i++;
  323. while (isdigit(s[i]))
  324. e = e * 10 + (s[i] - '0');
  325. while (e-- > 0)
  326. val /= 10;
  327. return val;
  328. }
  329. else if (s[i] == '+')
  330. {
  331. i++;
  332. while (isdigit(s[i]))
  333. e = e * 10 + (s[i] - '0');
  334. while (e-- > 0)
  335. val *= 10;
  336. return val;
  337. }
  338. }
  339. else
  340. return val;
  341. }
  342.  
  343. int getline(char s[], int lim) //stores characters into an array, ends with '\n' '\0'
  344. {
  345. int c;
  346. int i = 0;
  347. while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
  348. s[i++] = c;
  349. if (c == '\n')
  350. s[i++] = c;
  351. s[i] = '\0';
  352. return i;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment