Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <math.h>
  6.  
  7. typedef struct Stack{ //stack for characters
  8. double data[255]; //char* array to serve as stack
  9. int top; //marks where the head is
  10. }stack;
  11.  
  12. void create(stack * s); //initializes stack
  13. int empty(stack * s); //checks if the stack is empty
  14. int full(stack * s); //checks if the stack is full
  15. double pop(stack * s); //pops a double from the stack
  16. void push(stack * s, double c); //gets a double then places them into the stack
  17. void process(stack* s, char* postfix);
  18. double solve(char c, double x, double y);
  19. double convert(char c); //convert the value from char to double
  20.  
  21. int main(){
  22. int choice; //open a file or not
  23. stack s; //stack for double inputs
  24. printf("Open a file? 1/0 ");
  25. scanf("%d", &choice);
  26. if(choice == 0){
  27. int count = 1; //to determine if the user will continue giving statements
  28. char* postfix;
  29. create(&s);
  30.  
  31. while(count != 0){
  32. printf("Enter postfix: ");
  33. scanf("%s", postfix);
  34.  
  35. process(&s, postfix);
  36.  
  37. printf("Continue? 1/0");
  38. scanf("%d", &count);
  39. }
  40. }
  41. else if(choice == 1){
  42. char fn[255]; //file name array
  43. char buffer[255]; //temporary array for reading lines in the text file to be given
  44. printf("Input file name: ");
  45. scanf("%s", fn);
  46. create(&s);
  47. FILE *fp;
  48. fp = fopen(fn, "r"); //opens file that contains multiple postfix commands
  49. while(!(feof(fp))){ //while not end of file, read contents
  50. FILE* w;
  51. w = fopen("output.txt", "w"); //creates an empty txt file
  52. fclose(w);
  53.  
  54. fgets(buffer, sizeof(buffer), (FILE*)fp); //gets the current line (aka the postfix command) and adds to buffer
  55. process(&s, buffer); //gets the file name of the current loop, the stack, the buffer (postfix), and the number of the current loop
  56. }
  57. fclose(fp); //closes the postfix txt
  58. }
  59. else
  60. printf("Give a valid choice.");
  61. }
  62.  
  63. void create(stack * s){
  64. s -> top = -1; //sets the stack head at -1 thus makes the array inaccessible
  65. }
  66.  
  67. int empty(stack * s){
  68. if(s->top==-1)
  69. return 1; //stack is empty
  70. return 0; //stack is not empty
  71. }
  72.  
  73. int full(stack * s){
  74. if(s->top==254)//since the data array is only 255 big
  75. return 1;//stack is full
  76. return 0;
  77. }
  78.  
  79. double pop(stack * s){
  80. double x;
  81. x=s->data[s->top];//gets the value of the data at the current top and assign to x
  82. s->top=s->top-1; //reduces the current value of top
  83. return(x);
  84. }
  85.  
  86. void push(stack * s, double c){
  87. s->top=s->top+1; //moves the top amount to move array position
  88. s->data[s->top]=c; //assigns the top to the value of c
  89. }
  90.  
  91. void process(stack* s, char* postfix){
  92. //stuff here
  93. double x, y;
  94. double total;
  95. int i;
  96. FILE* fp;
  97. fp = fopen("output.txt", "a");
  98. fprintf(fp, "%s\n", postfix);
  99. fclose(fp);
  100.  
  101. for(i = 0; i < strlen(postfix); i++){
  102. if(isalnum(postfix[i])){ //checks if the current postfix character is a number or a letter
  103. push(s, convert(postfix[i])); //pushes to stack the converted value of the operand
  104. }
  105. else{ //if the postfix character is an operand
  106. if(empty(s)==0){ //checks if the stack is empty
  107. x = pop(s); //pops the value
  108. }
  109. else{ //it means there are no operands anymore to compute
  110. printf("Form invalid.\n");
  111. exit(0);
  112. }
  113. if(empty(s)==0){ //a repetition of the earlier process but assigned to y
  114. y = pop(s);
  115. }
  116. else{
  117. printf("Form invalid.\n");
  118. exit(0);
  119. }
  120.  
  121. total = solve(postfix[i], x, y);
  122. push(s, total);
  123. }
  124. }
  125.  
  126. fp = fopen("output.txt", "a");
  127. total = pop(s); //pops the final pushed value
  128. if(empty(s)==1){ //if no more operands is left in the stack
  129. fprintf(fp, "Total is %lf\n", total);
  130. }
  131. else{ //something is still in the stack
  132. fprintf(fp, "Form invalid.\n");
  133. exit(0);
  134. }
  135. fclose(fp);
  136. }
  137.  
  138. double solve(char c, double x, double y){
  139. FILE* fp;
  140. fp = fopen("output.txt", "a"); //opens the current loop filename and appends the commands
  141.  
  142. char* s;
  143. switch(c){
  144. case '+':
  145. fprintf(fp, "LD %lf\nAD %lf\n", y, x);
  146. fclose(fp);
  147. return(y+x);
  148. break;
  149. case '-':
  150. fprintf(fp, "LD %lf\nSB %lf\n", y, x);
  151. fclose(fp);
  152. return(y-x);
  153. break;
  154. case '*':
  155. fprintf(fp, "LD %lf\nML %lf\n", y, x);
  156. fclose(fp);
  157. return(y*x);
  158. break;
  159. case '/':
  160. fprintf(fp, "LD %lf\nDV %lf\n", y, x);
  161. fclose(fp);
  162. return(y/x);
  163. break;
  164. case '^':
  165. fprintf(fp, "LD %lf\nPR %lf\n", x, y);
  166. fclose(fp);
  167. return pow(x, y);
  168. break;
  169. }
  170. }
  171.  
  172. double convert(char c){ //converts characters to double values
  173. switch(c){
  174. case 'A':
  175. return 1.0;
  176. break;
  177. case 'B':
  178. return 2.0;
  179. break;
  180. case 'C':
  181. return 3.0;
  182. break;
  183. case 'D':
  184. return 4.0;
  185. break;
  186. case 'E':
  187. return 5.0;
  188. break;
  189. case 'F':
  190. return 6.0;
  191. break;
  192. case 'G':
  193. return 7.0;
  194. break;
  195. case 'H':
  196. return 8.0;
  197. break;
  198. case 'I':
  199. return 9.0;
  200. break;
  201. case 'J':
  202. return 10.0;
  203. break;
  204. case 'K':
  205. return 11.0;
  206. break;
  207. case 'L':
  208. return 12.0;
  209. break;
  210. case 'M':
  211. return 13.0;
  212. break;
  213. case 'N':
  214. return 14.0;
  215. break;
  216. case 'O':
  217. return 15.0;
  218. break;
  219. case 'P':
  220. return 16.0;
  221. break;
  222. case 'Q':
  223. return 17.0;
  224. break;
  225. case 'R':
  226. return 18.0;
  227. break;
  228. case 'S':
  229. return 19.0;
  230. break;
  231. case 'T':
  232. return 20.0;
  233. break;
  234. case 'U':
  235. return 21.0;
  236. break;
  237. case 'V':
  238. return 22.0;
  239. break;
  240. case 'W':
  241. return 23.0;
  242. break;
  243. case 'X':
  244. return 24.0;
  245. break;
  246. case 'Y':
  247. return 25.0;
  248. break;
  249. case 'Z':
  250. return 26.0;
  251. break;
  252. case 'a':
  253. return -1.0;
  254. break;
  255. case 'b':
  256. return -2.0;
  257. break;
  258. case 'c':
  259. return -3.0;
  260. break;
  261. case 'd':
  262. return -4.0;
  263. break;
  264. case 'e':
  265. return -5.0;
  266. break;
  267. case 'f':
  268. return -6.0;
  269. break;
  270. case 'g':
  271. return -7.0;
  272. break;
  273. case 'h':
  274. return -8.0;
  275. break;
  276. case 'i':
  277. return -9.0;
  278. break;
  279. case 'j':
  280. return -10.0;
  281. break;
  282. case 'k':
  283. return -11.0;
  284. break;
  285. case 'l':
  286. return -12.0;
  287. break;
  288. case 'm':
  289. return -13.0;
  290. break;
  291. case 'n':
  292. return -14.0;
  293. break;
  294. case 'o':
  295. return -15.0;
  296. break;
  297. case 'p':
  298. return -16.0;
  299. break;
  300. case 'q':
  301. return -17.0;
  302. break;
  303. case 'r':
  304. return -18.0;
  305. break;
  306. case 's':
  307. return -19.0;
  308. break;
  309. case 't':
  310. return -20.0;
  311. break;
  312. case 'u':
  313. return -21.0;
  314. break;
  315. case 'v':
  316. return -22.0;
  317. break;
  318. case 'w':
  319. return -23.0;
  320. break;
  321. case 'x':
  322. return -24.0;
  323. break;
  324. case 'y':
  325. return -25.0;
  326. break;
  327. case 'z':
  328. return -26.0;
  329. break;
  330. case '1':
  331. return 1.0;
  332. break;
  333. case '2':
  334. return 2.0;
  335. break;
  336. case '3':
  337. return 3.0;
  338. break;
  339. case '4':
  340. return 4.0;
  341. break;
  342. case '5':
  343. return 5.0;
  344. break;
  345. case '6':
  346. return 6.0;
  347. break;
  348. case '7':
  349. return 7.0;
  350. break;
  351. case '8':
  352. return 8.0;
  353. break;
  354. case '9':
  355. return 9.0;
  356. break;
  357. case '0':
  358. return 0.0;
  359. }
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement