Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define stacksize 100
  5. #define TYPE int
  6. typedef struct
  7. {
  8. int top;
  9. char items[stacksize];
  10. } stack;
  11. void initialize (stack *s)
  12. {
  13. s->top=0;
  14. }
  15. void push (stack*s,char value)
  16. {
  17. s->items[s->top++]=value;
  18. }
  19. int pop (stack*s)
  20. {
  21. return s->items[--s->top];
  22. }
  23. int isfull(stack*s)
  24. {
  25. return s->top<stacksize? 0:1;
  26. }
  27. int isempty(stack *s)
  28. {
  29. return s->top==0? 1:0;
  30. }
  31. void error()
  32. {
  33. printf("error");
  34. exit(-1);
  35. }
  36. typedef struct {
  37. TYPE data;
  38. char data2;
  39. struct Node *next;
  40. } Node;
  41. Node * newNode(TYPE value,char b[]) {
  42. Node *n=malloc(sizeof(Node));
  43. n->data=value;
  44. n->data2=b;
  45. n->next=NULL;
  46. return n;
  47. }
  48. typedef struct {
  49. Node *head;
  50. Node *tail;
  51. } LinkedList;
  52. void initLinkedList(LinkedList *ll){ll->head=NULL;ll->tail=NULL;}
  53. void printLinkedList(Node *n){
  54. printf("[");
  55. while (n) {printf("%d ",n->data);n=(Node *)n->next;}
  56. printf("]\n");
  57. }
  58. void addHead(LinkedList *ll,TYPE value,char b[]) {
  59. Node *n=newNode(value, b);
  60. n->next=ll->head;
  61. ll->head=n;
  62. if(!ll->tail) ll->tail=n; //list was empty
  63. }
  64. void addTail(LinkedList *ll,TYPE value,char b[]) {
  65. Node *n=newNode(value,b);Node *tail=ll->tail;
  66. ll->tail=n;
  67. if (tail) tail->next=n;
  68. else ll->head=n; // TAIL is NULL so is head!
  69. }
  70. TYPE deleteHead(LinkedList *ll){
  71. Node *first=(Node*)ll->head;
  72. if (first){
  73. TYPE value=first->data;
  74. ll->head=(Node *)first->next;
  75. free(first);
  76. if (!ll->head) ll->tail=NULL;
  77. return value;
  78. }
  79. return -1 ;/* ??? this is BAD */
  80. }
  81. Node * search(Node *n,TYPE value,char b[]) {
  82. if (!n) return NULL;
  83. if (n->data==value&&n->data2==b) return n;
  84. else return search(n->next,value,b);
  85. }
  86. char* topost(char a[])
  87. {
  88. int i;
  89. char y;
  90. stack m;
  91. initialize(&m);
  92.  
  93. char b[200];
  94. int j=0;
  95.  
  96. for (i=0; i<strlen(a); i++)
  97. {
  98.  
  99.  
  100.  
  101.  
  102. if (a[i]>='0'&&a[i]<='9')
  103. {
  104. printf("%c",a[i]);
  105. b[j++]=a[i];
  106. }
  107. else
  108. {
  109. switch (a[i])
  110. {
  111.  
  112. case'(':
  113. push(&m,a[i]);
  114. break;
  115.  
  116. case '+':
  117.  
  118. case '-':
  119. case '*':
  120. case '/':
  121. push(&m,a[i]);
  122. break;
  123. case ')':
  124.  
  125. while (!isempty(&m))
  126. {
  127.  
  128.  
  129. int c=pop(&m);
  130.  
  131.  
  132. if (c=='(')
  133.  
  134. break;
  135. else
  136. {
  137. printf("%c",c);
  138. b[j++]=c;
  139.  
  140. }
  141.  
  142.  
  143.  
  144. }
  145. break;
  146.  
  147.  
  148. }
  149. }
  150. }
  151. }
  152. int top(char a[])
  153. {
  154.  
  155. int i;
  156. int j=0;
  157. int c;
  158. char b[200];
  159. stack m;
  160.  
  161.  
  162. for (i=0; i<strlen(a); i++)
  163. {
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. if (a[i]>='0'&&a[i]<='9')
  171. {
  172. printf("%c",a[i]);
  173. b[j++]=a[i];
  174. }
  175. else
  176. {
  177. switch (a[i])
  178. {
  179.  
  180. case'(':
  181. push(&m,a[i]);
  182. break;
  183.  
  184. case '+':
  185.  
  186. case '-':
  187. case '*':
  188. case '/':
  189. push(&m,a[i]);
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. break;
  198. case ')':
  199.  
  200. while (!isempty(&m))
  201. {
  202.  
  203.  
  204. c=pop(&m);
  205.  
  206.  
  207. if (c=='(')
  208.  
  209. break;
  210. else
  211. {
  212. printf("%c",c);
  213. b[j++]=c;
  214.  
  215. }
  216.  
  217.  
  218.  
  219. }
  220.  
  221.  
  222.  
  223. }
  224. }
  225. }
  226.  
  227. }
  228. int eval(char a[])
  229. {
  230.  
  231. stack h;
  232. initialize(&h);
  233. char *postExp=a;
  234. char ch, *pt=postExp;
  235. while (ch=*pt++)
  236. {
  237. if (ch==' ') continue;
  238. if (ch<='9' && ch>='0') push(&h,ch-'0');
  239. else
  240. {
  241. int e,f;
  242. if (isempty(&h)) error();
  243. else e=pop(&h);
  244. if (isempty(&h)) error();
  245. else f=pop(&h);
  246. switch(ch)
  247. {
  248. case '+':
  249. push(&h,e+f);
  250. break;
  251. case '-':
  252. push(&h,e-f);
  253. break;
  254. case '*':
  255. push(&h,e*f);
  256. break;
  257. case '/':
  258. push(&h,e/f);
  259. break;
  260. default:
  261. error();
  262. } // switch
  263. }
  264. }
  265. int z=pop(&h);
  266. printf("=%d",z);
  267.  
  268. return z;
  269.  
  270. }
  271.  
  272.  
  273. void putinmemory(char b[])
  274. {
  275. LinkedList *s;
  276. int v=eval(b);
  277. initLinkedList(s);
  278. Node*n=newNode(v,b);
  279. addTail(s,v,b);
  280.  
  281.  
  282.  
  283.  
  284. }
  285.  
  286.  
  287.  
  288.  
  289. int main()
  290. {
  291.  
  292. stack m;
  293. char c;
  294.  
  295. char p[90];
  296. initialize(&m);
  297. char a[100];
  298. char b[100];
  299. LinkedList ll;
  300. initLinkedList(&ll);
  301. while (1)
  302. {
  303. gets(a);
  304. topost(a);
  305. eval(a);
  306.  
  307.  
  308. putinmemory(a);
  309.  
  310.  
  311. }
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320. /* if (a[i]==' ')
  321. continue;
  322. if(a[i]>='0'&&a[i]<='9')printf("%c",a[i]);
  323. if (a[i]==')'){
  324. if(isempty(&s)){
  325. printf("error");
  326. }
  327. else{
  328.  
  329.  
  330.  
  331. printf("%c",pop(&s));
  332. }
  333.  
  334.  
  335. }
  336. else{
  337. push(&s,a[i]);
  338. }
  339.  
  340. }*/
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347. return 0;
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement