Advertisement
a53

EvaluareExpresie

a53
May 21st, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <ctype.h>
  5. #include <cmath>
  6. #define MAX 100
  7. #define EMPTY -1
  8. using namespace std;
  9. struct stack
  10. {
  11. char data[MAX];
  12. long long int top;
  13. };
  14.  
  15. int isempty(struct stack *s)
  16. {
  17. return (s->top==EMPTY)?1:0;
  18. }
  19.  
  20. void emptystack(struct stack* s)
  21. {
  22. s->top=EMPTY;
  23. }
  24.  
  25. void push(struct stack* s,int item)
  26. {
  27. if(s->top==(MAX-1))
  28. {
  29. printf("\nSTACK FULL");
  30. }
  31. else
  32. {
  33. ++s->top;
  34. s->data[s->top]=item;
  35. }
  36. }
  37.  
  38. char pop(struct stack* s)
  39. {
  40. char ret=(char)EMPTY;
  41. if(!isempty(s))
  42. {
  43. ret=s->data[s->top];
  44. --s->top;
  45. }
  46. return ret;
  47. }
  48.  
  49. void display(struct stack s)
  50. {
  51. while(s.top != EMPTY)
  52. {
  53. printf("\n%d",s.data[s.top]);
  54. s.top--;
  55. }
  56. }
  57.  
  58. int isoperator(char e)
  59. {
  60. if(e=='+'||e=='-'||e=='*'||e=='^')
  61. return 1;
  62. else
  63. return 0;
  64. }
  65.  
  66.  
  67. int priority(char e)
  68. {
  69. int pri=0;
  70. if(e=='^')
  71. pri=3;
  72. else
  73. if(e=='*')
  74. pri=2;
  75. else
  76. if(e=='+'||e=='-')
  77. pri=1;
  78. return pri;
  79. }
  80.  
  81. void infix2postfix(char* infix, char * postfix, int insertspace)
  82. {
  83. char *i,*p;
  84. struct stack X;
  85. char n1;
  86. emptystack(&X);
  87. i=&infix[0];
  88. p=&postfix[0];
  89. while(*i)
  90. {
  91. while(*i==' '||*i=='\t')
  92. {
  93. i++;
  94. }
  95. if(isdigit(*i)||isalpha(*i))
  96. {
  97. while(isdigit(*i)||isalpha(*i))
  98. {
  99. *p=*i;
  100. p++;
  101. i++;
  102. }
  103. /*SPACE CODE*/
  104. if(insertspace)
  105. {
  106. *p=' ';
  107. p++;
  108. }
  109. /*END SPACE CODE*/
  110. }
  111. if(*i=='(' )
  112. {
  113. push(&X,*i);
  114. i++;
  115. }
  116. if(*i==')')
  117. {
  118. n1=pop(&X);
  119. while(n1!='(' )
  120. {
  121. *p=n1;
  122. p++;
  123. /*SPACE CODE*/
  124. if(insertspace)
  125. {
  126. *p=' ';
  127. p++;
  128. }
  129. /*END SPACE CODE*/
  130. n1=pop(&X);
  131. }
  132. i++;
  133. }
  134. if(isoperator(*i))
  135. {
  136. if(isempty(&X))
  137. push(&X,*i);
  138. else
  139. {
  140. n1=pop(&X);
  141. while(priority(n1)>=priority(*i))
  142. {
  143. *p=n1;
  144. p++;
  145. /*SPACE CODE*/
  146. if(insertspace)
  147. {
  148. *p =' ';
  149. p++;
  150. }
  151. /*END SPACE CODE*/
  152. n1=pop(&X);
  153. }
  154. push(&X,n1);
  155. push(&X,*i);
  156. }
  157. i++;
  158. }
  159. }
  160. while(!isempty(&X))
  161. {
  162. n1=pop(&X);
  163. *p=n1;
  164. p++;
  165. /*SPACE CODE*/
  166. if(insertspace)
  167. {
  168. *p=' ';
  169. p++;
  170. }
  171. /// END SPACE CODE
  172. }
  173. *p ='\0';
  174. }
  175.  
  176. long long int evaluate(char *postfix)
  177. {
  178. char *p;
  179. long long int S[100];
  180. int varf=0;
  181. long long int nr=0,op1,op2,result;
  182. p=&postfix[0]; /// In p avem expresia in forma postfixata
  183. ///cout<<p<<"I"<<endl;
  184. while(*p!='\0') /// Cat timp n-am ajuns la sfarsitul expresiei
  185. {
  186. /// removes tabs and spaces
  187. if(*p==' '||*p=='\t')
  188. {
  189. p++;
  190. }
  191. /// if is digit
  192. if(isdigit(*p))
  193. {
  194. nr=(long long int)(*p-48);
  195. p++;
  196. while(isdigit(*p))
  197. nr=nr*10+(*p-48),++p;
  198. S[++varf]=nr;
  199. }
  200. else
  201. {
  202. /// it is an operator
  203. if(varf)
  204. op1=S[varf--];
  205. if(varf)
  206. op2=S[varf--];
  207. ///cout<<"OPERATIA: "<<op1<<' '<<*p<<' '<<op2<<endl;
  208. ///if(*p!='+'||*p!='-'||*p!='*'||*p!='^')
  209. switch(*p)
  210. {
  211. case '+':
  212. result=op2+op1;
  213. break;
  214.  
  215. case '-':
  216. result=op2-op1;
  217. break;
  218.  
  219. case '^':
  220. result=(long long int)pow(op2,op1);
  221. break;
  222.  
  223. case '*':
  224. result=op2*op1;
  225. break;
  226.  
  227. default:
  228. printf("\nInvalid Operator");
  229. return 0;
  230. }
  231. S[++varf]=result;
  232. }
  233. p++;
  234. }
  235. result=S[varf];
  236. return result;
  237. }
  238.  
  239. int main()
  240. {
  241. char in[50]={0},post[100]={0};
  242. FILE *f=fopen("evaluareexpresie.in","r");
  243. fscanf(f,"%s",in);
  244. fclose(f);
  245. ///cout<<"S= "<<in<<endl;
  246. infix2postfix(in,post,1);
  247. FILE *g=fopen("evaluareexpresie.out","w");
  248. unsigned int L=strlen(post);
  249. while(post[L-1]!='+'&&post[L-1]!='-'&&post[L-1]!='*'&&post[L-1]!='^')
  250. --L;
  251. post[L]='\0';
  252. fprintf(g,"%s\n",post);
  253. fprintf(f,"%lld\n",evaluate(post));
  254. fclose(g);
  255. return 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement