fork download
  1. #include <iostream>
  2. using namespace std;
  3. const int MYSTACK_SIZE = 100;
  4.  
  5. class myStack
  6. {
  7. private:
  8. int count; // number of items in the myStack
  9. int data[MYSTACK_SIZE];
  10. public:
  11. myStack();
  12. ~myStack();
  13. void push(const int item); // push an item on the myStack
  14. int pop(); // pop item off the myStack
  15. int getCount() const; // return count
  16. };
  17.  
  18. myStack::myStack() // constructor
  19. {
  20. count = 0; // zero the myStack
  21. }
  22.  
  23. myStack::~myStack() {} // default destructor
  24.  
  25. void myStack::push(const int item)
  26. {
  27. if (count < MYSTACK_SIZE)
  28. {
  29. data[count] = item;
  30. ++count;
  31. }
  32. else cout << "Overflow!\n";
  33. }
  34.  
  35. int myStack::pop()
  36. {
  37. if (count >0)
  38. {
  39. --count;
  40. return (data[count]);
  41. }
  42. else
  43. {
  44. cout << "Underflow!\n";
  45. return 0;
  46. }
  47. }
  48.  
  49. int myStack::getCount() const
  50. {
  51. return count;
  52. }
  53.  
  54. class number
  55. {
  56. private:
  57. int intInput;
  58. public:
  59. virtual int print_it();
  60. };
  61. //What am I supposed to do with this?
  62. int number::print_it()
  63. {
  64. return intInput;
  65. }
  66.  
  67.  
  68. class binary: public number
  69. {
  70. private:
  71. int intInput;
  72. int binRemainder;
  73. public:
  74. void print_it(int);
  75. };
  76.  
  77. void binary::print_it(int intInput)
  78. {
  79. myStack reverse;
  80. while (intInput > 0)
  81. {
  82. binRemainder = intInput % 2;
  83. intInput /= 2;
  84. reverse.push(binRemainder);
  85. }
  86. while(reverse.getCount() > 0)
  87. {
  88. cout << reverse.pop();
  89. }
  90. };
  91.  
  92. class octal: public number
  93. {
  94. private:
  95. int intInput;
  96. int octRemainder;
  97.  
  98. public:
  99. void print_it(int);
  100. };
  101.  
  102. void octal::print_it(int intInput)
  103. {
  104. myStack reverse;
  105. while(intInput > 0)
  106. {
  107. octRemainder = intInput % 8;
  108. intInput /= 8;
  109. reverse.push(octRemainder);
  110. }
  111. while (reverse.getCount() > 0)
  112. {
  113. cout << reverse.pop();
  114. }
  115. }
  116.  
  117.  
  118. class hex16: public number
  119. {
  120. private:
  121. int intInput;
  122. int hexRemainder;
  123. public:
  124. void print_it(int);
  125. };
  126.  
  127. void hex16::print_it(int intInput)
  128. {
  129. myStack reverse;
  130. int pop;
  131. while (intInput > 0)
  132. {
  133. hexRemainder = intInput % 16;
  134. intInput /= 16;
  135. reverse.push(hexRemainder);
  136. }
  137. while (reverse.getCount() > 0)
  138. {
  139. pop = reverse.pop();
  140. if (pop > 9)
  141. {
  142. cout << char('A' + pop - 10); // displays digit > 9 as char
  143. }
  144. else
  145. {
  146. cout << pop;
  147. }
  148. }
  149. }
  150.  
  151. int main() {
  152. int intInput;
  153. cout << "Enter a decimal to be converted: ";
  154. cin >> intInput;
  155.  
  156. binary bin;
  157. octal oct;
  158. hex16 hexa;
  159. number * convert1 = &bin;
  160. number * convert2 = &oct;
  161. number * convert3 = &hexa;
  162. convert1->print_it();
  163. convert2->print_it();
  164. convert3->print_it();
  165. cout << "Binary: " << convert1->print_it() << "\nOctal: " << convert2->print_it() << "\nHexadecimal: " << convert3->print_it() << endl;
  166.  
  167.  
  168. cout << endl;
  169. system("pause");
  170. return 0;
  171. }
  172.  
Success #stdin #stdout #stderr 0s 3304KB
stdin
Standard input is empty
stdout
Enter a decimal to be converted: Binary: 134521805
Octal: -1216862760
Hexadecimal: -1077901244

stderr
sh: pause: not found