how do perform multiple math operations in one single output?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 1
Reputation: wannabeengr is an unknown quantity at this point 
Solved Threads: 0
wannabeengr wannabeengr is offline Offline
Newbie Poster

how do perform multiple math operations in one single output?

 
0
  #1
Nov 4th, 2009
imtrying to do a math calculation that involves multiple math operation ex((a+b)*(sin(a)/sin(b)) something like this but whenever i execute the code its giving me 3 answers this is the code hoping you could help me it very urgent though thanks!!


  1. #include <iostream.h>
  2. #include <math.h>
  3. #include <string.h>
  4.  
  5.  
  6.  
  7.  
  8. int nVar = 0;
  9. char varName[50];
  10. double varValue[50];
  11.  
  12. double getValue(char ch);
  13. void setValue(char ch, double d);
  14.  
  15. int main()
  16. {
  17. cout << "This program enables the user to perform Math Functions such as fabs, sqrt, pow, log, log10, sin cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh, ceil, floor, mod. Just input a character(a - z ) followed by an equal sign and the numerical value then press enter. Then in the preceding line input the function you want to use followed by a parenthesis and the alpha character then press enter. To quit, type quit then press enter:\n\n\n";
  18.  
  19. for(int i = 0; i < 50; i++)
  20. {
  21. varName[i] = '(';
  22. }
  23.  
  24. char ch;
  25. char input[6];
  26. double operand1;
  27. double operand2;
  28. double res;
  29.  
  30. while(1)
  31. {
  32. cout << ">>";
  33.  
  34. cin >> ch;
  35.  
  36.  
  37. if(ch =='q'|| ch =='Q')
  38. {
  39. break;
  40. }
  41.  
  42. if(ch =='quit')
  43. {
  44. break;
  45. }
  46.  
  47.  
  48. input[0] = ch;
  49.  
  50. operand1 = getValue(ch);
  51.  
  52. cin >> ch;
  53. input[1] = ch;
  54. if(ch == '=')
  55. {
  56. cin >> operand1;
  57.  
  58. setValue(input[0], operand1);
  59.  
  60. cout << input[0] << "=\n\n\n\t" <<operand1 << "\n";
  61.  
  62. }
  63. else
  64. {
  65. switch(ch)
  66. {
  67. case'+':
  68.  
  69. cin >> ch;
  70.  
  71. operand2 = getValue(ch);
  72.  
  73. res = operand1 + operand2;
  74.  
  75. cout << "ans=\n\n\n\t" << res << "\n";
  76. break;
  77.  
  78. case'-':
  79.  
  80. cin >> ch;
  81.  
  82. operand2 = getValue(ch);
  83.  
  84. res = operand1 - operand2;
  85.  
  86. cout << "ans=\n\n\n\t" << res << "\n";
  87. break;
  88.  
  89. case'*':
  90.  
  91. cin >> ch;
  92.  
  93. operand2 = getValue(ch);
  94.  
  95. res = operand1 * operand2;
  96.  
  97. cout << "ans=\n\n\n\t" << res << "\n";
  98. break;
  99.  
  100. case'/':
  101.  
  102. cin >> ch;
  103.  
  104. operand2 = getValue(ch);
  105.  
  106. res = operand1 / operand2;
  107.  
  108. cout << "ans=\n\n\n\t" << res << "\n";
  109. break;
  110.  
  111. default:
  112.  
  113.  
  114. cin >> input[2];
  115. int i = 3;
  116. while (ch != '(')
  117. {
  118. cin >> ch;
  119. input [i] = ch;
  120. i++;
  121. }
  122.  
  123.  
  124. input[i-1] = '\0';
  125.  
  126. if(strcmp(input, "sin") == 0)
  127. {
  128. cin >> ch;
  129.  
  130. operand1 = getValue(ch);
  131.  
  132. res = sin(operand1);
  133.  
  134. cout << "ans=\n\n\n\t" << res << "\n";
  135.  
  136. cin >> ch;
  137.  
  138. }
  139.  
  140. if(strcmp(input, "cos") == 0)
  141. {
  142. cin >> ch;
  143.  
  144. operand1 = getValue(ch);
  145.  
  146. res = cos(operand1);
  147.  
  148. cout << "ans=\n\n\n\t" << res << "\n";
  149.  
  150. cin >> ch;
  151. }
  152.  
  153. if(strcmp(input, "tan") == 0)
  154. {
  155. cin >> ch;
  156.  
  157. operand1 = getValue(ch);
  158.  
  159. res = tan(operand1);
  160.  
  161. cout << "ans=\n\n\n\t" << res << "\n";
  162.  
  163. cin >> ch;
  164. }
  165.  
  166. if(strcmp(input, "exp") == 0)
  167. {
  168. cin >> ch;
  169.  
  170. operand1 = getValue(ch);
  171.  
  172. res = exp(operand1);
  173.  
  174. cout << "ans=\n\n\n\t" << res << "\n";
  175.  
  176. cin >> ch;
  177. }
  178.  
  179. if(strcmp(input, "log") == 0)
  180. {
  181. cin >> ch;
  182.  
  183. operand1 = getValue(ch);
  184.  
  185. res = log(operand1);
  186.  
  187. cout << "ans=\n\n\n\t" << res << "\n";
  188.  
  189. cin >> ch;
  190. }
  191.  
  192. if(strcmp(input, "pow") == 0)
  193. {
  194. cin >> ch;
  195.  
  196. operand1 = getValue(ch);
  197.  
  198. cin >> ch;
  199.  
  200. cin >> ch;
  201.  
  202. operand2 = getValue(ch);
  203.  
  204. res = pow(operand1, operand2);
  205.  
  206. cout << "ans=\n\n\n\t" << res << "\n";
  207.  
  208. cin >> ch;
  209. }
  210.  
  211.  
  212. if(strcmp(input, "asin") == 0)
  213. {
  214. cin >> ch;
  215.  
  216. operand1 = getValue(ch);
  217.  
  218. res = asin(operand1);
  219.  
  220. cout << "ans=\n\n\n\t" << res << "\n";
  221.  
  222. cin >> ch;
  223.  
  224. }
  225.  
  226. if(strcmp(input, "acos") == 0)
  227. {
  228. cin >> ch;
  229.  
  230. operand1 = getValue(ch);
  231.  
  232. res = acos(operand1);
  233.  
  234. cout << "ans=\n\n\n\t" << res << "\n";
  235.  
  236. cin >> ch;
  237. }
  238.  
  239. if(strcmp(input, "atan") == 0)
  240. {
  241. cin >> ch;
  242.  
  243. operand1 = getValue(ch);
  244.  
  245. res = atan(operand1);
  246.  
  247. cout << "ans=\n\n\n\t" << res << "\n";
  248.  
  249. cin >> ch;
  250.  
  251. }
  252.  
  253. if(strcmp(input, "fabs") == 0)
  254. {
  255. cin >> ch;
  256.  
  257. operand1 = getValue(ch);
  258.  
  259. res = fabs(operand1);
  260.  
  261. cout << "ans=\n\n\n\t" << res << "\n";
  262.  
  263. cin >> ch;
  264.  
  265. }
  266.  
  267. if(strcmp(input, "sqrt") == 0)
  268. {
  269. cin >> ch;
  270.  
  271. operand1 = getValue(ch);
  272.  
  273. res = sqrt(operand1);
  274.  
  275. cout << "ans=\n\n\n\t" << res << "\n";
  276.  
  277. cin >> ch;
  278.  
  279. }
  280.  
  281. if(strcmp(input, "ceil") == 0)
  282. {
  283. cin >> ch;
  284.  
  285. operand1 = getValue(ch);
  286.  
  287. res = ceil(operand1);
  288.  
  289. cout << "ans=\n\n\n\t" << res << "\n";
  290.  
  291. cin >> ch;
  292.  
  293. }
  294.  
  295. if(strcmp(input, "sinh") == 0)
  296. {
  297. cin >> ch;
  298.  
  299. operand1 = getValue(ch);
  300.  
  301. res = sinh(operand1);
  302.  
  303. cout << "ans=\n\n\n\t" << res << "\n";
  304.  
  305. cin >> ch;
  306.  
  307. }
  308. if(strcmp(input, "cosh") == 0)
  309. {
  310. cin >> ch;
  311.  
  312. operand1 = getValue(ch);
  313.  
  314. res = cosh(operand1);
  315.  
  316. cout << "ans=\n\n\n\t" << res << "\n";
  317.  
  318. cin >> ch;
  319.  
  320. }
  321. if(strcmp(input, "tanh") == 0)
  322. {
  323. cin >> ch;
  324.  
  325. operand1 = getValue(ch);
  326.  
  327. res = tanh(operand1);
  328.  
  329. cout << "ans=\n\n\n\t" << res << "\n";
  330.  
  331. cin >> ch;
  332.  
  333. }
  334.  
  335. if(strcmp(input, "fmod") == 0)
  336. {
  337. cin >> ch;
  338.  
  339. operand1 = getValue(ch);
  340.  
  341. res = fmod(operand1, operand2);
  342.  
  343. cout << "ans=\n\n\n\t" << res << "\n";
  344.  
  345. cin >> ch;
  346.  
  347. }
  348.  
  349. if(strcmp(input, "floor") == 0)
  350. {
  351. cin >> ch;
  352.  
  353. operand1 = getValue(ch);
  354.  
  355. res = floor(operand1);
  356.  
  357. cout << "ans=\n\n\n\t" << res << "\n";
  358.  
  359. cin >> ch;
  360.  
  361. }
  362.  
  363. if(strcmp(input, "log10") == 0)
  364. {
  365. cin >> ch;
  366.  
  367. //cin >> ch;
  368.  
  369. operand1 = getValue(ch);
  370.  
  371. res = log10(operand1);
  372.  
  373. cout << "ans=\n\n\n\t" << res << "\n";
  374.  
  375. cin >> ch;
  376. }
  377. if(strcmp(input, "atan2") == 0)
  378. {
  379. cin >> ch;
  380.  
  381. operand1 = getValue(ch);
  382.  
  383. cin >> ch;
  384.  
  385. cin >> ch;
  386.  
  387. res = atan2(operand1, operand2);
  388.  
  389. cout << "ans=\n\n\n\t" << res << "\n";
  390.  
  391. cin >> ch;
  392. }
  393.  
  394. break;
  395. }
  396. }
  397. }
  398. return 0;
  399. }
  400.  
  401.  
  402. double getValue(char ch)
  403. {
  404. for(int i = 0; i < nVar; i++)
  405. {
  406. if(ch == varName[i])
  407. {
  408. return varValue[i];
  409. }
  410. }
  411. varName[nVar] = ch;
  412. varValue[nVar] = 0.0;
  413.  
  414. nVar++;
  415. return 0.0;
  416. }
  417.  
  418.  
  419. void setValue(char ch, double d)
  420. {
  421. for(int i = 0; i <nVar; i++)
  422. {
  423. if(ch == varName[i])
  424. {
  425. varValue[i] = d;
  426. }
  427. }
  428. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC