我怎么能阻止我的整数显示为十六进制
How can I stop my integers from displaying as HEX?
我正在练习一些实现不同数据结构的代码。对于这个例子,我试图实现一个简单的堆栈数据结构。到目前为止,它按预期工作,但我不断得到十六进制字符时,试图显示我的堆栈。有人能帮我弄清楚为什么会这样吗?
我也在努力更好地组织我的代码,任何已经参与这个行业的人都可以给我一些建设性的批评,关于我到目前为止所写的代码。谢谢。
#include <iostream>
using namespace std;
// stack_MAX == maximum height of stack
const int stack_MAX = 10;
class stack{
public:
stack(){
//Constructor initializes top of stack
top = -1;
}
bool isFull(int top){
//isFull() will check to make sure stack is not full
//Will return TRUE if stack is FULL, and FALSE if
//stack is NOT FULL
if(top == stack_MAX - 1)
return true;
else
return false;
}
bool isEmpty(int top){
//isEmpty() will check to make sure stack is not empty
//Will return TRUE if stack is EMPTY, and FALSE if
//stack is NOT EMPTY
if(top == -1)
return true;
else
return false;
}
void push(int x){
//push() will push new element on top of stack
if(isFull(top)){
cout << "Sorry, but the stack is full!" << endl;
exit(1);
}
else{
top++;
x = stk[top];
}
}
void pop(){
//pop() will pop the top element from the stack
if(isEmpty(top)){
cout << "Sorry, but the stack is empty!" << endl;
exit(1);
}
else{
cout << stk[top] << " is being popped from stack!" << endl;
top--;
}
}
void display_stack(){
//diplay_stack() will show all elements currently in the stack
int temp; //will temporarily hold position of stack
temp = top;
while(!isEmpty(temp)){
cout << stk[temp] << endl;
temp--;
}
}
private:
int top;
int stk[stack_MAX];
};
int menu(){
int choice;
cout << "Welcome to my stack!" << endl;
cout << "What would you like to do? (select corresponding #)" << endl << endl;
cout << "1. Push" << endl;
cout << "2. Pop" << endl;
cout << "3. Display" << endl;
cout << "4. Quit" << endl;
cin >> choice;
return choice;
}
int main()
{
int selection, x;
stack myStack;
selection = menu();
while(selection != 4)
{
switch(selection){
case 1:
cout << "please enter number to be pushed: ";
cin >> x;
myStack.push(x);
selection = menu();
break;
case 2:
myStack.pop();
selection = menu();
break;
case 3:
myStack.display_stack();
selection = menu();
break;
default:
cout << "Oops that's not a selection, try again" << endl;
selection = menu();
break;
}
}
cout << "Thank you for stopping by and using my stack!" << endl;
system("pause");
return 0;
}
您的push
函数中的语句是错误的,修改如下:
void push(int x)
{
//push() will push new element on top of stack
if(isFull(top))
{
cout << "Sorry, but the stack is full!" << endl;
exit(1);
}
else
{
top++;
/***************************
x = stk[top];
****************************/
stk[top] = x;
}
}
建议:
- 学习调试,这里是教程
- 包含头文件
cstdlib
时,你想使用exit
在您的代码 - 不要用与STL中任何类相同的名字命名你的类
正如史前企鹅指出的,你的push()函数是不正确的:
x = stk[top];
应该改成:
stk[top] = x;
无论如何我都想评论一下,按你的要求提供一些一般性的评论:
这样的If语句可以用一行代码替换:
if(top == stack_MAX - 1) return true; else return false;
就变成:
return (stack_MAX - 1 == top);
将常量表达式放在比较表达式的左侧。例如:
(top == stack_MAX - 1)
就变成:
(stack_MAX - 1 == top)
原因是有一天你会不小心输入这样的东西:
(top = stack_MAX - 1)
和你或其他人会浪费很多时间调试它:)
你的isFull()和isEmpty()函数不应该带参数。它们应该只使用私有成员变量top。如果没有对top的访问,怎么能调用这些函数呢?
一般避免
using
。在我看来,它违背了名称空间的全部目的。使用名称空间STD是一种常用的异常,但即便如此,键入STD::cout有那么难吗?总是在if语句的子句周围加上花括号,即使它们只有一行。如果稍后需要在子句中添加更多语句,很容易忘记添加大括号,这可能会非常令人困惑。
你的代码格式很好,但是选择一个括号样式并保持一致。要么总是把左花括号放在函数头/控制语句等的同一行,要么总是把它放在后面的行。
希望对你有帮助。
相关文章:
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么C++总是显示十六进制内存地址,而不仅仅是整数?
- 'cout'将整数显示为十六进制
- 如何在不将其转换为 ASCII 字符的情况下显示 QByteArray 的十六进制值
- 当Cppunit断言失败时,如何显示十六进制值
- 在c++中创建和显示十六进制字符串
- 如何附加到相邻的十六进制值,然后显示
- 如何在单个块中显示十六进制值
- 如何在不显示十六进制的情况下读取dat文件中的文本?C++
- C++.将二进制文件读入缓冲区后,如何以十六进制显示缓冲区
- 为什么该函数以相反的顺序显示十六进制代码
- GDB:显示函数将我的 {type} 的地址打印为整数——想改成十六进制
- 如何获取显示字节十六进制值的字符串
- 在消息中显示每个字符的十六进制
- 为什么无符号字符不能正确显示十六进制值
- 如何用十六进制而不是ASCII符号显示实际值?
- 使用哪个小部件将硬盘扇区显示为十六进制
- 我怎么能阻止我的整数显示为十六进制
- 十六进制转储实用程序c++显示十六进制和Ascii
- 如何比较"4A4B4C"(字符串显示十六进制值)实际字符串"JKL"