Guest User

Untitled

a guest
Jan 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. #include <string>
  5.  
  6. #define MAX_SIZE 20
  7.  
  8. using namespace std;
  9.  
  10. template<class T> class Stack
  11. {
  12. private:
  13. T item[MAX_SIZE];
  14. int top;
  15.  
  16. public:
  17. Stack()
  18. {
  19. top = -1;
  20. }
  21.  
  22. void push(T data)
  23. {
  24. if(!this->is_full())
  25. item[++top] = data;
  26. else
  27. {
  28. cout << "Stack Error" << endl;
  29. exit(10);
  30. }
  31. }
  32.  
  33. T pop()
  34. {
  35. if(!this->is_empty())
  36. {
  37. return item[top--];
  38. }
  39. else
  40. {
  41. cout << "Stack is Empty" << endl;
  42. exit(11);
  43. }
  44. }
  45.  
  46. int size()
  47. {
  48. return top + 1;
  49. }
  50.  
  51. bool is_empty()
  52. {
  53. if(top == -1)
  54. return true;
  55. else
  56. return false;
  57. }
  58.  
  59. bool is_full()
  60. {
  61. if(top == MAX_SIZE - 1)
  62. return true;
  63. else
  64. return false;
  65. }
  66.  
  67. void display()
  68. {
  69. for(int i = 0; i < this->size(); i++)
  70. {
  71. cout << item[i] << " ";
  72. }
  73. cout << endl;
  74. }
  75.  
  76. T return_top()
  77. {
  78. return item[top];
  79. }
  80. };
  81.  
  82. class Convert
  83. {
  84. private:
  85. bool num_flag;
  86. bool two_digit_flag;
  87.  
  88. public:
  89. Convert();
  90. string return_with_bracket(string infix);
  91. void to_Postfix(string infix,char postfix[]);
  92. bool prcd(char op1, char op2);
  93. int isOperand(char op);
  94. int isOperator(char op);
  95. bool return_flag()
  96. {
  97. return num_flag;
  98. }
  99. };
  100.  
  101. Convert::Convert()
  102. {
  103. this->num_flag = false;
  104. this->two_digit_flag = false;
  105. }
  106.  
  107. string Convert::return_with_bracket(string infix)
  108. {
  109. return("(" + infix + ")");
  110. }
  111.  
  112. bool Convert::prcd(char op1, char op2)
  113. {
  114. if((op1 == '+' || op1 == '-' || op1 == '*' || op1 == '/') && op2 == '(')
  115. return true;
  116. if(op1=='+' && op2=='+')
  117. return true;
  118. if(op1=='-' && op2=='-')
  119. return false;
  120. if(op1=='-' && op2=='+')
  121. return false;
  122. if(op1=='+' && op2=='-')
  123. return false;
  124. if(op1=='/' && op2=='/')
  125. return false;
  126. if(op1=='/' && (op2=='-' || op2=='+'))
  127. return true;
  128. if(op1=='*' && (op2=='+' || op2=='-'))
  129. return true;
  130. if((op1 == '-' || op1 == '+') && (op2 =='*' || op2 == '/'))
  131. return false;
  132. if((op1 == '$' || op1 == '+') && (op2 =='*' || op2 == '/' || op2=='-'))
  133. return true;
  134. if((op1 == '-' || op1 == '+' || op1 =='*' || op1 == '/')&& op2=='^')
  135. return false;
  136. if(op1 == '^' && ( op2 == '+' || op2 =='*' || op2 == '/' || op2=='-'))
  137. return false;
  138. }
  139.  
  140. int Convert::isOperand(char op)
  141. {
  142. return(op >= '0' && op <= '9');
  143. }
  144.  
  145. int Convert::isOperator(char op)
  146. {
  147. return(op =='+' || op =='-' || op == '/' || op =='*' || op =='^');
  148. }
  149.  
  150. void Convert::to_Postfix(string infix, char postfix[])
  151. {
  152. int position, outpos=0;
  153. char c;
  154. int count = 0;
  155. char temp;
  156. char stacktop;
  157. Stack<char> stack;
  158.  
  159. for(position = 0; (c = infix[position]) != ''; position++)
  160. {
  161.  
  162. if(this->isOperand(c))
  163. {
  164. postfix[outpos++] = c;
  165. this->num_flag = true;
  166. count++;
  167.  
  168. if(count >= 2)
  169. {
  170. this->two_digit_flag = true;
  171. }
  172. }
  173. else if(this->isOperator(c))
  174. {
  175. count = 0;
  176. if(isOperator(infix[position]) && isOperator(infix[position + 1]))
  177. {
  178. cout << " '' aMissing argument in between " << infix[position] << " and " << infix[position + 1] << " in column " << position + 1 << endl;
  179. exit(9);
  180. }
  181. if(this->prcd(c, stacktop))
  182. {
  183. stacktop = stack.return_top();
  184. stack.push(c);
  185. stacktop = c;
  186. }
  187. else
  188. {
  189. while(true)
  190. {
  191. temp = stack.pop();
  192. postfix[outpos++] = temp;
  193. stacktop = stack.return_top();
  194.  
  195. if(prcd(c, stacktop) || stacktop == '(')
  196. break;
  197. }
  198. stack.push(c);
  199. stacktop = stack.return_top();
  200. }
  201. }
  202. else if(c == '(')
  203. {
  204. count = 0;
  205. stack.push(c);
  206. stacktop = stack.return_top();
  207. }
  208. else if(c == ')')
  209. {
  210. count = 0;
  211. while(1)
  212. {
  213. if(stack.size() == 0)
  214. {
  215. cout << "Warning!! Number of ')' is greater than '('" << endl;
  216. exit(2);
  217. }
  218. temp = stack.pop();
  219. if(temp != '(')
  220. {
  221. postfix[outpos++] = temp;
  222. }
  223. else
  224. {
  225. break;
  226. }
  227. }
  228. stacktop = stack.return_top();
  229. }
  230. else
  231. {
  232. cout << "Invalid input";
  233. exit(3);
  234. }
  235. if(infix[position] == ')' && infix[position + 1] == '(')
  236. {
  237. stack.push('*');
  238. stacktop = stack.return_top();
  239. }
  240. }
  241. if(stack.size() != 0)
  242. {
  243. cout << "Warning!!Number of '(' is greater than ')'" << endl;
  244. // exit(6);
  245. }
  246. if(!this->return_flag())
  247. {
  248. cout << "You must Enter Numeric value for calculation" << endl;
  249. cout << "This program cannot perform operations on variables";
  250. exit(5);
  251. }
  252. if(this->two_digit_flag)
  253. {
  254. cout << "Sory! Althoug u may have entered right string" << endl;
  255. cout << "this program is only for single digit operation" << endl;
  256. exit(8);
  257. }
  258. postfix[outpos] = '';
  259. }
  260.  
  261. class Evaluate
  262. {
  263. public:
  264. double eval(char expr[], Convert &);
  265. double oper(int symb, double op1, double op2);
  266. };
  267.  
  268. double Evaluate::oper(int symb, double op1, double op2)
  269. {
  270. switch(symb)
  271. {
  272. case '+': return (op1 + op2);
  273. case '-': return (op1 - op2);
  274. case '*': return (op1 * op2);
  275. case '/': return (op1 / op2);
  276. case '^': return (pow(op1, op2));
  277. }
  278. }
  279.  
  280. double Evaluate::eval(char expr[], Convert &convert)
  281. {
  282. int c, position;
  283. char temp1;
  284. int count = 0;
  285. double opnd1, opnd2, value;
  286. Stack<double> stack;
  287. for(position = 0; (c = expr[position]) != ''; position++)
  288. {
  289. if(convert.isOperand(c))
  290. {
  291. temp1 = double(c - '0');
  292. stack.push(temp1);
  293. }
  294. else
  295. {
  296. opnd2 = stack.pop();
  297. if(stack.size() == 0)
  298. {
  299. cout << "This program cannot process unary operation";
  300. exit(1);
  301. }
  302. opnd1 = stack.pop();
  303. value = oper(c, opnd1, opnd2);
  304. stack.push(value);
  305. }
  306. }
  307. if(stack.size() >= 2)
  308. {
  309. cout << "Sory! this program cannot calculate this" << endl;
  310. cout << "Enter +, *, /, - or ^ between bracket" << endl;
  311. exit(4);
  312. }
  313. return (stack.pop());
  314. }
  315.  
  316. int main()
  317. {
  318. Convert convert;
  319. Evaluate evaluate;
  320. string bracketted_infix;
  321. char infix[50], postfix[50];
  322. char choice;
  323.  
  324. while(1)
  325. {
  326. cout << "Enter string: ";
  327. cin >> infix;
  328. cout << endl;
  329. cout << "Entered String: " << infix << endl;
  330.  
  331. bracketted_infix = convert.return_with_bracket(infix);
  332. convert.to_Postfix(bracketted_infix, postfix);
  333.  
  334. cout << "Equivalent Postfix string: " << postfix << endl;
  335. cout << "RESULT: ";
  336. cout << evaluate.eval(postfix, convert);
  337. cout << "nCalculate another string?(y/n) ";
  338. cin >> choice;
  339. cout << endl;
  340.  
  341. if(choice == 'n')
  342. break;
  343. }
  344. return 0;
  345. }
Add Comment
Please, Sign In to add comment