Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. #include "expression.hpp"
  2. // Mateusz Gil
  3. vector< pair < string, double> > Variable::values;
  4.  
  5. Constant::Constant(double d ) : value(d){}
  6.  
  7. Pi::Pi() : Constant(3.141592653589793238462643383279)
  8. {
  9. }
  10. double Pi::calc()
  11. {
  12. return this->value;
  13. }
  14.  
  15. string Pi::description()
  16. {
  17. return "pi";
  18. }
  19.  
  20. Fi::Fi(): Constant(1.618033988749894848)
  21. {
  22.  
  23. }
  24.  
  25. double Fi::calc()
  26. {
  27. return this->value;
  28. }
  29.  
  30. string Fi::description()
  31. {
  32. return "fi";
  33. }
  34. E::E() : Constant (2.718281828459045235360287471352662497757247094)
  35. {
  36.  
  37. }
  38.  
  39. double E::calc()
  40. {
  41. return this->value;
  42. }
  43.  
  44. string E::description()
  45. {
  46. return "e";
  47. }
  48.  
  49. Number::Number(double x)
  50. {
  51. this->value = x;
  52. }
  53.  
  54. double Number::calc()
  55. {
  56. return this->value;
  57. }
  58.  
  59. string Number::description()
  60. {
  61. return to_string(this->value);
  62. }
  63.  
  64. Variable::Variable(string name)
  65. {
  66. this->name = name;
  67. // setName(name);
  68. }
  69.  
  70. string Variable::description()
  71. {
  72. return this->name;
  73. }
  74.  
  75. double Variable::getValue(string name)
  76. {
  77. for (int i = 0; i < values.size(); i++)
  78. {
  79. if(values[i].first==name)
  80. return values[i].second;
  81. }
  82. return -1.0;
  83. }
  84.  
  85. double Variable::Size()
  86. {
  87.  
  88. return values[1].second;
  89. }
  90.  
  91. void Variable::setValue(string name, double x)
  92. {
  93. bool b = true;
  94.  
  95. for (int i = 0; i < values.size(); i++)
  96. {
  97. if(values[i].first==name)
  98. {
  99. values[i].second = x;
  100. b = false;
  101. break;
  102. }
  103. }
  104. if(b)
  105. {
  106. values.push_back(make_pair(name, x));
  107. }
  108. }
  109.  
  110. void Variable::setName(string name)
  111. {
  112. values.push_back(make_pair(name, 0.0));
  113. }
  114. double Variable::calc()
  115. {
  116. return getValue(this->name);
  117. }
  118.  
  119. Sin::Sin(Expression *e1)
  120. {
  121. this->e = e1;
  122. }
  123.  
  124. double Sin::calc()
  125. {
  126. return sin(this->e->calc());
  127. }
  128.  
  129. string Sin::description()
  130. {
  131. string temp = "sin(";
  132. temp += this->e->description();
  133. temp += ")";
  134. return temp;
  135. }
  136.  
  137. Cos::Cos(Expression *e1): e(e1)
  138. {
  139. // this->e = e1;
  140. }
  141.  
  142. double Cos::calc()
  143. {
  144. return cos(this->e->calc());
  145. }
  146.  
  147. string Cos::description()
  148. {
  149. string temp = "cos(";
  150. temp += this->e->description();
  151. temp += ")";
  152. return temp;
  153. }
  154.  
  155. Exp::Exp(Expression *e1): e(e1)
  156. {
  157.  
  158. }
  159.  
  160. double Exp::calc()
  161. {
  162. E *temp = new E();
  163. return exp(this->e->calc());
  164. }
  165.  
  166. string Exp::description()
  167. {
  168. string temp;
  169. temp = "e^";
  170. temp += this->e->description();
  171. temp += ')';
  172. return temp;
  173.  
  174. }
  175.  
  176. Ln::Ln(Expression *e): e(e)
  177. {
  178. }
  179.  
  180. double Ln::calc()
  181. {
  182. return log(this->e->calc());
  183. }
  184.  
  185. string Ln::description()
  186. {
  187. string temp;
  188. temp = "Ln(";
  189. temp += this->e->description();
  190. temp += ")";
  191. return temp;
  192. }
  193.  
  194. Abs::Abs(Expression *e): e(e)
  195. {
  196.  
  197. }
  198.  
  199. double Abs::calc()
  200. {
  201. return abs(this->e->calc());
  202. }
  203.  
  204. string Abs::description()
  205. {
  206. string temp;
  207. temp = '|';
  208. temp += this->e->description();
  209. temp += '|';
  210. return temp;
  211. }
  212.  
  213. Opposite::Opposite(Expression *e): e(e)
  214. {
  215.  
  216. }
  217.  
  218. double Opposite::calc()
  219. {
  220. return -1 * this->e->calc();
  221. }
  222.  
  223. string Opposite::description()
  224. {
  225. string temp;
  226. temp = "-1(";
  227. temp += this->e->description();
  228. temp += ')';
  229.  
  230. return temp;
  231. }
  232.  
  233. Resiprocal::Resiprocal(Expression *e): e(e)
  234. {
  235.  
  236. }
  237.  
  238.  
  239. double Resiprocal::calc()
  240. {
  241. if(this->calc()==0)
  242. {
  243. throw invalid_argument("Invalid syntax.");
  244. }
  245. return 1.0 / this->e->calc();
  246. }
  247.  
  248. string Resiprocal::description()
  249. {
  250. string temp;
  251. temp = "1/(";
  252. temp += this->e->description();
  253. temp += ')';
  254. return temp;
  255. }
  256.  
  257. Add::Add(Expression *e1, Expression *e2): e1(e1), e2(e2)
  258. {
  259.  
  260. }
  261.  
  262. double Add::calc()
  263. {
  264. return this->e1->calc() + this->e2->calc();
  265. }
  266.  
  267. string Add::description()
  268. {
  269. string temp;
  270. temp = "(";
  271. temp += this->e1->description();
  272. temp += '+';
  273. temp += this->e2->description();
  274. temp += ')';
  275. return temp;
  276. }
  277.  
  278. Sub::Sub(Expression *e1, Expression *e2): e1(e1), e2(e2)
  279. {
  280.  
  281. }
  282.  
  283. double Sub::calc()
  284. {
  285. return this->e1->calc() - this->e2->calc();
  286. }
  287.  
  288. string Sub::description()
  289. {
  290. string temp;
  291. temp = "(";
  292. temp += this->e1->description();
  293. temp += '-';
  294. temp += this->e2->description();
  295. temp += ')';
  296. return temp;
  297. }
  298.  
  299. Mult::Mult(Expression *e1, Expression *e2): e1(e1), e2(e2)
  300. {
  301.  
  302. }
  303.  
  304. double Mult::calc()
  305. {
  306. return this->e1->calc() * this->e2->calc();
  307. }
  308.  
  309. string Mult::description()
  310. {
  311. string temp;
  312. temp = this->e1->description();
  313. temp += '*';
  314. temp += this->e2->description();
  315. return temp;
  316. }
  317.  
  318. Div::Div(Expression *e1, Expression *e2)
  319. {
  320. /// if( e2->calc()==0)
  321. // throw invalid_argument("Dividing by 0!!!");
  322.  
  323. this->e1 = e1;
  324. this->e2 = e2;
  325. }
  326.  
  327. double Div::calc()
  328. {
  329. return this->e1->calc() / this->e2->calc();
  330. }
  331.  
  332. string Div::description()
  333. {
  334. string temp;
  335. temp = this->e1->description();
  336. temp += '/';
  337. temp += this->e2->description();
  338. return temp;
  339. }
  340.  
  341. Log::Log(Expression *e1, Expression *e2)
  342. {
  343. if(e1->calc() <= 0)
  344. throw invalid_argument("Invalid base of logarithm!!!");
  345.  
  346. this->e1 = e1;
  347. this->e2 = e2;
  348. }
  349.  
  350. double Log::calc()
  351. {
  352. return log(this->e1->calc()) / log(this->e2->calc());
  353. }
  354.  
  355. string Log::description()
  356. {
  357. string temp;
  358. temp = "Log(";
  359. temp += this->e1->description();
  360. temp += ',';
  361. temp += this->e2->description();
  362. temp += ')';
  363. return temp;
  364. }
  365.  
  366. Mod::Mod(Expression *e1, Expression *e2): e1(e1), e2(e2)
  367. {
  368.  
  369. }
  370.  
  371. double Mod::calc()
  372. {
  373. return (int)(this->e1->calc())%(int)(this->e2->calc());
  374. }
  375.  
  376. string Mod::description()
  377. {
  378. string temp;
  379. temp = '(';
  380. temp += this->e1->description();
  381. temp += ")%(";
  382. temp += this->e2->description();
  383. temp += ')';
  384. return temp;
  385. }
  386.  
  387. Pow::Pow(Expression *e1, Expression *e2): e1(e1), e2(e2)
  388. {
  389.  
  390. }
  391.  
  392. double Pow::calc()
  393. {
  394. return pow(this->e1->calc(), this->e2->calc());
  395. }
  396.  
  397. string Pow::description()
  398. {
  399. string temp;
  400. temp = '(';
  401. temp += this->e1->description();
  402. temp += ")^(";
  403. temp += this->e2->description();
  404. temp += ')';
  405. return temp;
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement