Advertisement
Guest User

Untitled

a guest
May 4th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 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.  
  146.  
  147.  
  148. }
  149. }
  150.  
  151. }
  152. return b;
  153. }
  154. /*int top(char a[])
  155. {
  156.  
  157. int i;
  158. int j=0;
  159. int c;
  160. char b[200];
  161. stack m;
  162.  
  163.  
  164. for (i=0; i<strlen(a); i++)
  165. {
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. if (a[i]>='0'&&a[i]<='9')
  173. {
  174. printf("%c",a[i]);
  175. b[j++]=a[i];
  176. }
  177. else
  178. {
  179. switch (a[i])
  180. {
  181.  
  182. case'(':
  183. push(&m,a[i]);
  184. break;
  185.  
  186. case '+':
  187.  
  188. case '-':
  189. case '*':
  190. case '/':
  191. push(&m,a[i]);
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. break;
  200. case ')':
  201.  
  202. while (!isempty(&m))
  203. {
  204.  
  205.  
  206. c=pop(&m);
  207.  
  208.  
  209. if (c=='(')
  210.  
  211. break;
  212. else
  213. {
  214. printf("%c",c);
  215. b[j++]=c;
  216.  
  217. }
  218.  
  219.  
  220.  
  221. }
  222.  
  223.  
  224.  
  225. }
  226. }
  227. }
  228.  
  229. }*/
  230. int eval(char a[])
  231. {
  232.  
  233. stack h;
  234. initialize(&h);
  235. char *postExp=a;
  236. char ch, *pt=postExp;
  237. while (ch=*pt++)
  238. {
  239. if (ch==' ') continue;
  240. if (ch<='9' && ch>='0') push(&h,ch-'0');
  241. else
  242. {
  243. int e,f;
  244. if (isempty(&h)) error();
  245. else e=pop(&h);
  246. if (isempty(&h)) error();
  247. else f=pop(&h);
  248. switch(ch)
  249. {
  250. case '+':
  251. push(&h,e+f);
  252. break;
  253. case '-':
  254. push(&h,e-f);
  255. break;
  256. case '*':
  257. push(&h,e*f);
  258. break;
  259. case '/':
  260. push(&h,e/f);
  261. break;
  262. default:
  263. error();
  264. break;
  265.  
  266. } // switch
  267. }
  268. }
  269. int z=pop(&h);
  270. printf("=%d",z);
  271.  
  272. return z;
  273.  
  274. }
  275.  
  276.  
  277. void putinmemory(char b[])
  278. {
  279. LinkedList *s;
  280. int ve=eval(b);
  281. initLinkedList(s);
  282. Node*n=newNode(ve,b);
  283. addTail(s,ve,b);
  284.  
  285.  
  286.  
  287.  
  288. }
  289.  
  290.  
  291.  
  292.  
  293. int main()
  294. {
  295.  
  296. stack m;
  297. char c;
  298.  
  299. char p[90];
  300. initialize(&m);
  301. char a[100];
  302. char b[100];
  303. LinkedList ll;
  304. initLinkedList(&ll);
  305. while (1)
  306. {
  307. gets(a);
  308. char *x=topost(a);
  309. printf("%s",x);
  310. /* int y= eval(x);
  311.  
  312.  
  313. putinmemory(a);
  314.  
  315.  
  316. }*/
  317.  
  318.  
  319.  
  320. }
  321.  
  322.  
  323.  
  324.  
  325. /* if (a[i]==' ')
  326. continue;
  327. if(a[i]>='0'&&a[i]<='9')printf("%c",a[i]);
  328. if (a[i]==')'){
  329. if(isempty(&s)){
  330. printf("error");
  331. }
  332. else{
  333.  
  334.  
  335.  
  336. printf("%c",pop(&s));
  337. }
  338.  
  339.  
  340. }
  341. else{
  342. push(&s,a[i]);
  343. }
  344.  
  345. }*/
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352. return 0;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement