Octal to Decimal Conversion

Reply

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

Octal to Decimal Conversion

 
2
  #1
Nov 15th, 2009
I'm having horrible trouble trying to get this converter to work. If more code is needed, please feel free to let me know. The function I'm having trouble with getting the numbers to convert right is OctalIntoDecimal. The number it is trying to convert is a 9 in decimal...which should be 11 in octal. Same thing with 28, should be 30.

I got it to output a 2 instead of 11, which shows me it's doing something but it's not quite there yet. Any help you all could give me would be great. This assignment is due this week! Thanks...

1. genregister is a string array of 8
2. memory is also a string array holding values
3. Go to the commented Second Part and to the if statement about relative deferred add to see where I am implementing the problematic function. (Line 240)

  1. /**************/
  2. int RelativeDeferred(string numberreg) {
  3. //This function helps find the relative deferred number and converts
  4. //the string into a decimal number.
  5.  
  6. if ("000000" == numberreg)
  7. return 0;
  8. if ("000001" == numberreg)
  9. return 1;
  10. if ("000002" == numberreg)
  11. return 2;
  12. if ("000003" == numberreg)
  13. return 3;
  14. if ("000004" == numberreg)
  15. return 4;
  16. if ("000005" == numberreg)
  17. return 5;
  18. if ("000006" == numberreg)
  19. return 6;
  20. if ("000007" == numberreg)
  21. return 7;
  22.  
  23. return 0;
  24. }
  25.  
  26. /**************/
  27. string ConvertAnswer(string number) {
  28. //This function will convert the decimal back into binary for my arrays.
  29.  
  30. if ("0" == number)
  31. return "000";
  32. if ("1" == number)
  33. return "001";
  34. if ("2" == number)
  35. return "010";
  36. if ("3" == number)
  37. return "011";
  38. if ("4" == number)
  39. return "100";
  40. if ("5" == number)
  41. return "101";
  42. if ("6" == number)
  43. return "110";
  44. if ("7" == number)
  45. return "111";
  46. if ("8" == number)
  47. return "1000";
  48. if ("9" == number)
  49. return "1001";
  50. if ("10" == number)
  51. return "1010";
  52. if ("11" == number)
  53. return "1011";
  54. if ("12" == number)
  55. return "1100";
  56. if ("13" == number)
  57. return "1101";
  58. if ("14" == number)
  59. return "1110";
  60.  
  61. return 0;
  62. }
  63.  
  64. /**************/
  65. int Absolute(string numberreg) {
  66. //This function uses the register numbers to help add.
  67.  
  68. if ("000" == numberreg)
  69. return 0;
  70. if ("001" == numberreg)
  71. return 1;
  72. if ("010" == numberreg)
  73. return 2;
  74. if ("011" == numberreg)
  75. return 3;
  76. if ("100" == numberreg)
  77. return 4;
  78. if ("101" == numberreg)
  79. return 5;
  80. if ("110" == numberreg)
  81. return 6;
  82. if ("111" == numberreg)
  83. return 7;
  84.  
  85. return 0;
  86. }
  87.  
  88. /**************/
  89. int ConvertDecimal(string math) {
  90. //This function converts binary into decimal.
  91.  
  92. if ("000" == math)
  93. return 0;
  94. if ("001" == math)
  95. return 1;
  96. if ("010" == math)
  97. return 2;
  98. if ("011" == math)
  99. return 3;
  100. if ("100" == math)
  101. return 4;
  102. if ("101" == math)
  103. return 5;
  104. if ("110" == math)
  105. return 6;
  106. if ("111" == math)
  107. return 7;
  108.  
  109. return 0;
  110. }
  111.  
  112. /**************/
  113. int OctalIntoDecimal(int answer) {
  114. //This function converts octal into decimal.
  115.  
  116. double i;
  117. int number;
  118. int r;
  119. int m, x, p;
  120. double s = 0;
  121.  
  122. //number = answer;
  123. for (i = 0; number !=0; i++) {
  124. //r = number % 10;
  125. //s = s + r * (double)pow(10, i);
  126. //number = number / 10;
  127. m = pow(10, i);
  128. number = answer;
  129. x = number % m;
  130. p = x * pow(10, i-1);
  131. r += p;
  132. number /= 10;
  133. }
  134.  
  135. return number;
  136. }
  137.  
  138. /**************/
  139. void Add() {
  140. //This function sets the Opcode modes.
  141.  
  142. string opcode;
  143. string opcode2;
  144. string add;
  145. string add2;
  146. int math = 100;
  147. int math2 = 100;
  148. int answer = 100;
  149. std:: string s;
  150. std:: stringstream output;
  151. string test;
  152. int numberreg;
  153. int numberreg2;
  154. int relative;
  155. int relative2;
  156. int deferred;
  157. int deferred2;
  158. int relativedef;
  159. int relativedef2;
  160. string relativedefstr;
  161. string relativedefstr2;
  162.  
  163. //Set the variables to the correct spot for each operation.
  164. opcode = IR.substr(4, 3);
  165. add = IR.substr(7, 3);
  166. opcode2 = IR.substr(10, 3);
  167. add2 = IR.substr(13, 3);
  168.  
  169. //Print out the operations.
  170. cout << "This is the first Opcode mode: " << opcode << endl;
  171. cout << "This is the first number to add: " << add << endl;
  172. cout << "This is the second Opcode mode: " << opcode2 << endl;
  173. cout << "This is the second number to add: " << add2 << endl;
  174.  
  175. //First Part
  176. if (opcode == "000") {
  177. cout << "Immediate Add" << endl;
  178. math = ConvertDecimal(add);
  179. }
  180. if (opcode == "001") {
  181. cout << "Absolute Add" << endl;
  182. numberreg = Absolute(add);
  183. cout << numberreg << endl;
  184. math = genregister[numberreg];
  185. }
  186. if (opcode == "010") {
  187. cout << "Relative Add" << endl;
  188. numberreg = Absolute(add);
  189. cout << numberreg << endl;
  190. relative = genregister[numberreg] + numberreg;
  191. math = genregister[relative];
  192. }
  193. if (opcode == "011") {
  194. cout << "Relative Deferred Add" << endl;
  195. numberreg = Absolute(add);
  196. cout << numberreg << endl;
  197. relativedef = genregister[numberreg];
  198. cout << "Relative Deferred One: " << relativedef << endl;
  199. cout << memory[relativedef-2] << endl;
  200. math = RelativeDeferred(memory[relativedef-2]);
  201. }
  202.  
  203. //Second Half of the Equation/ReadIn
  204. if (opcode2 == "000") {
  205. //Brings in second number, adds it to the first,
  206. //then out sources the answer to convert it back to octal.
  207. cout << "Immediate Add" << endl;
  208. math2 = ConvertDecimal(add2);
  209. answer = math + math2;
  210. output << answer;
  211. test = output.str();
  212. memory[genregister[7]] = ConvertAnswer(test);
  213. genregister[7]++;
  214. }
  215. if (opcode2 == "001") {
  216. cout << "Absolute Add" << endl;
  217. numberreg2 = Absolute(add2);
  218. math2 = genregister[numberreg2];
  219. answer = math + math2;
  220. genregister[numberreg2] = math + math2;
  221. }
  222. if (opcode2 == "010") {
  223. cout << "Relative Add" << endl;
  224. numberreg2 = Absolute(add2);
  225. cout << numberreg2 << endl;
  226. relative2 = genregister[numberreg2] + numberreg2;
  227. math2 = genregister[relative2];
  228. genregister[numberreg2] = math + math2;
  229. answer = math + math2;
  230. }
  231. if (opcode2 == "011") {
  232. cout << "Relative Deferred Add" << endl;
  233. numberreg2 = Absolute(add2);
  234. cout << numberreg2 << endl;
  235. relativedef2 = genregister[numberreg2];
  236. cout << "Relative Deferred Two: " << relativedef2 << endl;
  237. cout << memory[relativedef2-2] << endl;
  238. math2 = RelativeDeferred(memory[relativedef2-2]);
  239. answer = math + math2;
  240. genregister[numberreg2] = OctalIntoDecimal(answer);
  241. }
  242.  
  243. cout << endl;
  244. cout << "First number to be added: " << math << endl;
  245. cout << "Second number to be added: " << math2 << endl;
  246. cout << "The answer to the equation: " << answer << endl;
  247. cout << endl;
  248.  
  249. }
Last edited by dagohbah; Nov 15th, 2009 at 11:39 am.
Reply With Quote Quick reply to this message  
Reply

Tags
conversion, decimal

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 960 | Replies: 0
Thread Tools Search this Thread



Tag cloud for conversion, decimal
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC